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
- program
- comic agile
- MySQL
- Software Engineering
- history
- Spain
- management
- django
- Java
- ubuntu
- leadership
- Malaysia
- Book review
- Linux
- hbase
- Kuala Lumpur
- hadoop
- essay
- web
- erlang
- QT
- Book
- Python
- Programming
- programming_book
- agile
- RFID
- Italy
- psychology
- France
Archives
- Today
- Total
Jedis - 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 TestJedisSet { public static void main(String[] args) { JedisPool pool = new JedisPool(new JedisPoolConfig(), "flatbox"); Jedis jedis = pool.getResource(); try { int ELEMENTS_NUM = 3; // set final String[] sets = { "set1", "set2", "set3" }; for ( final String set : sets ) { for ( IteratormemberItr = jedis.smembers(set).iterator(); memberItr.hasNext(); ) { final String member = memberItr.next(); if ( null != jedis.sismember(set, member) ) jedis.srem(set, member); } for ( int i = 0; i < ELEMENTS_NUM; ++i ) jedis.sadd(set, "val" + i); ++ELEMENTS_NUM; // smembers(set) returns all the members in set System.out.println("jedis.smembers(" + set + ") = " + jedis.smembers(set)); // scard(set) returns the number of members in set System.out.println("jedis.scard(" + set + ") = " + jedis.scard(set)); } // sadd(set, member) adds member to set and returns 1 if succeeded System.out.println("jedis.sadd(" + sets[0] + ", \"val-1\") = " + jedis.sadd(sets[0], "val-1")); System.out.println("jedis.smembers(" + sets[0] + ") = " + jedis.smembers(sets[0])); // sdiff(sets...) returns the result of sets[0] - sets[1] - sets[2]... System.out.println("jedis.sdiff(" + sets[2] + ", " + sets[0] + ", " + sets[1] + ") = " + jedis.sdiff(sets[2], sets[0], sets[1])); final String set0 = "set0"; // sdiffstore(set, sets...) saves the result of sdiff(sets...) in set // and returns the number of members in set System.out.println("jedis.sdiffstore(" + set0 + ", " + sets[2] + ", " + sets[0] + ", " + sets[1] + ") = " + jedis.sdiffstore(set0, sets[2], sets[0], sets[1])); System.out.println("jedis.smembers(" + set0 + ") = " + jedis.smembers(set0)); // sinter(sets...) returns the result of sets[0] & sets[1] & sets[2]... System.out.println("jedis.sinter(" + sets[0] + ", " + sets[1] + ") = " + jedis.sinter(sets[0], sets[1])); // sinterstore(set, sets...) saves the result of sinter(sets...) in set // and returns the number of members in set System.out.println("jedis.sinterstore(" + set0 + ", " + sets[0] + ", " + sets[1] + ") = " + jedis.sinterstore(set0, sets[0], sets[1])); System.out.println("jedis.smembers(" + set0 + ") = " + jedis.smembers(set0)); // sismember(set, member) returns true/false whether the member is in set System.out.println("jedis.sismember(" + set0 + ", \"val1\") = " + jedis.sismember(set0, "val1")); System.out.println("jedis.sismember(" + set0 + ", \"valx\") = " + jedis.sismember(set0, "valx")); // smove(srcKey, dstKey, member) moves member in srcKey to dstKey System.out.println("jedis.smove(" + sets[2] + ", " + set0 + ", \"val3\") = " + jedis.smove(sets[2], set0, "val3")); System.out.println("jedis.smembers(" + set0 + ") = " + jedis.smembers(set0)); // spop(set) removes & returns a random member in set System.out.println("jedis.spop(" + set0 + ") = " + jedis.spop(set0)); // srandmember(set) returns a random member in set System.out.println("jedis.srandmember(" + set0 + ") = " + jedis.srandmember(set0)); // srem(set, member) removes member from set and returns 1 if succeede System.out.println("jedis.srem(" + sets[0] + ", \"val1\") = " + jedis.srem(sets[0], "val1")); // sunion(sets...) returns the result of sets[0] | sets[1] | sets[2]... System.out.println("jedis.sunion(sets) = " + jedis.sunion(sets)); // sunionstore(set, sets...) saves the result of sunion(sets...) in set // and returns the number of members in set System.out.println("jedis.sunionstore(" + set0 + ", sets) = " + jedis.sunionstore(set0, sets)); System.out.println("jedis.smembers(" + set0 + ") = " + jedis.smembers(set0)); /* redis 127.0.0.1:6379> help @set SADD key member summary: Add a member to a set since: 0.07 SCARD key summary: Get the number of members in a set since: 0.07 ... */ } 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