集合 set 相关命令

作者:吴泽鑫 分类: Redis 发布于:2015-11-1 22:20 ė583次浏览 60条评论

集合的性质: 唯一性,无序性,确定性

注: 在string和link的命令中,可以通过range 来访问string中的某几个字符或某几个元素

但,因为集合的无序性,无法通过下标或范围来访问部分元素.

因此想看元素,要么随机先一个,要么全选

sadd key  value1 value2

作用: 往集合key中增加元素

例子:

127.0.0.1:6379> sadd set v1 v2 v3
(integer) 3



srem key value1 value2

作用: 删除集合中集为 value1 value2的元素

返回值: 忽略不存在的元素后,真正删除掉的元素的个数

例子:

127.0.0.1:6379> srem set v1 v3 v4
(integer) 2



spop key

作用: 返回并删除集合中key中1个随机元素


随机--体现了无序性

例子:

127.0.0.1:6379> sadd set v4 v5 v6 v7
(integer) 4
127.0.0.1:6379> spop set
"v2"
127.0.0.1:6379> spop set
"v5"



srandmember key

作用: 返回集合key中,随机的1个元素.

例子:

127.0.0.1:6379> srandmember set
"v7"
127.0.0.1:6379> srandmember set
"v4"
127.0.0.1:6379> srandmember set
"v6"



sismember key  value

作用: 判断value是否在key集合中

是返回1,否返回0

例子:

127.0.0.1:6379> smembers set
1) "v6"
2) "v7"
3) "v4"
127.0.0.1:6379> sismember set v4
(integer) 1
127.0.0.1:6379> sismember set v2
(integer) 0



smembers key

作用: 返回集中中所有的元素

例子:

127.0.0.1:6379> smembers set
1) "v6"
2) "v7"
3) "v4"



scard key

作用: 返回集合中元素的个数

例子:

127.0.0.1:6379> smembers set
1) "v6"
2) "v7"
3) "v4"
127.0.0.1:6379> scard set
(integer) 3



smove source dest value

作用:把source中的value删除,并添加到dest集合中

例子:

127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> sadd set1 v1 v2 v3
(integer) 3
127.0.0.1:6379> sadd set2 v4 v5 v6
(integer) 3
127.0.0.1:6379> smembers set1
1) "v3"
2) "v2"
3) "v1"
127.0.0.1:6379> smembers set2
1) "v6"
2) "v5"
3) "v4"
127.0.0.1:6379> smove set1 set2 v3
(integer) 1
127.0.0.1:6379> smmebres set1
(error) ERR unknown command 'smmebres'
127.0.0.1:6379> smembers set1
1) "v2"
2) "v1"
127.0.0.1:6379> smembers set2
1) "v3"
2) "v6"
3) "v5"
4) "v4"



sinter  key1 key2 key3

作用: 求出key1 key2 key3 三个集合中的交集,并返回

例子:

127.0.0.1:6379> smembers set1 
1) "v3"
2) "v2"
3) "v5"
4) "v1"
5) "v4"
127.0.0.1:6379> smembers set2
1) "v3"
2) "v6"
3) "v5"
4) "v4"
127.0.0.1:6379> smembers set3
1) "v3"
2) "v2"
3) "v4"
127.0.0.1:6379> sinter set1 set2 set3
1) "v3"
2) "v4"


redis 127.0.0.1:6379> sadd s1 0 2 4 6

(integer) 4

redis 127.0.0.1:6379> sadd s2 1 2 3 4

(integer) 4

redis 127.0.0.1:6379> sadd s3 4 8 9 12

(integer) 4

redis 127.0.0.1:6379> sinter s1 s2 s3

1) "4"

redis 127.0.0.1:6379> sinter s3 s1 s2

1) "4"


sinterstore dest key1 key2 key3

作用: 求出key1 key2 key3 三个集合中的交集,并赋给dest

例子:

127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> sadd set1 v1 v2 v3 v4
(integer) 4
127.0.0.1:6379> sadd set2 v1 v3 v5
(integer) 3
127.0.0.1:6379> sinterstore set3 set1 set2
(integer) 2
127.0.0.1:6379> members set3
(error) ERR unknown command 'members'
127.0.0.1:6379> smembers set3
1) "v3"
2) "v1"




sunion key1 key2.. Keyn

作用: 求出key1 key2 keyn的并集,并返回

例子:

127.0.0.1:6379> smembers set1
1) "v3"
2) "v2"
3) "v4"
4) "v1"
127.0.0.1:6379> smembers set2
1) "v3"
2) "v5"
3) "v1"
127.0.0.1:6379> sunion set1 set2
1) "v1"
2) "v2"
3) "v5"
4) "v3"
5) "v4"



sdiff key1 key2 key3

作用: 求出key1与key2 key3的差集,属于key1,但不属于key2,key3

即key1-key2-key3

例子:

127.0.0.1:6379> smembers set1
1) "v3"
2) "v2"
3) "v4"
4) "v1"
127.0.0.1:6379> smembers set2
1) "v3"
2) "v5"
3) "v1"
127.0.0.1:6379> smembers set3
1) "v3"
2) "v4"
3) "v8"
4) "v1"
127.0.0.1:6379> sdiff set1 set2 set3
1) "v2"



本文出自 Jensen-吴泽鑫的博客,转载时请注明出处及相应链接:http://www.wuzexin.cn/post-6.html

发表评论

电子邮件地址不会被公开。必填项已用*标注


Ɣ回顶部