Data type; ordered collection types (sorted set)

Ordered set sortedset

A feature of

1, orderly, deduplication
2, is a string type element
3, each element is associated with a floating-point score (Score), and small (may be the same value) to a large set of elements arranged in order according to the branch
4, a maximum of 2 ^ 32-1 element comprising

Example 1: A holds an ordered collection of fruit prices

Scores 2.0 4.0 6.0 8.0
element watermelon grape Mango banana

Example 2: A Save the ordered collection staff salaries

Scores 6000 8000 10000 12000
element lucy tom jim jack

Example 3: A Save the number of certain technologies are reading a book

Scores 300 400 500 600
element Core programming database django repeat

Two Command Summary

# Add a member in an ordered set 
    Zadd Key Score Member
 # View the specified range of elements (in ascending order) 
    Z Range The Key Start STOP [withscores]
 # View the specified range of elements (in descending order) 
    ZREVRANGE Key Start STOP [withscores]
 # view the scores of the specified element 
    Key member ZSCORE
 # returns the specified range of elements 
# offset: the number of elements skipped 
# COUNT: returns the number 
# parentheses: Fruits open interval zrangebyscore (2.0 8.0 
    zrangebyscore Key min max [withscores] [limit offset COUNT]
 # remove members 
    zrem key member
 # increase or decrease the value 
    zincrby key increment member 
                   value members 
# returns the element ranking
    Member Key zrank
 # Returns the element reverse ranking 
    zrevrank Member Key
 # delete elements in the specified range 
    zremrangebyscore min max Key
 # returns the number of elements in the set 
    zcard Key
 # Returns the number of elements in the specified range 
    zcount Key min max 
    zcount Fruits . 4. 7  
    zcount Fruits ( . 4. 7. 4 <Fruits <=. 7 # '(' is an open interval does not include 
                  (. 4 (. 7. 4 <Fruits <. 7 # and set 
zunionstore destination numkeys key [weights weight value] [aGGREGATE to the SUM | MIN | MAX]
 # few sets numkeys is also a few weight value corresponding to several 
# intersection: union and similar, just take the same elements 
ZINTERSTORE destination numkeys key1 key2 wEIGHTS weight AGGREGATE SUM | MIN | MAX

Three operational commands demo

1. See: interval specified index element (ascending)

  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. View: specified index range of elements (in descending order)

 

  ZREVRANGE key start stop [withscores]

3. Display the score of the specified element

  ZSCORE key member

127.0.0.1:6379> zscore salary jack
"14000"

4. Return the specified range of elements

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

  offset: Skip the number of elements

  count: return several

  Parentheses: open interval 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. Delete

  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. increased or decreased value

  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. Return Ranking (index) elements

  zrank key member

127.0.0.1:6379> zrank salary jack
(integer) 2

8. Return the element reverse ranking

  zrevrank key member

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

9. Remove the elements within the specified range

  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. Return the number of elements in a set of

  zcard key

127.0.0.1:6379> ZCARD salary
(integer) 2

11. Return the number of elements in the specified range

  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. union

  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 and AGGREGATE to 
127.0.0.1:6379> Z Range The stu_score1 0 -1 withscores
 . 1) " Tom " 
2) " 60 " 
. 3) " Jim " 
. 4) " 70 " 
127.0.0.1:6379> Z Range The stu_score2 0 -1 withscores
 . 1) " Tom " 
2) " 80 " 
. 3) " Lucy " 
. 4) " 90 " 
# weights 1 to stu_score1, weighting 0.5 to stu_score2, summing the weights calculated after completing the 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. Intersection

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

  And the like, and set, taking just like elements

Operating sorted set python

Import Redis 

R & lt = redis.Redis (= Host ' 192.168.43.49 ' , Port = 6379, password = ' 123456 ' , DB = 0)
 # Note that the second parameter is a dictionary 
r.zadd ( ' the salary ' , { ' Tom ' : 6000, ' Jim ' : 8000, ' Jack ' : 12000 })
 # result is stored in the list of tuples [(), (), ()] 
Print (r.zrange ( ' the salary ' , 0, -1, withscores = True))
 Print (r.zrevrange ( ' the salary ', 0, -1, withscores = True))
 # Start: start value, num: number display bar 
Print (r.zrangebyscore ( ' the salary ' , 6000,12000, Start =. 1, NUM = 2, withscores = True))
 # delete 
r.zrem ( ' the salary ' , ' Tom ' )
 Print (r.zrange ( ' the salary ' , 0, -1, withscores = True))
 # increasing value 
r.zincrby ( ' the salary ' , 5000, ' Jack ' )
 Print (r.zrange ( ' the salary ', 0, -1, withscores = True))
 # Returns the element Ranking 
Print (r.zrank ( ' the salary ' , ' Jack ' ))
 Print (r.zrevrank ( ' the salary ' , ' Jack ' ))
 # delete a specified interval element 
r.zremrangebyscore ( ' the salary ' , 6000,8000 )
 Print (r.zrange ( ' the salary ' , 0, -1, withscores = True))
 # statistical number of elements 
Print (r.zcard ( ' the salary ' ))
 #Returns the number of elements in the specified range 
Print (r.zcount ( ' the salary ' , 6000,20000 ))
 # union 
r.zadd ( ' salary2 ' , { ' Jack ' : 17000, ' Lucy ' : 8000 }) 
r.zunionstore ( ' salary3 ' , ( ' the 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))

 

Guess you like

Origin www.cnblogs.com/maplethefox/p/11309487.html