Redis (zadd) uses Java API for simple ranking

Source: Internet
Author: User
Tags object serialization
Redis

Zadd key score member [[score member] [score member]...]

Set one or moreMemberElements andScoreValues are added to the sorted set.Key.

IfMemberIf it is already a member of the sorted set, update thisMemberOfScoreValue, and re-insert thisMemberTo ensureMemberIn the correct position.

ScoreThe value can be an integer or a double-precision floating point number.

IfKeyOtherwise, an empty ordered set is created and the zadd operation is executed.

WhenKeyAn error is returned when an ordered set exists but is not of the type.

For more information about Ordered Sets, see sorted set.

Available version:
> = 1.2.0
Time Complexity:
O (M * log (n )),
NIs the base of an ordered set,
MThe number of new members that are successfully added.
Return Value:

The number of new members successfully added, excluding updated and existing members.

 

# Add a single element redis> zadd page_rank 10 Google.com (integer) 1 # add multiple elements redis> zadd page_rank 9 Baidu.com 8 bing.com (integer) 2 redis> zrange page_rank 0-1 withscores1) "bing.com" 2) "8" 3) "Baidu.com" 4) "9" 5) "Google.com" 6) "10" # Add an existing element, and the score value remains unchanged redis> zadd page_rank 10 Google.com (integer) 0 redis> zrange page_rank 0-1 withscores # Not changed 1) "bing.com" 2) "8" 3) "Baidu.com" 4) "9" 5) "Google.com" 6) "10" # Add an existing element, but change the score value to redis> zadd page_rank 6 bing.com (integer) 0 redis> score of zrange page_rank 0-1 withscores # bing.com element is changed. 1) "bing.com" 2) "6" 3) "Baidu.com" 4) "9" 5) "Google.com" 6) "10"

 

 

Zrevrange key start stop [withscores]

Returns an ordered set.Key.

