Redis 的数据类型 - Zset 集合类型:有序集合

ZADD:将元素及其分数添加到集合中

  语法:ZADD key courseScore member [courseScore member]

    ZADD courseScore 100 tom

    ZADD courseScore 99 jerry 88 mario 77 jack 66 lucy 55 chris

    ZADD courseScore 60 tom 67 apple 56 cherry  #这里 tom 已经存在了,直接略过不执行,返回值为 2 #

    ZADD courseScore 12.3 lenovo

    ZADD courseScore +inf maxInt -inf minInt

    ZSCORE courseScore maxInt

    ZSCORE courseScore minInt

127.0.0.1:6379> ZADD courseScore 100 tom
(integer) 1

127.0.0.1:6379> ZADD courseScore 99 jerry 88 mario 77 jack 66 lucy 55 chris
(integer) 5

127.0.0.1:6379> ZADD courseScore 60 tom 67 apple 56 cherry
(integer) 2

127.0.0.1:6379> ZADD courseScore 12.3 lenovo
(integer) 1

127.0.0.1:6379> ZADD courseScore +inf maxInt -inf minInt
(integer) 1

127.0.0.1:6379> ZSCORE courseScore maxInt
"inf"

127.0.0.1:6379> ZSCORE courseScore minInt
"-inf"

 

ZSCORE:获得指定元素的分数

  语法:ZSCORE key member

    ZSCORE courseScore mario

    ZSCORE courseScore lenovo

127.0.0.1:6379> ZSCORE courseScore mario
"88"

127.0.0.1:6379> ZSCORE courseScore lenovo
"12.300000000000001"

 

ZRANGE:按照元素分数从小到大的顺序返回指定索引 start 到 stop 之间所有元素(包含两端)

  语法:ZRANGE key start stop [WITHSCORES]

    ZRANGE courseScore 0 -1

    ZRANGE courseScore 0 -1 WITHSCORES

    ZRANGE courseScore 0 2 WITHSCORES

    ZRANGE courseScore 0 2000 WITHSCORES

    ZRANGE courseScore 1000 2000 WITHSCORES

    ZADD courseScore 60 test1 60 test2 60 test3 60 test4

    ZRANGE courseScore 0 -1 WITHSCORES

  #当两个元素的分数相同的时候,Redis 在排序按照字典的顺序 (0<9<A<Z<a<z) ,如果使用的是 UTF-8 的编码方式的中文同样按照字典顺序排列#

127.0.0.1:6379> ZRANGE courseScore 0 -1
 1) "minInt"
 2) "minInx"
 3) "lenovo"
 4) "chris"
 5) "cherry"
 6) "tom"
 7) "lucy"
 8) "apple"
 9) "jack"
10) "mario"
11) "jerry"
12) "maxInt"

127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES
 1) "minInt"
 2) "-inf"
 3) "minInx"
 4) "-inf"
 5) "lenovo"
 6) "12.300000000000001"
 7) "chris"
 8) "55"
 9) "cherry"
10) "56"
11) "tom"
12) "60"
13) "lucy"
14) "66"
15) "apple"
16) "67"
17) "jack"
18) "77"
19) "mario"
20) "88"
21) "jerry"
22) "99"
23) "maxInt"
24) "inf"

127.0.0.1:6379> ZRANGE courseScore 0 2 WITHSCORES
1) "minInt"
2) "-inf"
3) "minInx"
4) "-inf"
5) "lenovo"
6) "12.300000000000001"

127.0.0.1:6379> ZRANGE courseScore 0 2000 WITHSCORES
 1) "minInt"
 2) "-inf"
 3) "minInx"
 4) "-inf"
 5) "lenovo"
 6) "12.300000000000001"
 7) "chris"
 8) "55"
 9) "cherry"
10) "56"
11) "tom"
12) "60"
13) "lucy"
14) "66"
15) "apple"
16) "67"
17) "jack"
18) "77"
19) "mario"
20) "88"
21) "jerry"
22) "99"
23) "maxInt"
24) "inf"

