Preface introduces
, in a lot of friends all say with me, redis ZSet (ordered set) is their most strange collection, is also one feel particularly complex set at the same time, in the development process often use it, but everybody was not too sure about the use of collection, so the author to ZSet set from the beginning to explain the use of a collection of redis guide, I hope I can help you!
An ordered set
The concept of ZSET
Ordered sets (zsets) : Can hold many things like sets, except that sets hold strings and ordered sets (zsets) hold key-value pairs. In a more rigorous sense, ordered sets are closer to hashes. Except ordered sets are ordered, kind of like TreeSet.
Definition of ZSET
Ordered set (ZSet) : It is a key-value pair that stores the object value of member itself and its corresponding score, and automatically sorts according to the score value from smallest to largest. Corresponding to specific data structures,
- The keys of an ordered set are called members and each member is different
- The value of an ordered set is called a score, and the score must be a floating point number
ZADD command:
-
Its full name is zset Add, and it adds members of a given score to an ordered collection
-
Because ordered collections are self-sorting, they don’t have left or right inserts like lists do
Instructions:
# zadd key score value
zadd zset-weight 60 Kelvin
Copy the code
Java code
System.out.println(conn.zadd("zset-weight".60."Kelvin"));
Copy the code
role
Add the Kelvin-60 key-value pair to the ordered set zset-weight
return
- 1 is saved successfully
- Failed to save ‘0’, zset-weight already exists
Pay attention to
The difference with ordered sets is that the score comes first, because ordered sets are sorted by score, from small to large by default (positive order ASC).
Other cases
zadd zset-weight 40 Lina
zadd zset-weight 50 Oscar
zadd zset-weight 70 Mike
Copy the code
Now that we’ve done the storage, how do we delete the elements?
ZREM (removed)
ZREM, zset remove, removes a given member from an ordered set and returns the number of removed members
instruction
zrem zset-weight Mike
Copy the code
- Removes the element whose key is Mike from zset-weight
- Return: “1” means the number of removed elements is 1
System.out.println(conn.zrem("zset-weight","Mike"));
Copy the code
ZCARD
ZCARD, zset card, returns the number of members in an ordered set
instruction
zcard high
Copy the code
Java code
System.out.println(conn.zadd("high",180,"Kelvin"));
System.out.println(conn.zadd("high",160,"Lina"));
System.out.println(conn.zadd("high",177,"Mike"));
System.out.println(conn.zcard("high"));
Copy the code
The zcard command returns 3.
ZINCRBY
ZINCRBY zset increase by: increases the score of a member by a given number.
System.out.println(conn.zincrby("high".2."Kelvin"));
Copy the code
ZCOUNT
ZCOUNT zset count returns the number of members whose score is between [min, Max]
System.out.println(conn.zcount("high".165.190));
Copy the code
ZRANK
ZRANK (zset rank) returns the rank of a member in an ordered set
System.out.println(conn.zrank("high"."Kelvin"));
Copy the code
ZSCORE
ZSCORE (full name: zset Score) returns a member’s score
System.out.println(conn.zscore("high"."Kelvin"));
Copy the code
ZRANGE (get)
-
ZRANGE full name: Zset range key start end [WITHSCORES] zset range key start end [WITHSCORES] zset range key start end [WITHSCORES] The command returns the member’s score as well.
-
If you want to pull it all out, you can use start=0, end=-1, and you can see that it’s a loop, open at the front and closed at the back. If start is set to 1, the first data will not be retrieved, and one less data will be retrieved. End =-1 indicates that the last one was retrieved
Instructions:
zrange key start end withscores
zrange zset-weight 0 -1 withscores
Copy the code
return
(1)"Lina"
(2)"40"
(3)"Oscar"
(4)"50"
(5)"Kelvin"
(6)"60"
(7)"Mike"
(8)"70"
Copy the code
We now select the data ranked between 3 and 6, (3,6)
System.out.println(conn.zrange("high".3.6));
Copy the code
Because the zrange is open on the left and closed on the right, so if we want to get (3,6) and get the score of these.
System. The out. Println (conn. ZrangeWithScores (" high ", 3, 6));Copy the code
ZRANGEBYSCORE
The full name of ZRANGEBYSCORE is Zset range by Score. Compared with Zrange, ZRANGEBYSCORE is obtained according to the range of score and returns all members in the ordered set whose score is between min and Max
Demand scenarios
We pick out the members with a score between 160 and 180
instruction
zrangebyscore key start end [withscores]
zrangebyscore zset-weight 160 180 withscores
Copy the code
Java code
System.out.println(conn.zrangeByScore("high".160.180));
Copy the code
role
Retrieves key-value pairs between [0,60]
return
(1)"Lina"
(2)"40"
(3)"Oscar"
(4)"50"
(5)"Kelvin"
(6)"60"
Copy the code
ZREVRANGE
ZREVRANGE, full name for Zset Reverse Range, returns the members of an ordered set within a given ranking range, arranged in order of score from highest to lowest
instruction
zrevrange key start end withscores
zrevrange zset-weight 0 -1 withscores
Copy the code
Java code
System.out.println(conn.zrevrange("high".1.5));
Copy the code
ZREVRANGEBYSCORE
ZREVRANGEBYSCORE full name,
zset reversal range by score
, gets all the members of the ordered set whose scores are between min and Max, and returns them in the order of their scores from highest to lowest.
instruction
Get the positions from 160 to 180 in reverse order. Note that 180 is the start coordinate and 160 is the end coordinate
zrevrangebyscore key start end
zrevrangebyscore high 180 160
Copy the code
Java code
System.out.println(conn.zrevrangeByScore("high".180.160));
Copy the code
ZREVRANK
ZREVRANK, which is also called Zset Reversal Rank, returns the ranking of the members in the ordered set from largest to smallest, with the default value being from smallest to largest.
Suppose, “Kelvin” is the sixth and “Yellow” is the third, but from the reverse view, “Kelvin” is 0 and “Yellow” is 3, so let’s write a line of code to check.
System.out.println(conn.zrevrank("high"."Yellow"));
System.out.println(conn.zrevrank("high"."Kelvin"));
Copy the code
ZREMRANGEBYRANK
ZREMRANGEBYRANK zset remove range by rank removes all members of an ordered set between start and stop
Take a look at the data in Redis before removing
Running Java code
System. The out. Println (conn. ZremrangeByRank (" high ", 2, 4));Copy the code
Returns the number removed
ZREMRANGENYSCORE
ZREMRANGEBYSCORE Zset remove range by score Removes all members of an ordered set whose ranking score is between start and stop
For demonstration purposes, we will add a few more rows of data to the high ordered collection
System.out.println(conn.zadd("high".174."Jay"));
System.out.println(conn.zadd("high".169."Pei"));
System.out.println(conn.zadd("high".186."Jone"));
Copy the code
Then remove any member with a score of 170 or less
System.out.println(conn.zremrangeByScore("high".0.170));
Copy the code
ZINTERSTORE
The full name of ZINTERSTORE is Zset Inter Store. When performing intersection operation on a given ordered set, the default aggregation function used is sum, that is, by default, the points corresponding to the same members in different sets are added and aggregated into a new ordered set.
ZUNIONSTORE
The full name of ZUNIONSTORE is Zset Union Store. It performs union operation on a given ordered set. Similarly, the aggregation function of union operation can also choose “Max”,”min” and “sum”. In order to avoid repetition, we chose Max to do a union demonstration.