数据类型;有序集合类型(sorted set)

有序集合sortedset

一特点

1、有序、去重
2、元素是字符串类型
3、每个元素都关联着一个浮点数分值(score),并按照分支从小到大的顺序排列集合中的元素(分值可以相同)
4、最多包含2^32-1元素

示例1:一个保存了水果价格的有序集合

分值 2.0 4.0 6.0 8.0
元素 西瓜 葡萄 芒果 香蕉

示例2:一个保存了员工薪水的有序集合

分值 6000 8000 10000 12000
元素 lucy tom jim jack

示例3:一个保存了正在阅读某些技术书的人数

分值 300 400 500 600
元素 核心编程 数据库 django redis

二命令汇总

# 在有序集合中添加一个成员
    zadd key score member
# 查看指定区间元素(升序)
    zrange key start stop [withscores]
# 查看指定区间元素(降序)
    ZREVRANGE key start stop [withscores]
# 查看指定元素的分值
    ZSCORE key member
# 返回指定区间元素
# offset : 跳过多少个元素
# count : 返回几个
# 小括号 : 开区间  zrangebyscore fruits (2.0 8.0
    zrangebyscore key min max [withscores]     [limit offset count]
# 删除成员
    zrem key member
# 增加或者减少分值
    zincrby key increment member
                   值      成员
# 返回元素排名
    zrank key member
# 返回元素逆序排名
    zrevrank key member
# 删除指定区间内的元素
    zremrangebyscore key min max
# 返回集合中元素个数
    zcard key
# 返回指定范围中元素的个数
    zcount key min max
    zcount fruits 4 7 
    zcount fruits (4 7   4<fruits<=7#'('是开区间不包括
                  (4 (7  4<fruits<7
# 并集
zunionstore destination numkeys key [weights 权重值] [AGGREGATE SUM|MIN|MAX]
# 有几个集合numkeys就是几  权重值也要相应的给几个
# 交集:和并集类似,只取相同的元素
ZINTERSTORE destination numkeys key1 key2 WEIGHTS weight AGGREGATE SUM|MIN|MAX

操作命令演示

1.查看: 指定索引区间元素(升序)

  zrange key start stop [withscores]

127.0.0.1:6379> ZRANGE salary 0 -1
1) "lucy"
2) "tom"
3) "jim"
4) "jack"
127.0.0.1:6379> ZRANGE salary 0 -1 withscores
1) "lucy"
2) "6000"
3) "tom"
4) "8000"
5) "jim"
6) "10000"
7) "jack"
8) "12000"

2.查看: 指定索引区间元素(降序)

 

  ZREVRANGE key start stop [withscores]

3.显示指定元素的分值

  ZSCORE key member

127.0.0.1:6379> zscore salary jack
"14000"

4.返回指定区间元素

  zrangebyscore key min max [withscores] [limit offset count]

  offset : 跳过多少个元素

  count : 返回几个

  小括号 : 开区间 zrangebyscore fruits (2.0 8.0

127.0.0.1:6379> ZRANGEBYSCORE salary (8000 12000
1) "jim"
2) "jack"
127.0.0.1:6379> ZRANGE salary 0 -1 withscores
1) "lucy"
2) "6000"
3) "tom"
4) "8000"
5) "jim"
6) "10000"
7) "jack"
8) "12000"

5.删除

  zrem key member

127.0.0.1:6379> ZREM salary jim
(integer) 1
127.0.0.1:6379> ZRANGE salary 0 -1 withscores
1) "lucy"
2) "6000"
3) "tom"
4) "8000"
5) "jack"
6) "12000"

6.增加或者减少分值

  zincrby key increment member

127.0.0.1:6379> ZINCRBY salary 2000 jack
"14000"
127.0.0.1:6379> ZRANGE salary 0 -1 withscores
1) "lucy"
2) "6000"
3) "tom"
4) "8000"
5) "jack"
6) "14000"

7.返回元素的排名(索引)

  zrank key member

127.0.0.1:6379> zrank salary jack
(integer) 2

8.返回元素逆序排名

  zrevrank key member

127.0.0.1:6379> ZREVRANK salary jack
(integer) 0
127.0.0.1:6379> ZREVRANK salary lucy
(integer) 2

9.删除指定区间内的元素

  zremrangebyscore key min max

127.0.0.1:6379> ZREMRANGEBYSCORE salary 4000 6000
(integer) 1
127.0.0.1:6379> ZRANGE salary 0 -1 withscores
1) "tom"
2) "8000"
3) "jack"
4) "14000"

10.返回集合中元素个数

  zcard key

127.0.0.1:6379> ZCARD salary
(integer) 2

11.返回指定范围中元素的个数

  zcount key min max

  zcount fruits 4 7

  zcount fruits (4 7

127.0.0.1:6379> ZRANGE salary 0 -1 withscores
1) "tom"
2) "8000"
3) "jack"
4) "14000"
127.0.0.1:6379> zcount salary 8000 14000
(integer) 2
# 不包含8000,包含14000
127.0.0.1:6379> zcount salary (8000 14000
(integer) 1

12.并集

  zunionstore destination numkeys key [weights ] [AGGREGATE SUM|MIN|MAX]

127.0.0.1:6379> zadd stu_score1 60 tom 70 jim
(integer) 2
127.0.0.1:6379> zadd stu_score2 80 tom 90 lucy
(integer) 2
# 默认为SUM
127.0.0.1:6379> ZUNIONSTORE stu_score3 2 stu_score1 stu_score2
(integer) 3
127.0.0.1:6379> ZRANGE stu_score3 0 -1 withscores
1) "jim"
2) "70"
3) "lucy"
4) "90"
5) "tom"
6) "140"
127.0.0.1:6379> 
# WEIGHTS 和 AGGREGATE 
127.0.0.1:6379> ZRANGE stu_score1 0 -1 withscores
1) "tom"
2) "60"
3) "jim"
4) "70"
127.0.0.1:6379> ZRANGE stu_score2 0 -1 withscores
1) "tom"
2) "80"
3) "lucy"
4) "90"
# 权重1给stu_score1,权重0.5给stu_score2,算完权重之后求和SUM
127.0.0.1:6379> ZUNIONSTORE stu_score8 2 stu_score1 stu_score2 weights 1 0.5 AGGREGATE SUM
(integer) 3
127.0.0.1:6379> ZRANGE stu_score8 0 -1 withscores
1) "lucy"
2) "45"
3) "jim"
4) "70"
5) "tom"
6) "100"
127.0.0.1:6379> 

12.交集

  ZINTERSTORE destination numkeys key1 key2 WEIGHTS weight AGGREGATE SUM|MIN|MAX

  和并集类似,只取相同的元素

python操作sorted set

import redis

r = redis.Redis(host='192.168.43.49',port=6379,password='123456',db=0)
# 注意第二个参数为字典
r.zadd('salary',{'tom':6000,'jim':8000,'jack':12000})
# 结果为列表中存放元组[(),(),()]
print(r.zrange('salary',0,-1,withscores=True))
print(r.zrevrange('salary',0,-1,withscores=True))
# start:起始值,num:显示条数
print(r.zrangebyscore('salary',6000,12000,start=1,num=2,withscores=True))
# 删除
r.zrem('salary','tom')
print(r.zrange('salary',0,-1,withscores=True))
# 增加分值
r.zincrby('salary',5000,'jack')
print(r.zrange('salary',0,-1,withscores=True))
# 返回元素排名
print(r.zrank('salary','jack'))
print(r.zrevrank('salary','jack'))
# 删除指定区间内的元素
r.zremrangebyscore('salary',6000,8000)
print(r.zrange('salary',0,-1,withscores=True))
# 统计元素个数
print(r.zcard('salary'))
# 返回指定范围内元素个数
print(r.zcount('salary',6000,20000))
# 并集
r.zadd('salary2',{'jack':17000,'lucy':8000})
r.zunionstore('salary3',('salary','salary2'),aggregate='max')
print(r.zrange('salary3',0,-1,withscores=True))
# 交集
r.zinterstore('salary4',('salary','salary2'),aggregate='max')
print(r.zrange('salary4',0,-1,withscores=True))

猜你喜欢

转载自www.cnblogs.com/maplethefox/p/11309487.html