127.0.0.1:6379> ZRANGE courseScore 1000 2000 WITHSCORES
(empty list or set)

127.0.0.1:6379> ZADD courseScore 60 test1 60 test2 60 test3 60 test4
(integer) 4

127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES
 1) "minInt"
 2) "-inf"
 3) "minInx"
 4) "-inf"
 5) "lenovo"
 6) "12.300000000000001"
 7) "chris"
 8) "55"
 9) "cherry"
10) "56"
11) "test1"
12) "60"
13) "test2"
14) "60"
15) "test3"
16) "60"
17) "test4"
18) "60"
19) "tom"
20) "60"
21) "lucy"
22) "66"
23) "apple"
24) "67"
25) "jack"
26) "77"
27) "mario"
28) "88"
29) "jerry"
30) "99"
31) "maxInt"
32) "inf"

 

ZREVRANGE:和 ZRANGE 相反,按照分数从大到小的顺序

  语法:ZREVRANGE key start stop [WITHSCORES]

    ZREVRANGE courseScore 0 -1 WITHSCORES

127.0.0.1:6379> ZREVRANGE courseScore 0 -1 WITHSCORES
 1) "maxInt"
 2) "inf"
 3) "jerry"
 4) "99"
 5) "mario"
 6) "88"
 7) "jack"
 8) "77"
 9) "apple"
10) "67"
11) "lucy"
12) "66"
13) "tom"
14) "60"
15) "test4"
16) "60"
17) "test3"
18) "60"
19) "test2"
20) "60"
21) "test1"
22) "60"
23) "cherry"
24) "56"
25) "chris"
26) "55"
27) "lenovo"
28) "12.300000000000001"
29) "minInx"
30) "-inf"
31) "minInt"
32) "-inf"

 

ZRANGEBYSCORE:获得指定分数范围内的元素,按照分数从小到大的顺序,返回的是分数在指定的 min 到 max 的元素

  语法:ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

    ZRANGEBYSCORE courseScore 80 90   #获得分数80~90之间的所有元素

    ZRANGEBYSCORE courseScore 60 90 WITHSCORES   #获得分数60~90之间的所有元素

    ZRANGEBYSCORE courseScore 60 (88 WITHSCORES

    ZRANGEBYSCORE courseScore (60 (88 WITHSCORES   #通过 '(' 代表不包端点#

    ZRANGEBYSCORE courseScore 90 +inf WITHSCORES

    ZRANGEBYSCORE courseScore 80 +inf WITHSCORES LIMIT 0 3

    ZRANGEBYSCORE courseScore 60 +inf WITHSCORES LIMIT 3 3

127.0.0.1:6379> ZRANGEBYSCORE courseScore 80 90
1) "mario"

127.0.0.1:6379> ZRANGEBYSCORE courseScore 60 90 WITHSCORES
 1) "test1"
 2) "60"
 3) "test2"
 4) "60"
 5) "test3"
 6) "60"
 7) "test4"
 8) "60"
 9) "tom"
10) "60"
11) "lucy"
12) "66"
13) "apple"
14) "67"
15) "jack"
16) "77"
17) "mario"
18) "88"

127.0.0.1:6379> ZRANGEBYSCORE courseScore 60 (88 WITHSCORES
 1) "test1"
 2) "60"
 3) "test2"
 4) "60"
 5) "test3"
 6) "60"
 7) "test4"
 8) "60"
 9) "tom"
10) "60"
11) "lucy"
12) "66"
13) "apple"
14) "67"
15) "jack"
16) "77"

127.0.0.1:6379> ZRANGEBYSCORE courseScore (60 (88 WITHSCORES
1) "lucy"
2) "66"
3) "apple"
4) "67"
5) "jack"
6) "77"

