Java的Redis客户端Jedis

Posted QIANQIANCHEN

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java的Redis客户端Jedis相关的知识,希望对你有一定的参考价值。

 Java的Redis客户端Jedis

一、用windows中的Eclipse连接虚拟机的Redis的注意事项

禁用Linux的防火墙:Linux里执行命令 service iptables  stop

我的是已经禁用的,

 

开看看禁用启用状态都长什么样子

 

修改redis.conf中注释掉bind 127.0.0.1 ,然后 protect-mode no

 

二、新建动态java工程,

① 导包

 

 吐槽一下,这个workspace里都是Maven工程,去别的工程复制个junit jar包都找不到,好不容易找到包导入后运行又报错,

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing

②测试连接

三、测试key    

 

 1 System.out.println("----------KEY----------");
 2         Set<String> keySet = jedis.keys("*");
 3         
 4         for (String key : keySet) {
 5             System.out.println(key);
 6         }
 7         System.out.println("-------------------");
 8         Iterator<String> iterator = keySet.iterator();
 9 
10         while (iterator.hasNext()) {
11             String key = iterator.next();
12             System.out.println(key);
13 
14         }
15         Boolean exists = jedis.exists("index");
16         System.out.println(exists);
17         
18         System.out.println("---------String---------");
19         
20         System.out.println(jedis.get("k1"));
21         jedis.set("kn", "vn");
22         jedis.mset("str1","vstr1","str2","vstr2","str3","vstr3","str4","vstr4");
23         System.out.println(jedis.mget("str1","str2","str3","str4","str5"));
24         
25         System.out.println("---------List---------");
26         
27         List<String> list = jedis.lrange("index", 0, -1);
28         for (String index : list) {
29             System.out.println(index);
30         }
31         
32         System.out.println("---------Set---------");
33         
34         jedis.sadd("orders", "js001","js002","js003","js004","js005");
35         jedis.sadd("orders", "js006");
36         
37         jedis.sadd("names", "Jone001","Jone002","Jone003","Jone004","Jone005");
38         jedis.sadd("names", "Jone006");
39         Set<String> orders = jedis.smembers("orders");
40         Set<String> name = jedis.smembers("names");
41         System.out.println("order 的 元素:---------------");
42         for (String order : orders) {
43             System.out.println(order);
44         }
45         System.out.println("names个数" + jedis.scard("names"));
46         System.out.println("orders个数" + jedis.scard("orders"));
47         Set<String> sunion = jedis.sunion("names","orders");
48         System.out.println("jedis.sunion(\'names\',\'orders\') :---------");
49         for (String sn : sunion) {
50             System.out.println(sn);
51         }
52         
53         Long remove = jedis.srem("names", "Jone002");
54         System.out.println("是否删除成功 0:失败;1:成功"+ remove);
55         System.out.println("names个数" + jedis.scard("names"));
56         
57         System.out.println("---------Hash---------");
58         
59         Boolean hexists = jedis.hexists("hash1", "age");
60         System.out.println(hexists);
61         System.out.println(jedis.hlen("hash1"));
62         System.out.println(jedis.hsetnx("User", "username", "huahua"));
63         Map<String, String> maphash1 = jedis.hgetAll("hash1");
64         Set<Entry<String,String>> entrySet = maphash1.entrySet();
65         for (Entry<String, String> entry : entrySet) {
66             System.out.println(entry.getKey() +"------>"+ entry.getValue());
67         }
68         System.out.println(jedis.hget("User", "username"));
69         
70         System.out.println("---------Zset:如何利用zset实现一个文章访问量的排行榜---------");
71         
72         Map<Double,String> bandMap = new HashMap<>();
73         bandMap.put(12323D, "诛仙");
74         bandMap.put(1001D, "BitKeep做错了什么?");
75         bandMap.put(1000002D, "穷爸爸富爸爸");
76         bandMap.put(32343D, "一千零一夜");
77         bandMap.put(7777D, "诗经");
78         bandMap.put(878686D, "论语");
79         bandMap.put(8782236.0, "太史公记");
80         //(String, Map<Double,String>
81         jedis.zadd("pagesBand",bandMap);
82         
83         System.out.println(jedis.zcard("pagesBand"));;
84     
85         Set<String> set = jedis.zrange("pagesBand", 0, -1);
86         for (String string : set) {
87             System.out.println(string);
88         }
89         
90         Set<Tuple> scores = jedis.zrangeWithScores("pagesBand", 0, -1);
91         for (Tuple tuple : scores) {
92             System.out.println(tuple.getScore() +"--->" + tuple.getElement());
93         }
94         System.out.println("---------------------------");
95         Set<String> set2 = jedis.zrangeByScore("pagesBand", 1001, 878686);
96         Set<Tuple> scoreWithScores = jedis.zrangeByScoreWithScores("pagesBand",1000D, 100000D);
97         for (Tuple tuple : scoreWithScores) {
98             System.out.println(tuple.getScore() +"--->" + tuple.getElement());
99         }

 

 部分控制台打印结果

 1 ---------Hash---------
 2 true
 3 7
 4 0
 5 sex------>man
 6 tel------>139121212
 7 id------>2
 8 dept------>anbao
 9 dept1------>anbao
10 age------>11
11 username------>zhangsan
12 huahua
13 ---------Zset:如何利用zset实现一个文章访问量的排行榜---------
14 7
15 BitKeep做错了什么?
16 诗经
17 诛仙
18 一千零一夜
19 论语
20 穷爸爸富爸爸
21 太史公记
22 1001.0--->BitKeep做错了什么?
23 7777.0--->诗经
24 12323.0--->诛仙
25 32343.0--->一千零一夜
26 878686.0--->论语
27 1000002.0--->穷爸爸富爸爸
28 8782236.0--->太史公记
29 ---------------------------
30 1001.0--->BitKeep做错了什么?
31 7777.0--->诗经
32 12323.0--->诛仙
33 32343.0--->一千零一夜

 

以上是关于Java的Redis客户端Jedis的主要内容,如果未能解决你的问题,请参考以下文章

Redis --Java客户端 Jedis

jedis连接redis

Jedis/JedisPool和Redis数据类型与特性

redis--jedis

Redis的Java客户端Jedis的八种调用方式(事务管道分布式…)介绍(转)

spring 集成redis客户端jedis(java)