(10-6)redis -set 数据类型

(10-6)redis -set 数据类型


启动redis:
[root@baozi ~]# redis-server /etc/redis.conf
[root@baozi ~]# redis-cli
127.0.0.1:6379>


redis数据类型--set
1、set集合中的元素都是不重复的、无序的,一个集合类型键可以存储 2^32 -1 个元素;

常用命令:
1、sadd/smembers/srem/sismember
2、sdiff/sinter/sunion
3、sdiffstroe/sinterstore/sunionstore
4、scard/spop
5、srandmember
	count>=0,若小于集合基数,返回一个count个不相同的元素的数组;
		 若大于集合基数,返回整个集合;
	count<0,返回一个包含count个元素的数组,元素可以相同,返回数组可以大于集合基数;



//增加元素
127.0.0.1:6379> sadd set1 a
(integer) 1
127.0.0.1:6379> sadd set1 b
(integer) 1

//遍历元素
127.0.0.1:6379> smembers set1
1) "b"
2) "a"

//删除指定元素
127.0.0.1:6379> srem set1 a
(integer) 1
127.0.0.1:6379> smembers set1
1) "b"

//查看某元素是否存在
127.0.0.1:6379> sismember set1 b
(integer) 1
127.0.0.1:6379>


//差集,集合set1存在,而set2不存在的元素
127.0.0.1:6379> smembers set1
1) "b"
2) "a"
127.0.0.1:6379> smembers set2
1) "c"
2) "b"
127.0.0.1:6379> sdiff set1 set2
1) "a"
127.0.0.1:6379>


//求交集
127.0.0.1:6379> sinter set1 set2
1) "b"

//求并集
127.0.0.1:6379> sunion set1 set2
1) "c"
2) "b"
3) "a"
127.0.0.1:6379>



//把set1和set2的并集结果存在set0中
127.0.0.1:6379> sunionstore set0 set1 set2
(integer) 3
127.0.0.1:6379> type set0
set
127.0.0.1:6379> smembers set0
1) "c"
2) "b"
3) "a"
127.0.0.1:6379>



127.0.0.1:6379> smembers set0
1) "c"
2) "b"
3) "a"
//求集合元素个数
127.0.0.1:6379> scard set0
(integer) 3
//随机去除一个元素并删除
127.0.0.1:6379> spop set0
"b"
127.0.0.1:6379> smembers set0
1) "c"
2) "a"
127.0.0.1:6379>


//随机取出一个元素
127.0.0.1:6379> smembers set0
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> srandmember set0
"b"
127.0.0.1:6379> smembers set0
1) "c"
2) "b"
3) "a"
127.0.0.1:6379>


//正数时,若大于集合基数,显示整个集合
127.0.0.1:6379> smembers set0
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> srandmember set0 4
1) "c"
2) "b"
3) "a"
//负数时,显示个数可以大于集合基数,可以重复
127.0.0.1:6379> srandmember set0 -6
1) "c"
2) "c"
3) "c"
4) "b"
5) "a"
6) "a"
127.0.0.1:6379>





你可能感兴趣的:(Bigdatda-redis)