127.0.0.1:6379> ZRANGEBYSCORE courseScore 90 +inf WITHSCORES
1) "jerry"
2) "99"
3) "maxInt"
4) "inf"

127.0.0.1:6379> ZRANGEBYSCORE courseScore 80 +inf WITHSCORES LIMIT 0 3
1) "mario"
2) "88"
3) "jerry"
4) "99"
5) "maxInt"
6) "inf"

127.0.0.1:6379> ZRANGEBYSCORE courseScore 60 +inf WITHSCORES LIMIT 3 3
1) "test4"
2) "60"
3) "tom"
4) "60"
5) "lucy"
6) "66"

 

ZREVRANGEBYSCORE:获得指定分数范围内的元素,按照元素的分数从大到小的顺序返回 min 和 max 之间的元素

  语法:ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]

    ZREVRANGEBYSCORE courseScore 90 60 WITHSCORES

127.0.0.1:6379> ZREVRANGEBYSCORE courseScore 90 60 WITHSCORES
 1) "mario"
 2) "88"
 3) "jack"
 4) "77"
 5) "apple"
 6) "67"
 7) "lucy"
 8) "66"
 9) "tom"
10) "60"
11) "test4"
12) "60"
13) "test3"
14) "60"
15) "test2"
16) "60"
17) "test1"
18) "60"

 

ZINCRBY:操作某个元素的分数,返回操作之后的分数

  语法:ZINCRBY key increment member

    ZINCRBY courseScore 5 tom

    ZINCRBY courseScore -15 tom

127.0.0.1:6379> ZINCRBY courseScore 5 tom
"65"

127.0.0.1:6379> ZINCRBY courseScore -15 tom
"50"

 

ZCARD:获得集合中元素的数量

  语法:ZCARD key

    ZCARD courseScore

127.0.0.1:6379> ZCARD courseScore
(integer) 16

 

ZCONUT:获得指定分数内的元素的数量

  语法:ZCOUNT key min max

    ZCOUNT courseScore 80 90

127.0.0.1:6379> ZCOUNT courseScore 80 90
(integer) 1

 

ZREM:删除一个或者多个元素,返回删除元素的个数

  语法:ZREM key member ...

    ZREM courseScore tom

127.0.0.1:6379> ZREM courseScore tom
(integer) 1

 

ZREMRANGEBYRANK:按照排名范围删除元素,按照分数从小到大的顺序删除所指定的排名范围内的所有元素

  语法:ZREMRANGEBYRANK key start stop

    ZREMRANGEBYRANK courseScore 0 5

    ZRANGE courseScore 0 -1 WITHSCORES

127.0.0.1:6379> ZREMRANGEBYRANK courseScore 0 5
(integer) 6

127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES
 1) "test4"
 2) "60"
 3) "lucy"
 4) "66"
 5) "apple"
 6) "67"
 7) "jack"
 8) "77"
 9) "mario"
10) "88"
11) "jerry"
12) "99"
13) "tom"
14) "100"
15) "maxInt"
16) "inf"

 

ZREMRANGEBYSCORE:按照分数范围删除元素

  语法:ZREMRANGEBYSCORE key min max

    ZREMRANGEBYSCORE courseScore 60 70

    ZRANGE courseScore 0 -1 WITHSCORES

127.0.0.1:6379> ZREMRANGEBYSCORE courseScore 60 70
(integer) 3

127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES
 1) "jack"
 2) "77"
 3) "mario"
 4) "88"
 5) "jerry"
 6) "99"
 7) "tom"
 8) "100"
 9) "maxInt"
10) "inf"

 

ZRANK:获得指定元素的排名,根据分数从小到大的顺序

  语法:ZRANK key member

    ZRANK courseScore tom

127.0.0.1:6379> ZRANK courseScore tom
(integer) 3

 

ZREVRANK:获得指定元素的排名,根据分数从大到小的顺序

  语法:ZREVRANK key member

    ZREVRANK courseScore tom

