码迷,mamicode.com
首页 > 数据库 > 详细

04_NoSQL数据库之Redis数据库:set类型和zset类型

时间:2015-02-13 13:27:39      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:



sets类型及操作

   Set是集合,它是string类型的无序集合。set是通过hash table实现的,添加,删除和查找复杂度都是01)。对集合我们可以取并集、交集、差集。通过这些操作我们可以实现sns中的好友推荐和blogtag功能。

 

sadd:向集合中添加一个元素,通名称为keyset中添加元素。

[toto@localhost bin]$ ./redis-cli

127.0.0.1:6379> sadd myset1 one

(integer) 1

127.0.0.1:6379> sadd myset1 two

(integer) 1

127.0.0.1:6379> sadd myset1 two     再次添加的时候添加不成功了。

(integer) 0

127.0.0.1:6379>

通过smembers查看集合中有哪些元素

127.0.0.1:6379> smembers myset1

1) "two"

2) "one"

127.0.0.1:6379>

 

srem:删除名称为keyset中的元素,删除成功返回1,删除失败返回0

127.0.0.1:6379> smembers myset2

1) "two"

2) "one"

3) "three"

127.0.0.1:6379> srem myset2 two

(integer) 1

127.0.0.1:6379> srem myset2 two

(integer) 0

127.0.0.1:6379> smembers myset2

1) "one"

2) "three"

127.0.0.1:6379>

 

spop:无法指定弹出元素,是随机返回并删除名称为keyset中一个元素。

127.0.0.1:6379> sadd myset3 one

(integer) 1

127.0.0.1:6379> sadd myset3 two

(integer) 1

127.0.0.1:6379> sadd myset3 three

(integer) 1

127.0.0.1:6379> sadd myset3 four

(integer) 1

127.0.0.1:6379> sadd myset3 five

(integer) 1

127.0.0.1:6379> smembers myset3

1) "four"

2) "three"

3) "one"

4) "two"

5) "five"

127.0.0.1:6379> spop myset3     //随机弹出的是three

"three"

127.0.0.1:6379> smembers myset3

1) "one"

2) "four"

3) "five"

4) "two"

127.0.0.1:6379> spop myset3   //随机弹出的是four

"four"

127.0.0.1:6379> smembers myset3

1) "one"

2) "five"

3) "two"

127.0.0.1:6379>

 

sdiff:两个集合的差集。

返回所有给定key与第一个key的差集。

技术分享

127.0.0.1:6379> smembers myset3

1) "one"

2) "four"

3) "five"

4) "two"

127.0.0.1:6379> spop myset3

"four"

127.0.0.1:6379> smembers myset3

1) "one"

2) "five"

3) "two"

127.0.0.1:6379> sadd myset2 three

(integer) 1

127.0.0.1:6379> sadd myset2 four

(integer) 1

127.0.0.1:6379> sadd myset2 one

(integer) 1

127.0.0.1:6379> smembers myset2

1) "one"

2) "four"

3) "three"

127.0.0.1:6379> sdiff myset2 myset3    //去除myset2中含有与myset3元素的值。

1) "three"

2) "four"

127.0.0.1:6379>

 

sdiffstore:返回所有给定key与第一个key的差集,并将结果存为另一个key.

技术分享

sdiffstore myset4 myset2 myset3表示的意思是将myset3myset2的差集存储到myset4里面。

127.0.0.1:6379> sadd myset2 one

(integer) 1

127.0.0.1:6379> sadd myset2 two

(integer) 1

127.0.0.1:6379> sadd myset2 three

(integer) 1

127.0.0.1:6379> sadd myset3 one

(integer) 1

127.0.0.1:6379> sadd myset3 two

(integer) 1

127.0.0.1:6379> sadd myset3 three

(integer) 1

127.0.0.1:6379> sadd myset3 four

(integer) 1

127.0.0.1:6379> sadd myset3 five

(integer) 1

127.0.0.1:6379> sadd myset3 six

(integer) 1

127.0.0.1:6379> sdiff myset2 myset3

(empty list or set)

127.0.0.1:6379> sdiff myset3 myset2  //显示myset3中含有myset2的元素

