redis客户端--jedis

一、jedis

jedis 是 redis推荐的java客户端。通过Jedis我们可以很方便地使用java代码的方式,对redis进行操作。jedis使用起来比较简单,它的操作方法与redis命令相类似。对于初次使用redis的人来说,上手更快,更能适应。jedis在github上的下载地址为https://github.com/xetorthio/jedis 。本例子使用maven,需要添加如下依赖:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.6.0</version> 
</dependency>

二、相关实例
  以下只是对数据的一些基本操作。
  获取Jedis操作对象

1 Jedis jedis;
2 
3 @Before
4 public void connectionTest() {
5 jedis = new Jedis("127.0.0.1", 6379);//redis的地址以及连接端口
6 //jedis.auth("helloworld");  //开启密码验证(配置文件中为 requirepass helloworld)的时候需要执行该方法
7
}

  Jedis对key的操作

 1 @Test
 2 public void keyTest() throws UnsupportedEncodingException {
 3 System.out.println(jedis.flushDB());// 清空数据
 4 System.out.println(jedis.echo("hello"));
 5 
 6 // 判断key否存在
 7 System.out.println(jedis.exists("foo"));
 8 
 9 jedis.set("key", "values");
10 jedis.set("key2", "values");
11 System.out.println(jedis.exists("key"));// 判断是否存在
12 
13 // 如果数据库没有任何key,返回nil,否则返回数据库中一个随机的key。
14 String randomKey = jedis.randomKey();
15 System.out.println("randomKey: " + randomKey);
16 
17 // 设置60秒后该key过期
18 jedis.expire("key", 60);
19 
20 // key有效毫秒数
21 System.out.println(jedis.pttl("key"));
22 
23 // 移除key的过期时间
24 jedis.persist("key");
25 
26 // 获取key的类型, "string", "list", "set". "none" none表示key不存在
27 System.out.println("type: " + jedis.type("key"));
28 
29 // 导出key的值
30 byte[] bytes = jedis.dump("key");
31 System.out.println(new String(bytes));
32 
33 // 将key重命名
34 jedis.renamenx("key", "keytest");
35 System.out.println("key是否存在: " + jedis.exists("key"));// 判断是否存在
36 System.out.println("keytest是否存在: " + jedis.exists("keytest"));// 判断是否存在
37 
38 // 查询匹配的key
39 // KEYS * 匹配数据库中所有 key 。
40 // KEYS h?llo 匹配 hello , hallo 和 hxllo 等。
41 // KEYS h*llo 匹配 hllo 和 heeeeello 等。
42 // KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。
43 // 特殊符号用  隔开。
44 Set<String> set = jedis.keys("k*");
45 System.out.println(set);
46 
47 // 删除key
48 jedis.del("key");
49 System.out.println(jedis.exists("key"));
50 }

  Jedis对字符串(String)的相关操作

 1 @Test
 2 public void stringTest() {
 3 jedis.set("hello", "hello");
 4 System.out.println(jedis.get("hello"));
 5 
 6 // 使用append 向字符串后面添加
 7 jedis.append("hello", " world");
 8 System.out.println(jedis.get("hello"));
 9 
10 // set覆盖字符串
11 jedis.set("hello", "123");
12 System.out.println(jedis.get("hello"));
13 
14 // 设置过期时间
15 jedis.setex("hello2", 2, "world2");
16 System.out.println(jedis.get("hello2"));
17 try {
18 Thread.sleep(3000);
19 } catch (InterruptedException e) {
20 }
21 System.out.println(jedis.get("hello2"));
22 
23 // 一次添加多个key-value对
24 jedis.mset("a", "1", "b", "2");
25 // 获取a和b的value
26 List<String> valus = jedis.mget("a", "b");
27 System.out.println(valus);
28 
29 // 批量删除
30 jedis.del("a", "b");
31 System.out.println(jedis.exists("a"));
32 System.out.println(jedis.exists("b"));
33 }

  Jedis对链表(Lists)的操作

 1 @Test
 2 public void listTest() {
 3 String key = "mylist";
 4 jedis.del(key);
 5 
 6 // 队列添加元素
 7 jedis.rpush(key, "aaaa");
 8 jedis.rpush(key, "aaaa");
 9 jedis.rpush(key, "bbbb");
10 jedis.rpush(key, "cccc");
11 jedis.rpush(key, "cccc");
12 
13 // 队列长度
14 System.out.println("lenth: " + jedis.llen(key));
15 
16 // 打印队列,从索引0开始,到倒数第1个(全部元素)
17 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
18 
19 // 索引为1的元素
20 System.out.println("index of 1: " + jedis.lindex(key, 1));
21 
22 // 设置队列里面一个元素的值,当index超出范围时会返回一个error。
23 jedis.lset(key, 1, "aa22");
24 System.out.println("index of 1: " + jedis.lindex(key, 1));
25 
26 // 从队列的右边入队一个元素
27 jedis.rpush(key, "-2", "-1");// 先-2,后-1入队列
28 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
29 
30 // 从队列的左边入队一个或多个元素
31 jedis.lpush(key, "second element", "first element");// 先second
32 // element,后first
33 // elementF入队列
34 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
35 
36 // 从队列的右边出队一个元素
37 System.out.println(jedis.rpop(key));
38 // 从队列的左边出队一个元素
39 System.out.println(jedis.lpop(key));
40 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
41 
42 // count > 0: 从头往尾移除值为 value 的元素,count为移除的个数。
43 // count < 0: 从尾往头移除值为 value 的元素,count为移除的个数。
44 // count = 0: 移除所有值为 value 的元素。
45 jedis.lrem(key, 1, "cccc");
46 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
47 
48 // 即最右边的那个元素也会被包含在内。 如果start比list的尾部下标大的时候,会返回一个空列表。
49 // 如果stop比list的实际尾部大的时候,Redis会当它是最后一个元素的下标。
50 System.out.println(jedis.lrange(key, 0, 2));
51 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
52 
53 // 删除区间以外的元素
54 System.out.println(jedis.ltrim(key, 0, 2));
55 System.out.println("all elements: " + jedis.lrange(key, 0, -1));
56 }

  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)的操作

 1 @Test
 2 public void testSortSet() {
 3 // 清空数据
 4 System.out.println(jedis.flushDB());
 5 String key = "mysortset";
 6 
 7 Map<String, Double> scoreMembers = new HashMap<String, Double>();
 8 scoreMembers.put("aaa", 1001.0);
 9 scoreMembers.put("bbb", 1002.0);
10 scoreMembers.put("ccc", 1003.0);
11 
12 // 添加数据
13 jedis.zadd(key, 1004.0, "ddd");
14 jedis.zadd(key, scoreMembers);
15 
16 // 获取一个排序的集合中的成员数量
17 System.out.println(jedis.zcard(key));
18 
19 // 返回的成员在指定范围内的有序集合,以0表示有序集第一个成员,以1表示有序集第二个成员,以此类推。
20 // 负数下标,以-1表示最后一个成员,-2表示倒数第二个成员
21 Set<String> coll = jedis.zrange(key, 0, -1);
22 System.out.println(coll);
23 
24 // 返回的成员在指定范围内的逆序集合
25 coll = jedis.zrevrange(key, 0, -1);
26 System.out.println(coll);
27 
28 // 元素下标
29 System.out.println(jedis.zscore(key, "bbb"));
30 
31 // 删除元素
32 System.out.println(jedis.zrem(key, "aaa"));
33 System.out.println(jedis.zrange(key, 0, -1));
34 
35 // 给定值范围内的成员数
36 System.out.println(jedis.zcount(key, 1002.0, 1003.0));
37 }

