Redis 数据库集合对象(set object)是由string类型的无重复元素的无需集合,底层编码可以是intset或者hashtable。intset编码的集合对象用整数集合最为底层实现,所有对象元素保存在整数集合中。
Python的redis模块实现了 SADD、SCARD 、SDIFF 、SDIFFSTORE、SINTER 、SINTERSTORE、SISMEMBER 、SMEMBERS 、SMOVE、SPOP、SRANDMEMBER、SREM、SUNION、SUNIONSTORE操作命令的基本用法,但是在用用版本的redis模块中未见SSCAN命令操作相关实现方法。
函数说明
- SADD: 向集合对象添加成员,sadd(self, name, value)
- SCARD : 获取集合元素个数,scard(self, name)
- SDIFF: 返回给定多个集合对象的差集(set key1-set key2..-set keyn),从左到右计算集合差集,sdiff(self, keys, *args)
- SDIFFSTORE: 返回给定给定多个集合对象的差集并存储在目标(dest)集合中,sdiffstore(self, dest, keys, *args)
- SINTER: 返回给定所有集合(keys, *args)的交集, sinter(self, keys, *args)
- SINTERSTORE: 返回给定所有集合(keys, *args)的交集并存储在 集合(dest) 中, sinterstore(self, dest, keys, *args)
- SISMEMBER: 判断 value元素是否是集合 name 的成员,返回布尔逻辑值True或者False, sismember(self, name, value)
- SMEMBERS: 返回集合中所元素值的Python集合类型数据,smembers(self, name)
- SMOVE: 将元素value从集合src移动到 集合 dest。若元素value在集合src中不存在,则集合dest中不会添加元素value, smove(self, src, dest, value)
- SPOP: 移除并返回集合中的一个随机元素, spop(self, name)
- SRANDMEMBER: 返回集合中一个随机数,但是不做移除操作, srandmember(self, name)。从 Redis 2.6 版本开始, 在命令行下Srandmember 命令接受可选返回元素数量的参数
redis>SRANDMEMBER name count
- SREM: 移除集合中一个元素,srem(self, name, value),redis模块任然沿用 Redis 2.4 版本以前的只接受单个元素的用法。
- SUNION: 返回所有给定集合的并集中所有元素,sunion(self, keys, *args)
- SUNIONSTORE: 所有给定集合的并集存储在集合dest 中, sunionstore(self, dest, keys, *args)
代码示例
#!/usr/bin/python
import redis
client = redis.Redis(host='localhost', port=6379, db=0)
if client.ping():
print "Connect to server successfully!"
else:
print "Connect to server failed!"
# Build a set myset (sadd),
# redis support add multiple, maybe python module limit it to one
client.sadd('myset','redis')
client.sadd('myset','hello')
client.sadd('myset','bar')
# Return all members of the set ``name``,smembers(self, name)
setVal = client.smembers('myset')
print "All members in the set:", setVal
# Get numbers of set member, scard(self,name)
setNum = client.scard('myset')
print "Get numbers of element in the set:",setNum
# Return the difference of sets specified by ``keys``, sdiff(self, keys *args)
client.sadd('myset1','redis')
#client.sadd('myset1','hello')
client.sadd('myset2','bar')
client.sadd('myset1','hi')
setDiff = client.sdiff(['myset','myset1','myset2'])
print "The difference of sets specified by keys:",setDiff
# Store the difference of sets specified by ``keys`` into a new
# set named ``dest``. Returns the number of keys in the new set.
# sdiffstore(self, dest, keys, *args)
client.sdiffstore('setdiff',['myset','myset1'])
setDiff = client.smembers('setdiff')
print "The difference of sets store in the new set setDiff:", setDiff
# Return the intersection of sets specified by ``keys``, sinter(self, keys, *args)
setInter = client.sinter(['myset','myset1'])
print "The intersection of sets specified by keys:",setInter
# sinterstore(self, dest, keys, *args)
# Store the intersection of sets specified by ``keys`` into a new
# set named ``dest``. Returns the number of keys in the new set.
client.sinterstore('setinter',['myset','myset1'])
setInter = client.smembers('setinter')
print "The intererence of sets store in the new set setInter:", setInter
# Judge if element belongs to te set(sismember)
judge =['redis','foo']
for ele in judge:
boonVal = client.sismember('myset',ele)
if boonVal:
print "Element",ele," belongs to set myset"
else:
print "Element",ele," doesn't belong to set myset"
# smove(self, src, dst, value)
# Move ``value`` from set ``src`` to set ``dst`` atomically
client.sadd('myset','world')
client.smove('myset','myset1','world')
setNew = client.smembers('myset1')
print "The distination set:", setNew
# Remove and return a random member of set ``name``, spop(self, name)
spop = client.spop('myset1')
print "Get return random element:",spop, ", remaining elements:",client.smembers('myset1')
# Return a random member of set ``name``, srandmember(self, name),
# >2.6 return numbers can be self define
srandom = client.srandmember('myset')
print "Return random element:",srandom
# Remove ``value`` from set ``name``, srem(self, name, value)
rem = client.srem('myset','hello')
print "Get srem boolean:",rem
# sunion(self, keys, *args)
# Return the union of sets specifiued by ``keys``
client.sadd('myset0','special')
setUnion = client.sunion(['myset','myset0','myset1'])
print "The union of sets specified by keys:",setUnion
# sunionstore(self, dest, keys, *args)
# Store the union of sets specified by ``keys`` into a new
# set named ``dest``. Returns the number of keys in the new set.
client.sunionstore('setunion',['myset','myset1'])
setUnion = client.smembers('setunion')
print "The union of sets store in the new set setUnion:", setUnion
# no SSCAN
#Empty db
client.flushdb()
结果释疑
运行差集部分涉及到的集合有myset(‘redis’, ‘bar’, ‘hello’), myset1(‘redis’, ‘hi’), myset2(‘bar’), 按照正常逻辑diff =myset-myset1-myset2, diff1=myset-myset1=( ‘bar’, ‘hello’), diff= diff1-myset2=(‘hello’),对应第一行差集结果,而第二行差集结果显示我diff1内容,说明sdiff以及sdiffstore很好的支持了多个集合差集滴运算。该部分运行结果如下:
The difference of sets specified by keys: set(['hello'])
The difference of sets store in the new set setDiff: set(['bar', 'hello'])
参考资料
1、Redis 集合(Set)
2、Python redis文档(python交互模式下命令>>>help redis
)
3、Redis设计与实现(数据库技术丛书),黄建宏
4、Python中的集合类型(笔记)