1) "six"

2) "five"

3) "four"

127.0.0.1:6379> sdiffstore myset4 myset3 myset2myset3myset2的差集存入myset4

(integer) 3

127.0.0.1:6379> smembers myset4

1) "six"

2) "five"

3) "four"

127.0.0.1:6379>

 

sinter:

返回所有给定key的交集

技术分享

技术分享

sinterstore将取出的差集存入到另外的一个集合中。

127.0.0.1:6379> sadd myset1 one

(integer) 1

127.0.0.1:6379> sadd myset1 two

(integer) 1

127.0.0.1:6379> sadd myset2 two

(integer) 1

127.0.0.1:6379> sadd myset2 three

(integer) 1

127.0.0.1:6379> sadd myset2 four

(integer) 1

127.0.0.1:6379> sadd myset2 five

(integer) 1

127.0.0.1:6379> sadd myset3 four

(integer) 1

127.0.0.1:6379> sadd myset3 five

(integer) 1

127.0.0.1:6379> sadd myset3 six

(integer) 1

127.0.0.1:6379> sadd myset3 seven

(integer) 1

127.0.0.1:6379> smembers myset1

1) "two"

2) "one"

127.0.0.1:6379> smembers myset2

1) "four"

2) "three"

3) "five"

4) "two"

127.0.0.1:6379> smembers myset3

1) "seven"

2) "four"

3) "six"

4) "five"

127.0.0.1:6379> sinter myset3 myset2

1) "four"

2) "five"

127.0.0.1:6379> sinterstore myset6 myset3 myset2

(integer) 2

127.0.0.1:6379> smembers myset6

1) "four"

2) "five"

127.0.0.1:6379>

 

sunion:表示插入并集。

返回所有给定key的并集。

127.0.0.1:6379> sadd myset1 one

(integer) 1

127.0.0.1:6379> sadd myset2 two

(integer) 1

127.0.0.1:6379> sadd myset1 two

(integer) 1

127.0.0.1:6379> sadd myset2 three

(integer) 1

127.0.0.1:6379> sadd myset2 four

(integer) 1

127.0.0.1:6379> sunion myset1 myset2    查看并集

1) "three"

2) "two"

3) "four"

4) "one"

127.0.0.1:6379> sunionstore myset7 myset1 myset2   将并集的结果存入myset7

(integer) 4                   //返回值是4表示成功将4个元素插入myset7

127.0.0.1:6379> smembers myset1

1) "two"

2) "one"

127.0.0.1:6379> smembers myset2

1) "three"

2) "two"

3) "four"

127.0.0.1:6379> smembers myset7

1) "three"

2) "two"

3) "four"

4) "one"

127.0.0.1:6379>

 

smove:从第一个key对应的set中移除member并添加到第二个对应的set中。(将第一个集合中的元素剪切下来放入第二个元素中)

技术分享

smove myset2 myset7 three表示:myset2中的three元素扔到myset7

127.0.0.1:6379> smembers myset1

1) "two"

2) "one"

127.0.0.1:6379> smembers myset2

1) "three"

2) "two"

3) "four"

127.0.0.1:6379> sadd myset1 five

(integer) 1

127.0.0.1:6379> smembers myset8

(empty list or set)

127.0.0.1:6379> smove myset1 myset8 two  myset1中的two元素移动到myset8

(integer) 1

127.0.0.1:6379> smembers myset8

1) "two"

127.0.0.1:6379> smembers myset1

1) "five"

2) "one"

127.0.0.1:6379>

 

scard返回名称为keyset的元素个数。(看集合中的元素个数)

127.0.0.1:6379> smembers myset1    myset1集合中的元素有哪些

1) "five"

2) "one"

127.0.0.1:6379> smembers myset2   myset2集合中的元素有哪些

1) "three"

2) "two"

3) "four"

127.0.0.1:6379> smembers myset8   myset8集合中的元素有哪些

1) "two"

127.0.0.1:6379> scard myset1    查看myset1中的元素个数

(integer) 2

127.0.0.1:6379> scard myset2    查看myset2中的元素个数