127.0.0.1:6379> ZREVRANK courseScore tom
(integer) 1

 

ZINTERSTORE:计算有序集合的交集,并将结果保存起来

  语法:ZINTERSTORE destination numkeys [WEIGHTS weight weight...] [AGGREGATE SUM | MIN | MAX]

    ZADD testSortedSet1 1 a 2 b 3 c

    ZADD testSortedSet2 10 a 20 b 30 c

    ZINTERSTORE resTestSorted1 2 testSortedSet1 testSortedSet2

    ZRANGE resTestSorted1 0 -1 WITHSCORES

    ZINTERSTORE resTestSorted2 2 testSortedSet1 testSortedSet2 AGGREGATE SUM  #返回多个集合中的分数的和#

    ZRANGE resTestSorted2 0 -1 WITHSCORES  # 1+10 2+20 3+30 #

    ZINTERSTORE resTestSorted3 2 testSortedSet1 testSortedSet2 AGGREGATE MIN  #返回多个集合中分数最小的值#

    ZRANGE resTestSorted3 0 -1 WITHSCORES  # 1 2 3 #

    ZINTERSTORE resTestSorted4 2 testSortedSet1 testSortedSet2 AGGREGATE MAX  #返回多个集合中分数最小的值#

    ZRANGE resTestSorted4 0 -1 WITHSCORES  # 10 20 30 #

    ZINTERSTORE resTestSorted5 2 testSortedSet1 testSortedSet2 WEIGHTS 2 0.2  

    #返回第一个集合分数的 2 倍,第二个集合分数的 0.2倍,这里第三个参数没写默认使用 SUM 求两次重赋值的和#

    ZRANGE resTestSorted5 0 -1 WITHSCORES  # 1x2+10x0.2 2x2+20x0.2 3x2+30x0.2 #

127.0.0.1:6379> ZADD testSortedSet1 1 a 2 b 3 c
(integer) 3

127.0.0.1:6379> ZADD testSortedSet2 10 a 20 b 30 c
(integer) 3

127.0.0.1:6379> ZINTERSTORE resTestSorted1 2  testSortedSet1 testSortedSet2
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted1 0 -1
1) "a"
2) "b"
3) "c"

127.0.0.1:6379> ZRANGE resTestSorted1 0 -1 WITHSCORES
1) "a"
2) "11"
3) "b"
4) "22"
5) "c"
6) "33"

127.0.0.1:6379> ZINTERSTORE resTestSorted2 2 testSortedSet2 testSortedSet2 AGGREGATE SUM
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted2 0 -1 WITHSCORES
1) "a"
2) "20"
3) "b"
4) "40"
5) "c"
6) "60"

127.0.0.1:6379> ZINTERSTORE resTestSorted2 2 testSortedSet1 testSortedSet2 AGGREGATE SUM
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted2 0 -1 WITHSCORES
1) "a"
2) "11"
3) "b"
4) "22"
5) "c"
6) "33"

127.0.0.1:6379> ZINTERSTORE resTestSorted3 2 testSortedSet1 testSortedSet2 AGGREGATE MIN
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted3 0 -1 WITHSCORES
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"

127.0.0.1:6379> ZINTERSTORE resTestSorted4 2 testSortedSet1 testSortedSet2 AGGREGATE MAX
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted4 0 -1 WITHSCORES
1) "a"
2) "10"
3) "b"
4) "20"
5) "c"
6) "30"

127.0.0.1:6379> ZINTERSTORE resTestSorted5 2 testSortedSet1 testSortedSet2 WEIGHTS 2 0.2
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted5 0 -1 WITHSCORES
1) "a"
2) "4"
3) "b"
4) "8"
5) "c"
6) "12"

 

