使用Java来操作Redis,Jedis是Redis官方推荐使用的Java连接redis的客户端,这篇文章带你简单了解如何使用Jedis。
这里我们导入两个包一个是jedis、还有一个就是阿里巴巴的fastjson。
<dependencies>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>3.2.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.62version>
dependency>
dependencies>
package com.wlm;
import redis.clients.jedis.Jedis;
public class TestPing {
public static void main(String[] args) {
// 1、new Jedis 对象
Jedis jedis = new Jedis("127.0.0.1",6379);
// 2、可以通过这个对象直接操作我们的五大数据类型和三种特殊类型
System.out.println(jedis.ping());
}
}
如果不想连接本地也可以连接远程,远程连接不上,可能有几个原因:
1.配置文件的ip限制注释掉
2.远程访问保护关掉,设置为no
3.如果是阿里ECS,需要开放6379端口
4.检查下是不是防火墙被拦了
5.远程redis启动了没有端口
五种常用数据类型:
String(字符串)
List(列表)
Set (集合)
Hash(哈希)
Zset(有序集合)
三种特殊类型:
Geospatial(地理位置)
Hyperloglog(基数统计)
Bitmap(位图)
这里就测试我们常用的五种数据类型(和我们在服务器使用Linux一样的):
package com.wlm;
import redis.clients.jedis.Jedis;
public class TestKey {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
System.out.println(jedis.ping());
// String
System.out.println(jedis.set("k1", "v1"));
System.out.println(jedis.get("k1")); // v1
System.out.println(jedis.append("k1", "+value"));
System.out.println(jedis.get("k1")); // v1+value
System.out.println(jedis.strlen("k1")); // 8
System.out.println("=====================================================");
// List
System.out.println(jedis.lpush("listKey", "l1", "l2", "l3"));
System.out.println(jedis.lrange("listKey", 0, -1)); // [l3, l2, l1]
System.out.println(jedis.llen("listKey"));
System.out.println("=====================================================");
// Hash
System.out.println(jedis.hset("hashKey", "k1", "v1"));
System.out.println(jedis.hset("hashKey", "k2", "v2"));
System.out.println(jedis.hset("hashKey", "k3", "v3"));
System.out.println(jedis.hmget("hashKey", "k1", "k2", "k3")); // [v1, v2, v3]
System.out.println(jedis.hgetAll("hashKey")); // {k3=v3, k2=v2, k1=v1}
System.out.println("=====================================================");
// Set
System.out.println(jedis.sadd("setKey", "s1", "s2", "s3", "s4"));
System.out.println(jedis.smembers("setKey")); // [s2, s1, s4, s3]
System.out.println(jedis.scard("setKey"));
System.out.println("=====================================================");
// ZSet
System.out.println(jedis.zadd("ZKey", 90, "z1"));
System.out.println(jedis.zadd("ZKey", 80, "z2"));
System.out.println(jedis.zadd("ZKey", 85, "z3"));
System.out.println(jedis.zrange("ZKey", 0, -1)); // [z2, z3, z1]
}
}
package com.wlm;
import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
public class TestTx {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "xian_yu");
jsonObject.put("age", "23");
jsonObject.put("sex", "boy");
Transaction multi = jedis.multi(); // 开启事务
String user = jsonObject.toJSONString();
try {
multi.set("user1", user);
multi.set("user2", user);
multi.exec();
} catch (Exception e) {
multi.discard(); // 出现问题,放弃事务
e.printStackTrace();
} finally {
System.out.println(jedis.mget("user1", "user2"));
jedis.close(); // 关闭连接
}
}
}