ジェーディsの操作と使用
10090 ワード
一、ジェディズ
ジェーディsはレディが推奨するjavaクライアントです.Jedisを通じて、私たちはJavaコードを使いやすく、redisを操作します.jedisは使用するのが簡単で、その操作方法はredisコマンドと似ています.初めてredisを使う人にとっては、より早くて、より適応できます.jedisのgithubの上のダウンロードの住所はそうです.https://github.com/xetorthio/jedis .この例では、mavenを使用していますが、以下のような依存性を追加する必要があります.
ジェーディsはレディが推奨するjavaクライアントです.Jedisを通じて、私たちはJavaコードを使いやすく、redisを操作します.jedisは使用するのが簡単で、その操作方法はredisコマンドと似ています.初めてredisを使う人にとっては、より早くて、より適応できます.jedisのgithubの上のダウンロードの住所はそうです.https://github.com/xetorthio/jedis .この例では、mavenを使用していますが、以下のような依存性を追加する必要があります.
redis.clients
jedis
2.6.0
二、関連例以下はデータに対する基本的な動作の一部にすぎない.Jedis操作対象を取得します.Jedis jedis;
@Before
public void connectionTest() {
jedis = new Jedis("127.0.0.1", 6379);//redis
//jedis.auth("helloworld"); // ( requirepass helloworld)
}
Jedisのkeyに対する操作@Test
public void keyTest() throws UnsupportedEncodingException {
System.out.println(jedis.flushDB());//
System.out.println(jedis.echo("hello"));
// key
System.out.println(jedis.exists("foo"));
jedis.set("key", "values");
jedis.set("key2", "values");
System.out.println(jedis.exists("key"));//
// key, nil, key。
String randomKey = jedis.randomKey();
System.out.println("randomKey: " + randomKey);
// 60 key
jedis.expire("key", 60);
// key
System.out.println(jedis.pttl("key"));
// key
jedis.persist("key");
// key , "string", "list", "set". "none" none key
System.out.println("type: " + jedis.type("key"));
// key
byte[] bytes = jedis.dump("key");
System.out.println(new String(bytes));
// key
jedis.renamenx("key", "keytest");
System.out.println("key : " + jedis.exists("key"));//
System.out.println("keytest : " + jedis.exists("keytest"));//
// key
// KEYS * key 。
// KEYS h?llo hello , hallo hxllo 。
// KEYS h*llo hllo heeeeello 。
// KEYS h[ae]llo hello hallo , hillo 。
// \ 。
Set set = jedis.keys("k*");
System.out.println(set);
// key
jedis.del("key");
System.out.println(jedis.exists("key"));
}
Jedisの文字列(String)に関する操作@Test
public void stringTest() {
jedis.set("hello", "hello");
System.out.println(jedis.get("hello"));
// append
jedis.append("hello", " world");
System.out.println(jedis.get("hello"));
// set
jedis.set("hello", "123");
System.out.println(jedis.get("hello"));
//
jedis.setex("hello2", 2, "world2");
System.out.println(jedis.get("hello2"));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
System.out.println(jedis.get("hello2"));
// key-value
jedis.mset("a", "1", "b", "2");
// a b value
List valus = jedis.mget("a", "b");
System.out.println(valus);
//
jedis.del("a", "b");
System.out.println(jedis.exists("a"));
System.out.println(jedis.exists("b"));
}
Jedis対チェーン(Lists)の操作@Test
public void listTest() {
String key = "mylist";
jedis.del(key);
//
jedis.rpush(key, "aaaa");
jedis.rpush(key, "aaaa");
jedis.rpush(key, "bbbb");
jedis.rpush(key, "cccc");
jedis.rpush(key, "cccc");
//
System.out.println("lenth: " + jedis.llen(key));
// , 0 , 1 ( )
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
// 1
System.out.println("index of 1: " + jedis.lindex(key, 1));
// , index error。
jedis.lset(key, 1, "aa22");
System.out.println("index of 1: " + jedis.lindex(key, 1));
//
jedis.rpush(key, "-2", "-1");// -2, -1
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
//
jedis.lpush(key, "second element", "first element");// second
// element, first
// elementF
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
//
System.out.println(jedis.rpop(key));
//
System.out.println(jedis.lpop(key));
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
// count > 0: value ,count 。
// count < 0: value ,count 。
// count = 0: value 。
jedis.lrem(key, 1, "cccc");
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
// 。 start list , 。
// stop list ,Redis 。
System.out.println(jedis.lrange(key, 0, 2));
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
//
System.out.println(jedis.ltrim(key, 0, 2));
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
}
Jedis対集合(Sets)の操作@Test
public void testSet() {
//
System.out.println(jedis.flushDB());
String key = "myset";
String key2 = "myset2";
//
jedis.sadd(key, "aaa", "bbb", "ccc");
jedis.sadd(key2, "bbb", "ccc", "ddd");
//
System.out.println(jedis.scard(key));
// ,
jedis.sinterstore("destination", key, key2);
System.out.println(jedis.smembers("destination"));
// ,
jedis.sunionstore("destination", key, key2);
System.out.println(jedis.smembers("destination"));
// key ,key2 ,
jedis.sdiffstore("destination", key, key2);
System.out.println(jedis.smembers("destination"));
//
System.out.println(jedis.sismember(key, "aaa"));
// key
System.out.println(jedis.srandmember(key));
// aaa key key2
jedis.smove(key, key2, "aaa");
System.out.println(jedis.smembers(key));
System.out.println(jedis.smembers(key2));
//
System.out.println(jedis.spop(key));
//
jedis.srem(key2, "ccc", "ddd");
System.out.println(jedis.smembers(key2));
}
Jedisの規則的な集合(Sorted Sets)に対する操作@Test
public void testSortSet() {
//
System.out.println(jedis.flushDB());
String key = "mysortset";
Map scoreMembers = new HashMap();
scoreMembers.put("aaa", 1001.0);
scoreMembers.put("bbb", 1002.0);
scoreMembers.put("ccc", 1003.0);
//
jedis.zadd(key, 1004.0, "ddd");
jedis.zadd(key, scoreMembers);
//
System.out.println(jedis.zcard(key));
// , 0 , 1 , 。
// , -1 ,-2
Set coll = jedis.zrange(key, 0, -1);
System.out.println(coll);
//
coll = jedis.zrevrange(key, 0, -1);
System.out.println(coll);
//
System.out.println(jedis.zscore(key, "bbb"));
//
System.out.println(jedis.zrem(key, "aaa"));
System.out.println(jedis.zrange(key, 0, -1));
//
System.out.println(jedis.zcount(key, 1002.0, 1003.0));
}
Jedisのハッシュに対する操作@Test
public void testHash() {
//
System.out.println(jedis.flushDB());
String key = "myhash";
Map hash = new HashMap();
hash.put("aaa", "11");
hash.put("bbb", "22");
hash.put("ccc", "33");
//
jedis.hmset(key, hash);
jedis.hset(key, "ddd", "44");
// hash (key )
System.out.println(jedis.hkeys(key));
// hash key value
System.out.println(jedis.hvals(key));
// hash
System.out.println(jedis.hlen(key));
// hash , Map
Map elements = jedis.hgetAll(key);
System.out.println(elements);
// key
System.out.println(jedis.hexists(key, "bbb"));
// hash
System.out.println(jedis.hmget(key, "aaa", "bbb"));
//
System.out.println(jedis.hget(key, "aaa"));
//
System.out.println(jedis.hdel(key, "aaa"));
System.out.println(jedis.hgetAll(key));
// key field increment
System.out.println(jedis.hincrBy(key, "bbb", 100));
System.out.println(jedis.hgetAll(key));
}
Jedis操作事務@Test
public void testTransaction() {
Transaction t = jedis.multi();
t.set("hello", "world");
Response response = t.get("hello");
t.zadd("foo", 1, "barowitch");
t.zadd("foo", 0, "barinsky");
t.zadd("foo", 0, "barikoviev");
Response> sose = t.zrange("foo", 0, -1); //
System.out.println(response);
System.out.println(sose);
t.exec(); // ,
String foolbar = response.get(); // Response.get()
int soseSize = sose.get().size(); // sose.get() set
System.out.println(foolbar);
System.out.println(sose.get());
}
Jedis操作配管@Test
public void testTransactionPipeling() {
Pipeline p = jedis.pipelined();//
p.set("fool", "bar");
p.zadd("foo", 1, "barowitch");
p.zadd("foo", 0, "barinsky");
p.zadd("foo", 0, "barikoviev");
Response pipeString = p.get("fool");
Response> sose = p.zrange("foo", 0, -1);
System.out.println(pipeString);
System.out.println(sose);
p.sync();//
System.out.println("==========");
System.out.println(p.get("fool"));
System.out.println(p.zrange("foo", 0, -1));
int soseSize = sose.get().size();
Set setBack = sose.get();
System.out.println(soseSize);
System.out.println(setBack);
}