jedis是redis的java版本的客户端实现。下面演示jedis的相关操作:
首先在eclipse新建动态web工程:
将jedis所需的jar包导入到工程中:
编写Java代码测试连通性:
import redis.clients.jedis.Jedis;
public class TestPing {
public static void main(String[] args) {
//192.168.202.131是我虚拟机的IP地址,6379是redis服务的端口号,请自行配置
Jedis jedis = new Jedis("192.168.202.131", 6379);
System.out.println(jedis.ping());
}
}
运行程序,会报错:
Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
我们需要更改redis的配置文件,将bind 127.0.0.1注释掉,使任何IP都可以访问redis服务:
重启redis服务,再运行程序,发现继续报错:
Exception in thread “main” redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command ‘CONFIG SET protected-mode no’ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to ‘no’, and then restarting the server. 3) If you started the server manually just for testing, restart it with the ‘–protected-mode no’ option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
大意是redis在保护模式下,拒绝在没有绑定IP地址或者没有授权密码的情况下进行访问。我们需要继续更改redis配置文件,将protected-mode yes改为protected-mode no:
重启redis服务,重新运行程序,会输出“PONG”,证明连接成功:
下面根据数据类型来测试jedis的API:
//String类型
@Test
public void testString() {
Jedis jedis = new Jedis("192.168.202.131", 6379);
//清空数据库
jedis.flushDB();
String k1 = "k1";
String v1 = "v1";
jedis.set("k1", v1);
System.out.println(jedis.get(k1));
//是否存在
System.out.println(jedis.exists(k1));
//类型
System.out.println(jedis.type(k1));
//拼接
jedis.append(k1, "v1v1v1");
System.out.println(jedis.get(k1));
//设置多个数据
jedis.mset("k2","v2","k3","3");
System.out.println(jedis.keys("*"));
//删除
jedis.del("k2");
System.out.println(jedis.keys("*"));
//自增
jedis.incr("k3");
System.out.println(jedis.get("k3"));
}
输出:
//List类型
@Test
public void testList(){
Jedis jedis = new Jedis("192.168.202.131", 6379);
//清空数据库
jedis.flushDB();
//从列表头部插入
jedis.lpush("l1", "a","b","c");
//从列表尾部插入
jedis.rpush("l1", "d","e");
//返回列表中所有值
System.out.println(jedis.lrange("l1", 0, -1));
//返回列表长度
System.out.println(jedis.llen("l1"));
//从列表头部移除一个元素并返回
System.out.println(jedis.lpop("l1"));
}
输出:
//Hash类型
@Test
public void testHash(){
Jedis jedis = new Jedis("192.168.202.131", 6379);
//清空数据库
jedis.flushDB();
Map<String,String> map = new HashMap<>();
map.put("name", "Alan");
map.put("age", "22");
jedis.hmset("user",map);
jedis.hset("user", "gender", "male");
//获取单个数据
System.out.println(jedis.hget("user", "age"));
//获取多个数据
System.out.println(jedis.hmget("user", "name","gender"));
//获取所有数据
System.out.println(jedis.hgetAll("user"));
//获取所有的键
System.out.println(jedis.hkeys("user"));
//获取所有的值
System.out.println(jedis.hvals("user"));
}
输出:
// Set类型
@Test
public void testSet() {
Jedis jedis = new Jedis("192.168.202.131", 6379);
// 清空数据库
jedis.flushDB();
//添加一个或多个数据
jedis.sadd("s1", "a","b","c");
jedis.sadd("s1", "d");
//返回集合中所有元素
System.out.println(jedis.smembers("s1"));
//成员个数
System.out.println(jedis.scard("s1"));
//求交集,并集,差集
jedis.sadd("s2", "c","d","e","f");
System.out.println(jedis.sinter("s1","s2"));
System.out.println(jedis.sunion("s1","s2"));
System.out.println(jedis.sdiff("s1","s2"));
//判断是否是成员
System.out.println(jedis.sismember("s1", "c"));
//移除一个或者多个元素
jedis.srem("s1", "b","c");
System.out.println(jedis.smembers("s1"));
}
输出:
// Zset类型
@Test
public void testZset() {
Jedis jedis = new Jedis("192.168.202.131", 6379);
// 清空数据库
jedis.flushDB();
//向有序集合中添加多个成员
Map<Double,String> map = new HashMap<>();
map.put(60.0, "Alan");
map.put(70.0, "Jack");
map.put(75.0, "Tom");
jedis.zadd("rank", map);
//返回所有成员
System.out.println(jedis.zrange("rank", 0, -1));
//带有分数地返回所有成员,字符串由ASCII码表示
System.out.println(jedis.zrangeWithScores("rank", 0, -1));
//增加成员分数
jedis.zincrby("rank", 0.5, "Alan");
System.out.println(jedis.zscore("rank", "Alan"));
//返回分数在60到70之间的成员
System.out.println(jedis.zrangeByScore("rank", 60, 70));
}
输出: