Redis从入门到放弃06——Sorted-sets(zset)数据类型

338 阅读13分钟

Redis从入门到放弃06——Sorted-set(Zset)数据类型

Sorted-set 数据类型介绍(官网原文)

A Redis sorted set is a collection of unique strings (members) ordered by an associated score. When more than one string has the same score, the strings are ordered lexicographically

You can think of sorted sets as a mix between a Set and a Hash. Like sets, sorted sets are composed of unique, non-repeating string elements, so in some sense a sorted set is a set as well.


Redis sorted-sets 是由一个相关分数排序的唯一字符串(成员)的集合。当多个字符串具有相同的 score 值时,这些字符串按字典顺序排序

你可以把sorted-sets看作是Set和Hash的混合体。与集合一样,sorted-sets 由唯一的、不重复的字符串元素组成,因此在某种意义上,sorted-sets也是一个集合。

sorted-set 数据类型常用的命令

查看sorted-set数据类型下所有命令 :help @sorted-set

127.0.0.1:6379> help @sorted-set

  BZMPOP timeout numkeys key [key ...] MIN|MAX [COUNT count]
  summary: Remove and return members with scores in a sorted set or block until one is available
  since: 7.0.0

  BZPOPMAX key [key ...] timeout
  summary: Remove and return the member with the highest score from one or more sorted sets, or block until one is available
  since: 5.0.0

  BZPOPMIN key [key ...] timeout
  summary: Remove and return the member with the lowest score from one or more sorted sets, or block until one is available
  since: 5.0.0

  ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]
  summary: Add one or more members to a sorted set, or update its score if it already exists
  since: 1.2.0

  ZCARD key
  summary: Get the number of members in a sorted set
  since: 1.2.0

  ZCOUNT key min max
  summary: Count the members in a sorted set with scores within the given values
  since: 2.0.0

  ZDIFF numkeys key [key ...] [WITHSCORES]
  summary: Subtract multiple sorted sets
  since: 6.2.0

  ZDIFFSTORE destination numkeys key [key ...]
  summary: Subtract multiple sorted sets and store the resulting sorted set in a new key
  since: 6.2.0

  ZINCRBY key increment member
  summary: Increment the score of a member in a sorted set
  since: 1.2.0

  ZINTER numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
  summary: Intersect multiple sorted sets
  since: 6.2.0

  ZINTERCARD numkeys key [key ...] [LIMIT limit]
  summary: Intersect multiple sorted sets and return the cardinality of the result
  since: 7.0.0

  ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
  summary: Intersect multiple sorted sets and store the resulting sorted set in a new key
  since: 2.0.0

  ZLEXCOUNT key min max
  summary: Count the number of members in a sorted set between a given lexicographical range
  since: 2.8.9

  ZMPOP numkeys key [key ...] MIN|MAX [COUNT count]
  summary: Remove and return members with scores in a sorted set
  since: 7.0.0

  ZMSCORE key member [member ...]
  summary: Get the score associated with the given members in a sorted set
  since: 6.2.0

  ZPOPMAX key [count]
  summary: Remove and return members with the highest scores in a sorted set
  since: 5.0.0

  ZPOPMIN key [count]
  summary: Remove and return members with the lowest scores in a sorted set
  since: 5.0.0

  ZRANDMEMBER key [count [WITHSCORES]]
  summary: Get one or multiple random elements from a sorted set
  since: 6.2.0

  ZRANGE key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
  summary: Return a range of members in a sorted set
  since: 1.2.0

  ZRANGEBYLEX key min max [LIMIT offset count]
  summary: Return a range of members in a sorted set, by lexicographical range
  since: 2.8.9

  ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
  summary: Return a range of members in a sorted set, by score
  since: 1.0.5

  ZRANGESTORE dst src min max [BYSCORE|BYLEX] [REV] [LIMIT offset count]
  summary: Store a range of members from sorted set into another key
  since: 6.2.0

  ZRANK key member
  summary: Determine the index of a member in a sorted set
  since: 2.0.0

  ZREM key member [member ...]
  summary: Remove one or more members from a sorted set
  since: 1.2.0

  ZREMRANGEBYLEX key min max
  summary: Remove all members in a sorted set between the given lexicographical range
  since: 2.8.9

  ZREMRANGEBYRANK key start stop
  summary: Remove all members in a sorted set within the given indexes
  since: 2.0.0

  ZREMRANGEBYSCORE key min max
  summary: Remove all members in a sorted set within the given scores
  since: 1.2.0

  ZREVRANGE key start stop [WITHSCORES]
  summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
  since: 1.2.0

  ZREVRANGEBYLEX key max min [LIMIT offset count]
  summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.
  since: 2.8.9

  ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
  summary: Return a range of members in a sorted set, by score, with scores ordered from high to low
  since: 2.2.0

  ZREVRANK key member
  summary: Determine the index of a member in a sorted set, with scores ordered from high to low
  since: 2.0.0

  ZSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate sorted sets elements and associated scores
  since: 2.8.0

  ZSCORE key member
  summary: Get the score associated with the given member in a sorted set
  since: 1.2.0

  ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
  summary: Add multiple sorted sets
  since: 6.2.0

  ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
  summary: Add multiple sorted sets and store the resulting sorted set in a new key
  since: 2.0.0

ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]:将指定score值的元素添加到 sort-set 集合中,如果元素存在,则更新score值,返回添加进元素的个数

127.0.0.1:6379> zadd zk1 20 zkv1 30 zkv2
(integer) 2

ZCARD key:返回添加进sorted-set集合中元素的个数

127.0.0.1:6379> zcard zk1
(integer) 2

ZCOUNT key min max :获取 score值在 min 到max (闭合)区间内所有元素的个数,

127.0.0.1:6379> zadd zk1 20 zkv1 30 zkv2
(integer) 2
127.0.0.1:6379> zcount zk1 20 21
(integer) 1
127.0.0.1:6379> zcount zk1 0 100
(integer) 2
127.0.0.1:6379> zcount zk1 0 20
(integer) 0

ZDIFF numkeys key [key ...] [WITHSCORES]:返回给定numkeys数量集合的差集的元素,如果加上withscore 参数则会把差集元素对应的score值一起返回,如果给定集合中的元素都不相同,则会返回第一个集合对应key中的元素

127.0.0.1:6379> zadd zset1 1 one
(integer) 1
127.0.0.1:6379> zadd zset1 2 two
(integer) 1
127.0.0.1:6379> zadd zset2 2 two
(integer) 1
127.0.0.1:6379> zadd zset3 3 three
(integer) 1
127.0.0.1:6379> zadd zset4 4 four
(integer) 1
127.0.0.1:6379> zdiff 2 zset1 zset2
1) "one"
-- 给定集合中的元素都不相同,则会返回命令中第一个集合 zset1 对应key中的元素
127.0.0.1:6379> zdiff 3 zset1 zset3 zset4
1) "one"
2) "two"

ZDIFFSTORE destination numkeys key [key ...]:返回给定numkeys数量集合的差集的元素并存储在新的sorted-set集合destination中,如果给定集合中的元素都不相同,则会返回第一个集合并存储

127.0.0.1:6379> zdiffstore zset5 3 zset1 zset3 zset4
(integer) 2
127.0.0.1:6379> zrange zset5 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
127.0.0.1:6379> zrange zset5 0 -1
1) "one"
2) "two"

ZINCRBY key increment member:操作集合中给定元素自增,返回自增之后的值

127.0.0.1:6379> zincrby zset1 1 one
"2"

ZINTER numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]:返回给定 numkeys 数量集合的交集,返回的交集score 值默认是 AGGREGATE SUM ,通过WEIGHTS参数设置每个集合的权重,每个集合在参与计算时元素的分数会被乘上该集合的权重。

127.0.0.1:6379> zadd zset1 1 k1 2 k2 3 k3
(integer) 3
127.0.0.1:6379> zadd zset2 10 k1 20 k2 30 k3 40 k4
(integer) 4
---不指定 AGGREGATE 默认是sum 
127.0.0.1:6379> zinter 2 zset1 zset2 withscores
1) "k1"
2) "11"
3) "k2"
4) "22"
5) "k3"
6) "33"
-- 指定 AGGREGATE 为min
1) "k1"
2) "1"
3) "k2"
4) "2"
5) "k3"
6) "3"
-- 指定 AGGREGATE 为max
127.0.0.1:6379> zinter 2 zset1 zset2 aggregate max  withscores
1) "k1"
2) "10"
3) "k2"
4) "20"
5) "k3"
6) "30"
-- 增加权重参数
127.0.0.1:6379> zinter 2 zset1 zset2 weights 10 20  aggregate max  withscores
1) "k1"
2) "200"
3) "k2"
4) "400"
5) "k3"
6) "600"
127.0.0.1:6379> zinter 2 zset1 zset2 weights 10 20  aggregate min  withscores
1) "k1"
2) "10"
3) "k2"
4) "20"
5) "k3"
6) "30"

ZINTERCARD numkeys key [key ...] [LIMIT limit]:返回给定集合交集中元素的数量

127.0.0.1:6379> zintercard 2 zset1 zset2
(integer) 2

ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]:返回给定 numkeys 数量集合的交集并存储到新的集合destination中,destination 中score 值默认是 AGGREGATE SUM ,通过WEIGHTS参数设置每个集合的权重,每个集合在参与计算时元素的分数会被乘上该集合的权重

--- 默认 AGGREGATE 是sum
127.0.0.1:6379> zinterstore des1 2 zset1 zset2
(integer) 3
127.0.0.1:6379> zrange des1 0 -1
1) "k1"
2) "k2"
3) "k3"
127.0.0.1:6379> zrange des1 0 -1 withscores
1) "k1"
2) "11"
3) "k2"
4) "22"
5) "k3"
6) "33"
---- 指定 AGGREGATE 为max
127.0.0.1:6379> zrange des2 0 -1 withscores
1) "k1"
2) "10"
3) "k2"
4) "20"
5) "k3"
6) "30"

--增加权重参数
127.0.0.1:6379> zinterstore des3 2 zset1 zset2 weights 10 20 aggregate max
(integer) 3
127.0.0.1:6379> zrange des3 0 -1
1) "k1"
2) "k2"
3) "k3"
127.0.0.1:6379> zrange des3 0 -1 withscores
1) "k1"
2) "200"
3) "k2"
4) "400"
5) "k3"
6) "600"

ZLEXCOUNT key min max :返回指定范围内元素的个数

127.0.0.1:6379> ZLEXCOUNT zset1 - +
(integer) 3
127.0.0.1:6379> zrange zset1 0 -1
1) "k1"
2) "k2"
3) "k3"
127.0.0.1:6379> zlexcount zset1 [k1 [k3
(integer) 3

ZMPOP numkeys key [key ...] MIN|MAX [COUNT count]:从提供的键名列表中的第一个非空排序集中弹出一个或多个成员-分数对元素,numkeys是命令中集合的个数

127.0.0.1:6379> zadd zset1 1 k1 2 k2 3 k3 4 k4
(integer) 4
127.0.0.1:6379> zadd zset2 10 k10 20 k20 30 k30
(integer) 3
-- 返回两个集合中最小的元素及其对应的score值
127.0.0.1:6379> zmpop 2 zset1 zset2 min count 2
1) "zset1"
2) 1) 1) "k1"
      2) "1"
   2) 1) "k2"
      2) "2"
127.0.0.1:6379> zrange zset1 0 -1
1) "k3"
2) "k4"

127.0.0.1:6379> zmpop 1 zset1  min count 2
1) "zset1"
2) 1) 1) "k3"
      2) "3"
   2) 1) "k4"
      2) "4"
--- 当第一个集合为空的时候,这时弹出的就是第二个集合的元素
127.0.0.1:6379> zrange zset1 0 -1
(empty array)
127.0.0.1:6379> zmpop 2 zset1 zset2  max count 3
1) "zset2"
2) 1) 1) "k30"
      2) "30"
   2) 1) "k20"
      2) "20"
   3) 1) "k10"
      2) "10"

ZMSCORE key member [member ...]:获取给定集合中多个元素的score值

127.0.0.1:6379> zadd zset1 1 k1 2 k2 3 k3 4 k4
(integer) 4
127.0.0.1:6379> zmscore zset1 k1 k2 k3
1) "1"
2) "2"
3) "3"

ZPOPMAX key [count] :移除给定集合中score值最大的元素

127.0.0.1:6379> zpopmax zset1 
1) "k4"
2) "4"

ZPOPMIN key [count]:移除给定集合中score值最小的元素

127.0.0.1:6379> zpopmin zset1
1) "k1"
2) "1"

ZRANDMEMBER key [count [WITHSCORES]] :随机返回集合中的一个元素

127.0.0.1:6379> ZRANDMEMBER zset1
"k2"

ZRANGE key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]:按照元素score值从小到大排序,返回索引从 min 到max 之间所有的元素,在不加[BYSCORE|BYLEX]、[LIMIT offset count]参数下 如果min 是0,max是-1 则是返回所有的元素;若要加上[LIMIT offset count] 参数,必须是和[BYSCORE|BYLEX]参数一起使用的

127.0.0.1:6379> zadd zset1 1 k1 2 k2 3 k3 4 k4
(integer) 2
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "k1"
2) "1"
3) "k2"
4) "2"
5) "k3"
6) "3"
7) "k4"
8) "4"

--返回 1<score<=3的元素
127.0.0.1:6379> zrange zset1 (1 3 byscore withscores
1) "k2"
2) "2"
3) "k3"
4) "3"
-- 返回 2<score<4的元素
127.0.0.1:6379> zrange zset1 (2 (4 byscore withscores
1) "k3"
2) "3"
-- 返回 1<=score<=3的元素
127.0.0.1:6379> zrange zset1 1 3 byscore
1) "k1"
2) "k2"
3) "k3"
-- 如果加了rev 参数则,min 和 max 参数位置需调换
127.0.0.1:6379> zrange zset1 1 3 rev byscore
(empty array)
127.0.0.1:6379> zrange zset1 3 1 rev byscore
1) "k3"
2) "k2"
3) "k1"
-- limit 参数用法
-- 返回zset1集合所有元素中 下标从1开始的两个元素
127.0.0.1:6379> zrange zset1 - + bylex limit 1 2
1) "k2"
2) "k3"
--- 返回 zset1 集合中 2<=score<=4 的元素中下标从2 开始的一个元素(可以理解为返回zset1集合中2<=score<=4的元素作为新的集合,并返回新集合中下标从2开始的一个元素)
127.0.0.1:6379> zrange zset1 2 4 byscore limit 2 1
1) "k4"

ZRANGEBYLEX key min max [LIMIT offset count]:以相同的score值插入已排序集中的所有元素时,为了强制按字典顺序排序,此命令将删除存储在键处的已排序集中的所有元素,这些元素位于由min和max指定的字典范围之间

127.0.0.1:6379> zdd myzset 0 aaaa 0 b 0 c 0 d 0 e
(integer) 5
127.0.0.1:6379> zadd myzset 0 foo 0 zap 0 zip 0 ALPHA 0 alpha
(integer) 5
127.0.0.1:6379> zrange myzset 0 -1
 1) "ALPHA"
 2) "aaaa"
 3) "alpha"
 4) "b"
 5) "c"
 6) "d"
 7) "e"
 8) "foo"
 9) "zap"
10) "zip"
127.0.0.1:6379> zrangebylex myzset [alpha [omega
(integer) 6
127.0.0.1:6379> zrange myzset 0 -1
1) "ALPHA"
2) "aaaa"
3) "zap"
4) "zip"

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]:按照score值从小到大返回集合中的元素

127.0.0.1:6379> zadd zset2 3 k3 1 k1 2 k2
(integer) 3
-- 返回所有的元素
127.0.0.1:6379> zrangebyscore zset2 -inf +inf
1) "k1"
2) "k2"
3) "k3"
-- 返回前两个
127.0.0.1:6379> zrangebyscore zset2 0 2
1) "k1"
2) "k2"

ZRANGESTORE dst src min max [BYSCORE|BYLEX] [REV] [LIMIT offset count]:把zrange 返回的元素存储到一个新的集合中去,min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] 参数用法参考zrange

127.0.0.1:6379> zrangestore zset3 zset1 0 2
(integer) 3
127.0.0.1:6379> zrange zset3 0 -1
1) "k1"
2) "k2"
3) "k3"

ZRANK key member:返回集合中指定元素所在的下标位置

127.0.0.1:6379> zrange zset3 0 -1
1) "k1"
2) "k2"
3) "k3"
127.0.0.1:6379> zrank zset3 k2
(integer) 1

ZREM key member [member ...]

127.0.0.1:6379> zrange zset3 0 -1
1) "k1"
2) "k2"
3) "k3"
127.0.0.1:6379> zrem zset3 k1
(integer) 1
127.0.0.1:6379> zrange zset3 0 -1
1) "k2"
2) "k3"

ZREMRANGEBYLEX key min max:当以相同的score值插入已排序集中的所有元素时,为了强制按字典顺序排序,此命令将删除存储在键处的已排序集中的所有元素,这些元素位于由min和max指定的字典范围之间。min和max的含义与ZRANGEBYLEX命令相同。类似地,如果使用相同的min和max参数调用ZRANGEBYLEX,该命令实际上会删除返回的相同元素。

127.0.0.1:6379> zadd myzset 0 aaaa 0 b 0 c 0 d 0 e
(integer) 5
127.0.0.1:6379>  zadd myzset 0 foo 0 zap 0 zip 0 ALPHA 0 alpha
(integer) 5
127.0.0.1:6379> zrange myzset 0 -1
 1) "ALPHA"
 2) "aaaa"
 3) "alpha"
 4) "b"
 5) "c"
 6) "d"
 7) "e"
 8) "foo"
 9) "zap"
10) "zip"
127.0.0.1:6379> zremrangebylex myzset [alpha [omega
(integer) 6
127.0.0.1:6379> zrange myzset 0 -1
1) "ALPHA"
2) "aaaa"
3) "zap"
4) "zip"

ZREMRANGEBYRANK key start stop:删除集合中start和stop之间范围的元素。start和stop都是基于0的索引,其中0是得分最低的元素。这些索引可以是负数,它们表示从得分最高的元素开始的偏移量。例如:-1是得分最高的元素,-2是得分第二高的元素,以此类推。

127.0.0.1:6379> zrange zset1 0 -1
1) "k1"
2) "k2"
3) "k3"
4) "k4"
127.0.0.1:6379> zremrangebyrank zset1 0 1
(integer) 2
127.0.0.1:6379> zrange zset1 0 -1
1) "k3"
2) "k4"

ZREMRANGEBYSCORE key min max:删除集合中score值在min和max之间的所有元素(包括)。

127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "k3"
2) "3"
3) "k4"
4) "4"
127.0.0.1:6379> zremrangebyscore zset1 0 3
(integer) 1
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "k4"
2) "4"

ZREVRANGE key start stop [WITHSCORES]:和zrange 加上 rev参数的效果一样,返回集合中的已排序集合中指定范围的元素。元素被认为是按从最高到最低的score值排序的。score值相等的元素按字典顺序降序排列。

127.0.0.1:6379> zadd zset2 0 k1 2 k2 3 k3 4 k4 5 k5
(integer) 5
127.0.0.1:6379> zrevrange zset2 0 -1 withscores
 1) "k30"
 2) "30"
 3) "k20"
 4) "20"
 5) "k10"
 6) "10"
 7) "k5"
 8) "5"
 9) "k4"
10) "4"
11) "k3"
12) "3"
13) "k2"
14) "2"
15) "k1"
16) "0"

ZREVRANGEBYLEX key max min [LIMIT offset count]:返回已排序集合中指定范围的元素。元素被认为是按从最高到最低的分数排序的。分数相等的元素按字典顺序降序排列。除了顺序颠倒之外,ZREVRANGE与ZRANGE相似。

127.0.0.1:6379> zrevrange zset2 0 -1 withscores
 1) "k30"
 2) "30"
 3) "k20"
 4) "20"
 5) "k10"
 6) "10"
 7) "k5"
 8) "5"
 9) "k4"
10) "4"
11) "k3"
12) "3"
13) "k2"
14) "2"
15) "k1"
16) "0"
127.0.0.1:6379> zrevrangebylex zset2 + - limit 2 3
1) "k10"
2) "k5"
3) "k4"

ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]:返回指定的score值范围内的元素

127.0.0.1:6379> zadd zset1 1 k1 3 k2 11 k3 23 k4 8 k5
(integer) 5
127.0.0.1:6379> zrange zset1 0 -1
1) "k1"
2) "k2"
3) "k5"
4) "k3"
5) "k4"
127.0.0.1:6379> zrevrangebyscore zset1 20 4 limit  1 1
1) "k5"

ZREVRANK key member:返回反转后的集合中指定元素的下标位置

127.0.0.1:6379> zrange zset1 0 -1
1) "k1"
2) "k2"
3) "k3"
4) "k4"
127.0.0.1:6379> zrank zset1 k1
(integer) 0
127.0.0.1:6379> zrevrank zset1 k1
(integer) 3

ZSCAN key cursor [MATCH pattern] [COUNT count]:#### 迭代集合中的元素

127.0.0.1:6379> zscan zset1 0 match k1
1) "0"
2) 1) "k1"
   2) "1"

ZSCORE key member:获取集合中某一个元素对应的score值

127.0.0.1:6379> zscore zk1 zkv1
"21"

ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]:返回给定集合的并集

127.0.0.1:6379> zadd zset1 1 one 2 two
(integer) 2
127.0.0.1:6379> zadd zset2 1 one 2 two 3 three 
(integer) 3
127.0.0.1:6379> zunion 2 zset1 zset2
1) "one"
2) "three"
3) "two"

ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]:返回给定集合的并集并存储到新的集合 destination 中去

127.0.0.1:6379> zunionstore zset3 2 zset1 zset2
(integer) 3
127.0.0.1:6379> zrange zset3 0 -1
1) "one"
2) "three"
3) "two"

应用场景

思路:定义商品销售排行榜(sorted set集合),key为goods:sellsort,分数为商品销售数量

-- 记录8中商品的销量
127.0.0.1:6379> zadd goods:sellsort 9 gid_001 20 gid_002 11 gid_003 34 gid_004 2 gid_005 25 gid_006 45 gid_007 4 gid_008 
(integer) 8

-- 获取排名前5销量的商品
127.0.0.1:6379> zrange goods:sellsort 0 5
1) "gid_005"
2) "gid_008"
3) "gid_001"
4) "gid_003"
5) "gid_002"