(integer) 3

127.0.0.1:6379> scard myset8

(integer) 1

127.0.0.1:6379>

 

sismember:测试member是否是名称为keyset的元素。(判断某个元素是否是某个集合中的元素,是返回1,不是返回0

127.0.0.1:6379> smembers myset1

1) "five"

2) "one"

127.0.0.1:6379> sismember myset1 one    判断one是否是myset1中的元素

(integer) 1

127.0.0.1:6379> sismember myset1 two   判断two是否是myset1中的元素

(integer) 0

127.0.0.1:6379>

 

srandmember:随机返回名称为keyset的一个元素,但不删除元素。

技术分享

127.0.0.1:6379> smembers myset1

1) "five"

2) "one"

127.0.0.1:6379> srandmember myset7

"three"

127.0.0.1:6379> srandmember myset7

"two"

127.0.0.1:6379> srandmember myset7

"four"

127.0.0.1:6379> srandmember myset7

"three"

 

Sorted sets类型及操作

    Sortedsetset的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的值调整顺序。可以理解为由两列的mysql表,一列存value,一列存顺序。操作中key理解为zset的名字。

 

zadd:向名称为keyzset中添加元素member,score用于排序。如果该元素存在,则更新器顺序。语法:zadd set名称 顺序

127.0.0.1:6379> zadd myzset 1 "one"

(integer) 1

127.0.0.1:6379> zadd myzset 2 "two"

(integer) 1

127.0.0.1:6379> zadd myzset 3 "two"

(integer) 0

 

这里的0 -1表示顺序号   withscores表示的意思是带上顺序号。

127.0.0.1:6379> zrange myzset 0 -1 withscores

1) "one"

2) "1"

3) "two"

4) "3"

127.0.0.1:6379>

 

再如:

127.0.0.1:6379> zadd sset1 1 two

(integer) 1

127.0.0.1:6379> zadd sset1 2 two

(integer) 0

127.0.0.1:6379> zadd sset1 3 two

(integer) 0

127.0.0.1:6379> zadd sset1 1 one

(integer) 1

127.0.0.1:6379> zrange sset1 0 -1

1) "one"

2) "two"

127.0.0.1:6379> zrange sset1 0 -1 withscores

1) "one"

2) "1"

3) "two"

4) "3"

127.0.0.1:6379>

 

zrem:删除名称为keyzset中的元素member

127.0.0.1:6379> zrange sset1 0 -1 withscores

1) "one"

2) "1"

3) "two"

4) "3"

127.0.0.1:6379> zrem sset1 two

(integer) 1

127.0.0.1:6379> zrange sset1 0 -1 withscores

1) "one"

2) "1"

127.0.0.1:6379>

 

zincrby:如果在名称为keyzset中已经存在元素member,则该元素的score增加increment,否则向该集合中添加该元素,其score的值为increment

127.0.0.1:6379> zrange sset1 0 -1 withscores

1) "one"

2) "1"

127.0.0.1:6379> zincrby sset1 2 one    //one的顺序号增加3,同样可以减2

"3"                               //表示one的顺序号变成了3

127.0.0.1:6379> zrange sset1 0 -1 withscores

1) "one"

2) "3"                             //one的序号变成了3

127.0.0.1:6379>

 

zrank:返回名称为keyzset中的member元素的排名。(按score从小到大排序)即下标。(返回的是索引值,和序号值是有差别的)

127.0.0.1:6379> zadd sset2 1 one

(integer) 1

127.0.0.1:6379> zadd sset2 2 two

(integer) 1

127.0.0.1:6379> zadd sset2 3 three

(integer) 1

127.0.0.1:6379> zadd sset2 4 four

(integer) 1

127.0.0.1:6379> zadd sset2 5 five

(integer) 1

127.0.0.1:6379> zrange sset2 0 -1 withscores

 1) "one"

 2) "1"

 3) "two"

 4) "2"

 5) "three"

 6) "3"

 7) "four"

 8) "4"

 9) "five"

10) "5"

127.0.0.1:6379> zrank sset2 four

(integer) 3        //这里的3表示的是索引值而不是序号值。

