Jedis所需要的jar包
Commons-pool-1.6.jar
Jedis-2.1.0.jar
用windows中的Eclipse连接虚拟机的Redis的注意事项
禁用Linux的防火墙:
临时禁用:service iptables stop
关闭开机自启:chkconfig iptables off
redis.conf中注释掉bind 127.0.0.1(61行) ,然后 protect-mode(80行)设置为 no。
Jedis测试连通性
public class Demo01 { public static void main(String[] args) { //连接本地的 Redis 服务 Jedis jedis = new Jedis("127.0.0.1",6379); //查看服务是否运行,打出pong表示OK System.out.println("connection is OK==========>: "+jedis.ping()); } }
Jedis-API: Key
//key Set<String> keys = jedis.keys("*"); for (Iterator iterator = keys.iterator(); iterator.hasNext();) { String key = (String) iterator.next(); System.out.println(key); } System.out.println("jedis.exists====>"+jedis.exists("k2")); System.out.println(jedis.ttl("k1"));
Jedis-API: String
System.out.println(jedis.get("k1")); jedis.set("k4","k4_Redis"); System.out.println("----------------------------------------"); jedis.mset("str1","v1","str2","v2","str3","v3"); System.out.println(jedis.mget("str1","str2","str3"));
Jedis-API: List
List<String> list = jedis.lrange("mylist",0,-1); for (String element : list) { System.out.println(element); }
Jedis-API: set
jedis.sadd("orders","jd001"); jedis.sadd("orders","jd002"); jedis.sadd("orders","jd003"); Set<String> set1 = jedis.smembers("orders"); for (Iterator iterator = set1.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); System.out.println(string); } jedis.srem("orders","jd002");
Jedis-API: hash
jedis.hset("hash1","userName","lisi"); System.out.println(jedis.hget("hash1","userName")); Map<String,String> map = new HashMap<String,String>(); map.put("telphone","13810169999"); map.put("address","atguigu"); map.put("email","abc@163.com"); jedis.hmset("hash2",map); List<String> result = jedis.hmget("hash2", "telphone","email"); for (String element : result) { System.out.println(element); }
Jedis-API: zset
jedis.zadd("zset01",60d,"v1"); jedis.zadd("zset01",70d,"v2"); jedis.zadd("zset01",80d,"v3"); jedis.zadd("zset01",90d,"v4"); Set<String> s1 = jedis.zrange("zset01",0,-1); for (Iterator iterator = s1.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); System.out.println(string); }