Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- history
- Italy
- essay
- hadoop
- France
- Software Engineering
- Programming
- programming_book
- Book
- Kuala Lumpur
- program
- Java
- ubuntu
- Malaysia
- agile
- Book review
- django
- Spain
- Linux
- MySQL
- QT
- leadership
- psychology
- hbase
- Python
- comic agile
- erlang
- RFID
- web
- management
Archives
- Today
- Total
Jedis - Sorted Set 본문
import java.util.Set; import java.util.Iterator; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class TestJedisSortedSet { public static void main(String[] args) { JedisPool pool = new JedisPool(new JedisPoolConfig(), "flatbox"); Jedis jedis = pool.getResource(); try { int ELEMENTS_NUM = 3; // sorted set final String[] sortedSets = { "sortedSet1", "sortedSet2", "sortedSet3" }; for ( final String sortedSet : sortedSets ) { for ( Iterator<String> memberItr = jedis.zrange(sortedSet, 0, -1).iterator(); memberItr.hasNext(); ) { final String member = memberItr.next(); if ( null != jedis.zrank(sortedSet, member) ) { jedis.zrem(sortedSet, member); } } for ( int i = 0; i < ELEMENTS_NUM; ++i ) jedis.zadd(sortedSet, ELEMENTS_NUM - i, "val" + i); System.out.println("\n--------------------\t" + sortedSet); // zcard(sorted_set) returns the number of members in sorted_set System.out.println("jedis.zcard(" + sortedSet + ") = " + jedis.zcard(sortedSet)); int min = 1, max = 3; // zcount(sorted_set, min, max) // returns the number of members in sorted_set with scores [min, max] System.out.println("jedis.zcount(" + sortedSet + ", " + min + ", " + max + ") = " + jedis.zcount(sortedSet, min, max)); // zrange(sorted_set, min, max) // returns members in sorted_set with scores [min, max] System.out.println("jedis.zrange(" + sortedSet + ", " + min + ", " + max + ") = " + jedis.zrange(sortedSet, min, max)); // zrangeWithScores(sorted_set, start, end) // returns members with their scores in sorted_set from [start, end] System.out.println("jedis.zrangeWithScores(" + sortedSet + ", " + min + ", " + max + ") = " + jedis.zrangeWithScores(sortedSet, min, max)); // zrangeByScore(sorted_set, min, max) // returns members in sorted_set with scores [min, max] System.out.println("jedis.zrangeByScore(" + sortedSet + ", " + min + ", " + max + ") = " + jedis.zrangeByScore(sortedSet, min, max)); // zrangeByScore(sorted_set, min, max, offset, count) // returns members in sorted_set with scores [min, max] // from offset, number of count members System.out.println("jedis.zrangeByScore(" + sortedSet + ", 0, " + ELEMENTS_NUM + ", 2, 2) = " + jedis.zrangeByScore(sortedSet, 0, ELEMENTS_NUM, 2, 2)); // zrangeByScoreWithScores(sorted_set, min, max) // returns members with their scores in sorted_set with scores [min, max] System.out.println("jedis.zrangeByScoreWithScores(" + sortedSet + ", 0, " + ELEMENTS_NUM + ") = " + jedis.zrangeByScoreWithScores(sortedSet, 0, ELEMENTS_NUM)); // zrangeByScoreWithScores(sorted_set, min, max, offset, count) // returns members with their scores in sorted_set // with scores [min, max] from offset, number of count members System.out.println("jedis.zrangeByScoreWithScores(" + sortedSet + ", 0, " + ELEMENTS_NUM + ", 2, 2) = " + jedis.zrangeByScoreWithScores(sortedSet, 0, ELEMENTS_NUM, 2, 2)); // zrevrange(sorted_set, max, min) // returns members in sorted_set with scores [min, max] in reverse order System.out.println("jedis.zrevrange(" + sortedSet + ", 0, " + ELEMENTS_NUM + ") = " + jedis.zrevrange(sortedSet, 0, ELEMENTS_NUM)); // zrevrangeWithScores(sorted_set, start, end) // returns members with their scores in sorted_set from [start, end] // in reverse order System.out.println("jedis.zrevrangeWithScores(" + sortedSet + ", 0, " + ELEMENTS_NUM + ") = " + jedis.zrevrangeWithScores(sortedSet, 0, ELEMENTS_NUM)); // zrevrangeByScore(sorted_set, max, min) // returns members with their scores in sorted_set with scores [max, min] // in reverse order System.out.println("jedis.zrevrangeByScore(" + sortedSet + ", " + ELEMENTS_NUM + ", 0) = " + jedis.zrevrangeByScore(sortedSet, ELEMENTS_NUM, 0)); // zrevrangeByScore(sorted_set, max, min, offset, count) // returns members with their scores in sorted_set with scores [max, min] // from offset, number of count members in reverse order System.out.println("jedis.zrevrangeByScore(" + sortedSet + ", " + ELEMENTS_NUM + ", 0, 2, 2) = " + jedis.zrevrangeByScore(sortedSet, ELEMENTS_NUM, 0, 2, 2)); // zrevrangeByScoreWithScores(sorted_set, max, min) // returns members with their scores in sorted_set with scores [max, min] // in reverse order System.out.println("jedis.zrevrangeByScoreWithScores(" + sortedSet + ", " + ELEMENTS_NUM + ", 0) = " + jedis.zrevrangeByScoreWithScores(sortedSet, ELEMENTS_NUM, 0)); // zrevrangeByScoreWithScores(sorted_set, max, min, offset, count) // returns members with their scores in sorted_set with scores [max, min] // from offset, number of count members in reverse order System.out.println("jedis.zrevrangeByScoreWithScores(" + sortedSet + ", " + ELEMENTS_NUM + ", 0, 2, 2) = " + jedis.zrevrangeByScoreWithScores(sortedSet, ELEMENTS_NUM, 0, 2, 2)); ++ELEMENTS_NUM; } System.out.println(); // zadd(sorted_set, score, value) adds value with score to sorted_set System.out.println("jedis.zadd(" + sortedSets[0] + ", 2.5, \"val2.5\") = " + jedis.zadd(sortedSets[0], 2.5, "val2.5")); // zincrby(sorted_set, plus_score, value) increases value's orginal score // to original score + plus_score in sorted_set System.out.println("jedis.zincrby(" + sortedSets[0] + ", 2, \"val0\") = " + jedis.zincrby(sortedSets[0], 2, "val0")); System.out.println("jedis.zadd(" + sortedSets[1] + ", 3.5, \"val3.5\") = " + jedis.zadd(sortedSets[1], 3.5, "val3.5")); System.out.println("jedis.zincrby(" + sortedSets[1] + ", 3, \"val0\") = " + jedis.zincrby(sortedSets[1], 3, "val0")); System.out.println("jedis.zadd(" + sortedSets[2] + ", 1.5, \"val1.5\") = " + jedis.zadd(sortedSets[2], 1.5, "val1.5")); System.out.println("jedis.zincrby(" + sortedSets[2] + ", 5, \"val1\") = " + jedis.zincrby(sortedSets[2], 5, "val1")); for ( final String sortedSet : sortedSets ) { System.out.println("\n--------------------\t" + sortedSet); // zrange(sorted_set, 0, -1) returns all the values in sorted_set System.out.println("jedis.zrange(" + sortedSet + ", 0, -1) = " + jedis.zrange(sortedSet, 0, -1)); // zrank(sorted_set, value) returns the index number of value in sorted_set System.out.println("jedis.zrank(" + sortedSet + ", \"val0\") = " + jedis.zrank(sortedSet, "val0")); // zrevrank(sorted_set, value) returns the index number of value // in sorted_set in reverse order System.out.println("jedis.zrevrank(" + sortedSet + ", \"val0\") = " + jedis.zrevrank(sortedSet, "val0")); // zscore(sorted_set, value) returns the score of value in sorted_set System.out.println("jedis.zscore(" + sortedSet + ", \"val0\") = " + jedis.zscore(sortedSet, "val0")); // zrem(sorted_set, value) removes value in sorted_set // and returns 1 if succeeded System.out.println("jedis.zrem(" + sortedSet + ", \"val0\") = " + jedis.zrem(sortedSet, "val0")); } System.out.println(); int start = 1, end = 2; // zremrangeByRank(sorted_set, start, end) removes all the members // in sorted_set within the index [start, end] // the result looks wrong in my opinion System.out.println("jedis.zremrangeByRank(" + sortedSets[0] + ", " + start + ", " + end + ") = " + jedis.zremrangeByRank(sortedSets[0], start, end)); // zremrangeByScore(sorted_set, min, max) removes all the members // in sorted_set within the score [min, max] System.out.println("jedis.zremrangeByScore(" + sortedSets[0] + ", " + start + ", " + end + ") = " + jedis.zremrangeByScore(sortedSets[0], start, end)); for ( final String sortedSet : sortedSets ) { System.out.println("\n--------------------\t" + sortedSet); System.out.println("jedis.zrange(" + sortedSet + ", 0, -1) = " + jedis.zrange(sortedSet, 0, -1)); } System.out.println(); final String sortedSet0 = "sortedSet0"; // zinterstore(sorted_set, sorted_sets...) saves the result of // sorted_sets[0] & sorted_sets[1] & sorted_sets[2]... in sorted_set // the result is related to not only value but also score in my opinion System.out.println("jedis.zinterstore(" + sortedSet0 + ", sortedSets) = " + jedis.zinterstore(sortedSet0, sortedSets)); System.out.println("jedis.zrange(" + sortedSet0 + ", 0, -1) = " + jedis.zrange(sortedSet0, 0, -1)); // zunionstore(sorted_set, sorted_sets...) saves the result of // sorted_sets[0] | sorted_sets[1] | sorted_sets[2]... in sorted_set System.out.println("jedis.zunionstore(" + sortedSet0 + ", sortedSets) = " + jedis.zunionstore(sortedSet0, sortedSets)); System.out.println("jedis.zrange(" + sortedSet0 + ", 0, -1) = " + jedis.zrange(sortedSet0, 0, -1)); /* redis 127.0.0.1:6379> help @sorted_set ZADD key score member summary: Add a member to a sorted set, or update its score if it already exists since: 1.1 ZCARD key summary: Get the number of members in a sorted set since: 1.1 ... */ } finally { /// ... it's important to return the Jedis instance to the pool //once you've finished using it pool.returnResource(jedis); } /// ... when closing your application: pool.destroy(); } }
Comments