127.0.0.1:6379>

 

zrevrank:返回名称为keyzsetmember元素的排名。(按score从大到小排序)即下标。

127.0.0.1:6379> zrange sset2 0 -1 withscores

 1) "one"

 2) "1"

 3) "two"

 4) "2"

 5) "three"

 6) "3"

 7) "four"

 8) "4"

 9) "five"

10) "5"

127.0.0.1:6379> zrevrank sset2 four   //获得four的逆向索引

(integer) 1

127.0.0.1:6379> zrevrank sset2 two   //获得two的逆向索引

(integer) 3

 

zrevrange:返回名称为keyzset(score从大到小顺序)中的indexstartend的所有元素。(也就是说是反向排序)

127.0.0.1:6379> zrevrange sset2 0 -1 withscores

 1) "five"

 2) "5"

 3) "four"

 4) "4"

 5) "three"

 6) "3"

 7) "two"

 8) "2"

 9) "one"

10) "1"

127.0.0.1:6379>

 

zrangebyscore:找到序号在指定范围内的元素

技术分享

127.0.0.1:6379> zrevrange sset2 0 -1 withscores

 1) "five"

 2) "5"

 3) "four"

 4) "4"

 5) "three"

 6) "3"

 7) "two"

 8) "2"

 9) "one"

10) "1"

127.0.0.1:6379> zrangebyscore sset2 2 4 withscores

1) "two"

2) "2"

3) "three"

4) "3"

5) "four"

6) "4"

127.0.0.1:6379>

 

zcount:返回集合中score在给定区间的数量。

127.0.0.1:6379> zrevrange sset2 0 -1 withscores

 1) "five"

 2) "5"

 3) "four"

 4) "4"

 5) "three"

 6) "3"

 7) "two"

 8) "2"

 9) "one"

10) "1"

127.0.0.1:6379> zcount sset2 2 4    表示包括区间值

(integer) 3

127.0.0.1:6379> zcount sset2 4 5    表示包括区间值

(integer) 2

127.0.0.1:6379>

 

zcard:返回集合中的所有元素的个数

127.0.0.1:6379> zrevrange sset2 0 -1 withscores

 1) "five"

 2) "5"

 3) "four"

 4) "4"

 5) "three"

 6) "3"

 7) "two"

 8) "2"

 9) "one"

10) "1"

127.0.0.1:6379> zcard sset2

(integer) 5

127.0.0.1:6379>

 

zremrangebyrank:删除集合中排名在给定区间的元素(按照索引来删除)。

技术分享

127.0.0.1:6379> zrange sset2 0 -1 withscores

 1) "one"

 2) "1"

 3) "two"

 4) "2"

 5) "three"

 6) "3"

 7) "four"

 8) "4"

 9) "five"

10) "5"

127.0.0.1:6379> zremrangebyrank sset2 2 3    //注意这里的2 3表示的是索引

(integer) 2

127.0.0.1:6379> zrange sset2 0 -1 withscores

1) "one"

2) "1"

3) "two"

4) "2"

5) "five"

6) "5"

127.0.0.1:6379>

 

zremrangebyscore:删除集合中score在给定区间的元素(表示的是按照顺序删除)。

技术分享

127.0.0.1:6379> zrange sset2 0 -1 withscores

1) "one"

2) "1"

3) "two"

4) "2"

5) "five"

6) "5"

127.0.0.1:6379> zremrangebyscore sset2 3 4   //没有删除成功的时候返回0

(integer) 0

127.0.0.1:6379> zrange sset2 0 -1 withscores

1) "one"

2) "1"

3) "two"

4) "2"

5) "five"

6) "5"

127.0.0.1:6379> zremrangebyscore sset2 2 5   //删除序号为25之间的元素

(integer) 2

127.0.0.1:6379> zrange sset2 0 -1 withscores

1) "one"

2) "1"

127.0.0.1:6379>

 

 

 

 

 

04_NoSQL数据库之Redis数据库:set类型和zset类型

标签:

原文地址:http://blog.csdn.net/tototuzuoquan/article/details/43792875

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!