The position of the Member is
ScoreValues are arranged in descending order (from large to small. Same
ScoreThe value members are in the lexicographic descending order (
Reverse lexicographical order.

ExceptScoreIn addition, other aspects of the zrevrange command andZrangeCommand.

Available version:
> = 1.2.0
Time Complexity:
O (log (n) + M ),
NIs the base of the sorted set, and
MThe base number of the result set.
Return Value:
Within the specified range,
ScoreValue (optional) List of sequential integrators.

 

Redis> zrange salary 0-1 withscores # Ascending Order 1) "Peter" 2) "3500" 3) "Tom" 4) "4000" 5) "Jack" 6) "5000" redis> zrevrange salary 0-1 withscores # descending order 1) "Jack" 2) "5000" 3) "Tom" 4) "4000" 5) "Peter" 6) "3500"

 

 


API practice code (adding and reading ranking ):

User (User class ):

 

Package COM. duobei. model; import Java. io. serializable; public class user implements serializable {Private Static final long serialversionuid = 1l; private string ID; // No. Private string name; // name private Double score; // score private int rank; // rank public user () {} public user (string ID, string name, Double score) {This. id = ID; this. name = Name; this. score = score;} Public String GETID () {return ID;} public void setid (string ID) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public double getscore () {return score;} public void setscore (Double score) {This. score = score;} public int getrank () {return rank;} public void setrank (INT rank) {This. rank = rank ;}@ overridepublic string tostring () {return "User [ID =" + ID + ", name =" + name + ", score =" + score + ", rank = "+ rank +"] ";}}

Custom serialization encapsulation class:

 

 

Package COM. duobei. tools; import Java. io. bytearrayinputstream; import Java. io. bytearrayoutputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream; public class objectser {/*** Object serialization * @ Param OBJ * @ return */public static byte [] objecttobyte (Object OBJ) {byte [] bytes = NULL; try {bytearrayoutputstream BO = new bytearrayoutputstream (); objectoutputstream oo = new objectoutputstream (BO); oo. writeobject (OBJ); bytes = bo. tobytearray (); Bo. close (); oo. close ();} catch (exception e) {e. printstacktrace ();} return bytes;}/*** deserialization * @ Param bytes * @ return */public static object bytetoobject (byte [] bytes) {object = NULL; try {bytearrayinputstream BAIS = new bytearrayinputstream (bytes); objectinputstream OIS = new objectinputstream (BAIS); object = Ois. readobject ();} catch (exception e) {e. printstacktrace ();} return object ;}}

Test class (read after adding ):

 

 

Package COM. duobei. test; import Java. util. arraylist; import Java. util. iterator; import Java. util. list; import Java. util. set; import Org. JUnit. before; import Org. JUnit. ignore; import Org. JUnit. test; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; import redis. clients. jedis. shardedjedis; import redis. clients. jedis. shardedjedispool; import COM. duobei. model. user; import COM. duobei. tools. objectser; public class rankingtest {private applicationcontext context; private shardedjedispool; private shardedjedis Jedis; Public rankingtest () {}@ beforepublic void Init () throws exception {string config [] = {"applicationcontext. XML "}; Context = new classpathxmlapplicationcontext (config); shardedjedispool = (shardedjedispool) context. getbean ("shardedjedispool"); Jedis = (shardedjedis) shardedjedispool. getresource () ;}@ test @ ignorepublic void rankadd () {user user1 = new user ("12345", "Chang Shaopeng", 99.9 ); user user2 = new user ("12346", "Wang Zhuo", 99.8); User user3 = new user ("12347", "Yu Xin", 96.8 ); user user4 = new user ("12348", "Zheng Weishan", 98.8); User user5 = new user ("12349", "Li chaojie", 99.6 ); user user6 = new user ("12350", "Dong Mingming", 99.0); User user7 = new user ("12351", "Chen Guofeng", 100.0 ); user user8 = new user ("12352", "Chu Xiaoli", 99.6); Jedis. zadd ("game ". getbytes (), user1.getscore (), objectser. objecttobyte (user1); Jedis. zadd ("game ". getbytes (), user2.getscore (), objectser. objecttobyte (user2); Jedis. zadd ("game ". getbytes (), user3.getscore (), objectser. objecttobyte (user3); Jedis. zadd ("game ". getbytes (), user4.getscore (), objectser. objecttobyte (user4); Jedis. zadd ("game ". getbytes (), user5.getscore (), objectser. objecttobyte (user5); Jedis. zadd ("game ". getbytes (), user6.getscore (), objectser. objecttobyte (user6); Jedis. zadd ("game ". getbytes (), user7.getscore (), objectser. objecttobyte (user7); Jedis. zadd ("game ". getbytes (), user8.getscore (), objectser. objecttobyte (user8);} @ test // @ ignorepublic void gamerankshow () {set <byte []> set = Jedis. zrevrange ("game ". getbytes (), 0,-1); iterator <byte []> iter = set. iterator (); int I = 1; List <user> List = new arraylist <user> (); While (ITER. hasnext () {user = (User) objectser. bytetoobject (ITER. next (); User. setrank (I ++); list. add (User) ;}for (User user User: List) system. out. println (User );}}

Test results:

 

 

User [ID = 12351, name = Chen Guofeng, score = 100.0, Rank = 1] user [ID = 12345, name = Chang Shaopeng, score = 99.9, rank = 2] user [ID = 12346, name = wang Zhuo, score = 99.8, Rank = 3] user [ID = 12352, name = Chu Xiaoli, score = 99.6, rank = 4] user [ID = 12349, name = Li chaojie, score = 99.6, Rank = 5] user [ID = 12350, name = Dong Mingming, score = 99.0, rank = 6] user [ID = 12348, name = Zheng Weishan, score = 98.8, Rank = 7] user [ID = 12347, name = Yi Yuxin, score = 96.8, rank = 8]

The same score is generally the same. If you are interested, you can use a custom algorithm to implement the ranking.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.