ZUNIONSTORE:计算有序集合并集,将结果保存起来

  语法:ZUNIONSTORE destination numkeys key key ... [WEIGHTS weight weight...] [AGGREGATE SUM | MIN | MAX]

    ZADD TESTUNIONSET1 1 a 2 b 3 c

    ZADD TESTUNIONSET2 4 d 5 e 6 f 7 a

    ZUNIONSTORE uniRes1 2 TESTUNIONSET1 TESTUNIONSET2  #返回多个集合的交际,这里第二个参数没写使用默认值 SUM #

    ZRANGE uniRes1 0 -1 WITHSCORES  # a=1+7 b=2 c=3 d=4 e=5 f=6 #

    ZUNIONSTORE uniRes2 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE SUM  #返回多个集合的交际 #

    ZRANGE uniRes2 0 -1 WITHSCORES  # a=1+7 b=2 c=3 d=4 e=5 f=6 #

    ZUNIONSTORE uniRes3 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MIN

    ZRANGE uniRes3 0 -1 WITHSCORES  # a=1 b=2 c=3 d=4 e=5 f=6 #

    ZUNIONSTORE uniRes4 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MAX

    ZRANGE uniRes4 0 -1 WITHSCORES  # a=7 b=2 c=3 d=4 e=5 f=6 #

    ZUNIONSTORE uniRes5 2 TESTUNIONSET1 TESTUNIONSET2 WEIGHTS 2 2

    ZRANGE uniRes5 0 -1 WITHSCORES  # a=1x2+7x2 b=2x2 c=3x2 d=4x2 e=5x2 f=6x2 #

127.0.0.1:6379> ZADD TESTUNIONSET1 1 a 2 b 3 c
(integer) 3

127.0.0.1:6379> ZADD TESTUNIONSET2 4 d 5 e 6 f 7 a
(integer) 4

127.0.0.1:6379> ZUNIONSTORE uniRes1 2  TESTUNIONSET1 TESTUNIONSET2
(integer) 6

127.0.0.1:6379> ZRANGE uniRes1 0 -1 WITHSCORES
 1) "b"
 2) "2"
 3) "c"
 4) "3"
 5) "d"
 6) "4"
 7) "e"
 8) "5"
 9) "f"
10) "6"
11) "a"
12) "8"

127.0.0.1:6379> ZUNIONSTORE uniRes2 2  TESTUNIONSET1 TESTUNIONSET2 AGGREGATE SUM
(integer) 6

127.0.0.1:6379> ZRANGE uniRes2 0 -1 WITHSCORES
 1) "b"
 2) "2"
 3) "c"
 4) "3"
 5) "d"
 6) "4"
 7) "e"
 8) "5"
 9) "f"
10) "6"
11) "a"
12) "8"

127.0.0.1:6379> ZUNIONSTORE uniRes3 2  TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MIN
(integer) 6

127.0.0.1:6379> ZRANGE uniRes3 0 -1 WITHSCORES
 1) "a"
 2) "1"
 3) "b"
 4) "2"
 5) "c"
 6) "3"
 7) "d"
 8) "4"
 9) "e"
10) "5"
11) "f"
12) "6"

127.0.0.1:6379> ZUNIONSTORE uniRes4 2  TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MAX
(integer) 6

127.0.0.1:6379> ZRANGE uniRes4 0 -1 WITHSCORES
 1) "b"
 2) "2"
 3) "c"
 4) "3"
 5) "d"
 6) "4"
 7) "e"
 8) "5"
 9) "f"
10) "6"
11) "a"
12) "7"

127.0.0.1:6379> ZUNIONSTORE uniRes5 2  TESTUNIONSET1 TESTUNIONSET2  WEIGHTS 2 2
(integer) 6

127.0.0.1:6379> ZRANGE uniRes5 0 -1 WITHSCORES
 1) "b"
 2) "4"
 3) "c"
 4) "6"
 5) "d"
 6) "8"
 7) "e"
 8) "10"
 9) "f"
10) "12"
11) "a"
12) "16"

 

posted @ 2017-06-30 21:51  Shuo_128  阅读(388)  评论(0编辑  收藏  举报