Jedis对哈希(Hashs)的操作

 1 @Test
 2 public void testHash() {
 3 // 清空数据
 4 System.out.println(jedis.flushDB());
 5 String key = "myhash";
 6 Map<String, String> hash = new HashMap<String, String>();
 7 hash.put("aaa", "11");
 8 hash.put("bbb", "22");
 9 hash.put("ccc", "33");
10 
11 // 添加数据
12 jedis.hmset(key, hash);
13 jedis.hset(key, "ddd", "44");
14 
15 // 获取hash的所有元素(key值)
16 System.out.println(jedis.hkeys(key));
17 
18 // 获取hash中所有的key对应的value值
19 System.out.println(jedis.hvals(key));
20 
21 // 获取hash里所有元素的数量
22 System.out.println(jedis.hlen(key));
23 
24 // 获取hash中全部的域和值,以Map<String, String> 的形式返回
25 Map<String, String> elements = jedis.hgetAll(key);
26 System.out.println(elements);
27 
28 // 判断给定key值是否存在于哈希集中
29 System.out.println(jedis.hexists(key, "bbb"));
30 
31 // 获取hash里面指定字段对应的值
32 System.out.println(jedis.hmget(key, "aaa", "bbb"));
33 
34 // 获取指定的值
35 System.out.println(jedis.hget(key, "aaa"));
36 
37 // 删除指定的值
38 System.out.println(jedis.hdel(key, "aaa"));
39 System.out.println(jedis.hgetAll(key));
40 
41 // 为key中的域 field 的值加上增量 increment
42 System.out.println(jedis.hincrBy(key, "bbb", 100));
43 System.out.println(jedis.hgetAll(key));
44 }

  Jedis操作事务

 1 @Test
 2 public void testTransaction() {
 3 Transaction t = jedis.multi();
 4 t.set("hello", "world");
 5 Response<String> response = t.get("hello");
 6 
 7 t.zadd("foo", 1, "barowitch");
 8 t.zadd("foo", 0, "barinsky");
 9 t.zadd("foo", 0, "barikoviev");
10 Response<Set<String>> sose = t.zrange("foo", 0, -1); //  返回全部相应并以有序集合的方式返回
11 System.out.println(response);
12 System.out.println(sose);
13 t.exec(); // 此行注意,不能缺少
14 
15 String foolbar = response.get(); // Response.get() 可以从响应中获取数据
16 
17 int soseSize = sose.get().size(); // sose.get() 会立即调用set方法
18 System.out.println(foolbar);
19 System.out.println(sose.get());
20 }

  Jedis操作管道

 1     @Test
 2     public void testTransactionPipeling() {
 3         Pipeline p = jedis.pipelined();//开一个管道
 4 
 5         p.set("fool", "bar");
 6         p.zadd("foo", 1, "barowitch");
 7         p.zadd("foo", 0, "barinsky");
 8         p.zadd("foo", 0, "barikoviev");
 9         Response<String> pipeString = p.get("fool");
10         Response<Set<String>> sose = p.zrange("foo", 0, -1);
11         System.out.println(pipeString);
12         System.out.println(sose);
13 
14         p.sync();//提交
15 
16         System.out.println("==========");
17         System.out.println(p.get("fool"));
18         System.out.println(p.zrange("foo", 0, -1));
19 
20         int soseSize = sose.get().size();
21         Set<String> setBack = sose.get();
22 
23         System.out.println(soseSize);
24         System.out.println(setBack);
25     }
原文地址:https://www.cnblogs.com/always-online/p/4121116.html