什么是jedis?
简而言之:jedis就是通过redis的API对redis的操作,通过Java类对数据库进行操作,注意:使用的命令则是redis数据库的API,jedis是一个java实现的redis客户端连接工具。常用的还有redisson,jedis跟接近于原生的操作,而redisson跟适合用于分布式,提供了分布式锁,以及其他多种数据结构。
连接redis数据库
注意:本篇文章讲解的主要为学习方法,所以使用的数据库使用的是本地环境下的数据库
环境则是windows下的redis,如果只是抱着学习为目的,可以下载redis到本地环境下
Releases · tporadowski/redis · GitHub windows环境下的redis下载
打开redis数据库
可以通过客户端连接进行测试
返回值为pong则数据库连接成功
导入依赖:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.3.2</version>
</dependency>
实现jedis的连接
import redis.clients.jedis.Jedis;
import java.util.Set;
public class connect_Redis {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379); //127.0.0.1为本地ip,6379则是端口号
jedis.flushAll();
System.out.println(jedis.ping()); //测试连接,如果出现pong则连接成功
}
}
运行结果:连接成功
redis API在Java的操作:其实在redis数据库的操作命令和功能相同,不懂得可以看这篇文章
Redis之五大基本的数据类型:字符串String 散列hashes 列表 lists 集合sets 有序集合sorted sets 基础命令讲解_不想睡醒的梦的博客-CSDN博客
1:key
import redis.clients.jedis.Jedis;
import java.util.concurrent.TimeUnit;
public class test1 {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.flushDB();
System.out.println("===========增加数据===========");
System.out.println(jedis.set("key1", "value1"));
System.out.println(jedis.set("key2", "value2"));
System.out.println(jedis.set("key3", "value3"));
System.out.println(jedis.del("key2")); //"删除键key2:"
System.out.println(jedis.get("key2")); //"获取键key2:"
System.out.println(jedis.set("key1", "value1Changed")); //"修改key1:" +
System.out.println("获取key1的值:" + jedis.get("key1")); //获取key1值
System.out.println(jedis.append("key3", "End")); //在key3后面加入值
System.out.println(jedis.get("key3")); //key3的值:
//"增加多个键值对:"
System.out.println(jedis.mset("key01", "value01", "key02", "value02", "key03", "value03"));
// "获取多个键值对"
System.out.println( jedis.mget("key01", "key02", "key03"));
// "获取多个键值对"
System.out.println(jedis.mget("key01", "key02", "key03", "key04"));
// 删除多个键值对
System.out.println(jedis.del("key01", "key02"));
// 获取多个键值对
System.out.println(jedis.mget("key01", "key02", "key03"));
jedis.flushDB();
System.out.println("===========新增键值对防止覆盖原先值==============");
System.out.println(jedis.setnx("key1", "value1"));
System.out.println(jedis.setnx("key2", "value2"));
System.out.println(jedis.setnx("key2", "value2-new"));
System.out.println(jedis.get("key1"));
System.out.println(jedis.get("key2"));
System.out.println("===========新增键值对并设置有效时间=============");
System.out.println(jedis.setex("key3", 2, "value3"));
System.out.println(jedis.get("key3"));
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(jedis.get("key3"));
System.out.println("===========获取原值,更新为新值==========");
System.out.println(jedis.getSet("key2", "key2GetSet"));
System.out.println(jedis.get("key2"));
System.out.println("获得key2的值的字串:" + jedis.getrange("key2", 2, 4));
}
}
输出运行结果
2:字符串
import redis.clients.jedis.Jedis;
import java.util.concurrent.TimeUnit;
public class test2 {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("===========增加数据===========");
System.out.println(jedis.set("key1", "value1"));
System.out.println(jedis.set("key2", "value2"));
System.out.println(jedis.set("key3", "value3"));
System.out.println("删除键key2:" + jedis.del("key2"));
System.out.println("获取键key2:" + jedis.get("key2"));
System.out.println("修改key1:" + jedis.set("key1", "value1Changed"));
System.out.println("获取key1的值:" + jedis.get("key1"));
System.out.println("在key3后面加入值:" + jedis.append("key3", "End"));
System.out.println("key3的值:" + jedis.get("key3"));
System.out.println("增加多个键值对:" + jedis.mset("key01", "value01", "key02", "value02", "key03", "value03"));
System.out.println("获取多个键值对:" + jedis.mget("key01", "key02", "key03"));
System.out.println("获取多个键值对:" + jedis.mget("key01", "key02", "key03", "key04"));
System.out.println("删除多个键值对:" + jedis.del("key01", "key02"));
System.out.println("获取多个键值对:" + jedis.mget("key01", "key02", "key03"));
jedis.flushDB();
System.out.println("===========新增键值对防止覆盖原先值==============");
System.out.println(jedis.setnx("key1", "value1"));
System.out.println(jedis.setnx("key2", "value2"));
System.out.println(jedis.setnx("key2", "value2-new"));
System.out.println(jedis.get("key1"));
System.out.println(jedis.get("key2"));
System.out.println("===========新增键值对并设置有效时间=============");
System.out.println(jedis.setex("key3", 2, "value3"));
System.out.println(jedis.get("key3"));
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(jedis.get("key3"));
System.out.println("===========获取原值,更新为新值==========");
System.out.println(jedis.getSet("key2", "key2GetSet"));
System.out.println(jedis.get("key2"));
System.out.println("获得key2的值的字串:" + jedis.getrange("key2", 2, 4));
}
}
3. list集合
import redis.clients.jedis.Jedis;
import java.util.concurrent.TimeUnit;
public class test3 {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("===========添加一个list===========");
jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
jedis.lpush("collections", "HashSet");
jedis.lpush("collections", "TreeSet");
jedis.lpush("collections", "TreeMap");
System.out.println("collections的内容:" + jedis.lrange("collections", 0, -1));//-1代表倒数第一个元素,-2代表倒数第二个元素,end为-1表示查询全部
System.out.println("collections区间0-3的元素:" + jedis.lrange("collections", 0, 3));
System.out.println("===============================");
// 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
System.out.println("删除指定元素个数:" + jedis.lrem("collections", 2, "HashMap"));
System.out.println("collections的内容:" + jedis.lrange("collections", 0, -1));
System.out.println("删除下表0-3区间之外的元素:" + jedis.ltrim("collections", 0, 3));
System.out.println("collections的内容:" + jedis.lrange("collections", 0, -1));
System.out.println("collections列表出栈(左端):" + jedis.lpop("collections"));
System.out.println("collections的内容:" + jedis.lrange("collections", 0, -1));
System.out.println("collections添加元素,从列表右端,与lpush相对应:" + jedis.rpush("collections", "EnumMap"));
System.out.println("collections的内容:" + jedis.lrange("collections", 0, -1));
System.out.println("collections列表出栈(右端):" + jedis.rpop("collections"));
System.out.println("collections的内容:" + jedis.lrange("collections", 0, -1));
System.out.println("修改collections指定下标1的内容:" + jedis.lset("collections", 1, "LinkedArrayList"));
System.out.println("collections的内容:" + jedis.lrange("collections", 0, -1));
System.out.println("===============================");
System.out.println("collections的长度:" + jedis.llen("collections"));
System.out.println("获取collections下标为2的元素:" + jedis.lindex("collections", 2));
System.out.println("===============================");
jedis.lpush("sortedList", "3", "6", "2", "0", "7", "4");
System.out.println("sortedList排序前:" + jedis.lrange("sortedList", 0, -1));
System.out.println(jedis.sort("sortedList"));
System.out.println("sortedList排序后:" + jedis.lrange("sortedList", 0, -1));
}
}
4.set集合
import redis.clients.jedis.Jedis;
import java.util.concurrent.TimeUnit;
public class test3 {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
System.out.println("============向集合中添加元素(不重复)============");
System.out.println(jedis.sadd("eleSet", "e1", "e2", "e4", "e3", "e0", "e8", "e7", "e5"));
System.out.println(jedis.sadd("eleSet", "e6"));
System.out.println(jedis.sadd("eleSet", "e6"));
System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
System.out.println("删除一个元素e0:" + jedis.srem("eleSet", "e0"));
System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
System.out.println("删除两个元素e7和e6:" + jedis.srem("eleSet", "e7", "e6"));
System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
System.out.println("随机的移除集合中的一个元素:" + jedis.spop("eleSet"));
System.out.println("随机的移除集合中的一个元素:" + jedis.spop("eleSet"));
System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
System.out.println("eleSet中包含元素的个数:" + jedis.scard("eleSet"));
System.out.println("e3是否在eleSet中:" + jedis.sismember("eleSet", "e3"));
System.out.println("e1是否在eleSet中:" + jedis.sismember("eleSet", "e1"));
System.out.println("e1是否在eleSet中:" + jedis.sismember("eleSet", "e5"));
System.out.println("=================================");
System.out.println(jedis.sadd("eleSet1", "e1", "e2", "e4", "e3", "e0", "e8", "e7", "e5"));
System.out.println(jedis.sadd("eleSet2", "e1", "e2", "e4", "e3", "e0", "e8"));
//移到集合元素
System.out.println("将eleSet1中删除e1并存入eleSet3中:" + jedis.smove("eleSet1", "eleSet3", "e1"));
System.out.println("将eleSet1中删除e2并存入eleSet3中:" + jedis.smove("eleSet1", "eleSet3", "e2"));
System.out.println("eleSet1中的元素:" + jedis.smembers("eleSet1"));
System.out.println("eleSet3中的元素:" + jedis.smembers("eleSet3"));
System.out.println("============集合运算=================");
System.out.println("eleSet1中的元素:" + jedis.smembers("eleSet1"));
System.out.println("eleSet2中的元素:" + jedis.smembers("eleSet2"));
System.out.println("eleSet1和eleSet2的交集:" + jedis.sinter("eleSet1", "eleSet2"));
System.out.println("eleSet1和eleSet2的并集:" + jedis.sunion("eleSet1", "eleSet2"));
//eleSet1中有,eleSet2中没有
System.out.println("eleSet1和eleSet2的差集:" + jedis.sdiff("eleSet1", "eleSet2"));
//求交集并将交集保存到dstkey的集合
jedis.sinterstore("eleSet4", "eleSet1", "eleSet2");
System.out.println("eleSet4中的元素:" + jedis.smembers("eleSet4"));
}
}
5. Hash集合
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class test3 {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.flushDB();
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
// 添加名称为 hash(key)的 hash 元素
jedis.hmset("hash", map);
// 向名称为 hash 的 hash 中添加 key 为 key5,value 为 value5 元素
jedis.hset("hash", "key5", "value5");
// return Map<String,String>
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll(" hash"));
// return Set<String>
System.out.println("散列hash的所有键为:" + jedis.hkeys("hash"));
// return List<String>
System.out.println("散列hash的所有值为:" + jedis.hvals("hash"));
System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:" + jedis.hincrBy(" hash", " key6", 6));
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:" + jedis.hincrBy(" hash", " key6", 3));
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
System.out.println("删除一个或者多个键值对:" + jedis.hdel("hash", "key2"));
System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
System.out.println("散列hash中键值对的个数:" + jedis.hlen("hash"));
System.out.println("判断hash中是否存在key2:" + jedis.hexists(" hash", " key2"));
System.out.println("判断hash中是否存在key3:" + jedis.hexists(" hash", " key3"));
System.out.println("获取hash中的值:" + jedis.hmget("hash", "key3"));
System.out.println("获取hash中的值:" + jedis.hmget(" hash", " key3", " key4"));
}
}
事务操作
没有概念的可以通过这篇文章去了解Redis:事务操作以及监控(悲观锁,乐观锁)_不想睡醒的梦的博客-CSDN博客
队列中的问题没有问题的情况下,代码正常的执行
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
import java.util.HashMap;
import java.util.Map;
public class connect_Redis2 {
public static void main(String[] args) throws Exception{
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.flushAll();
JSONObject jsonObject = new JSONObject();
Map<String,String> map = new HashMap<>();
map.put("demo1","dmeo1");
map.put("demo2","demo2");
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
Transaction mutil =jedis.multi(); //开启事务
try{ //尝试执行,用trycatchfinally语句来实现
mutil.set("json","json");
mutil.set("json2",s);
mutil.exec();
}
catch (Exception e){
e.printStackTrace();
mutil.discard();
}
finally {
System.out.println(jedis.get("json"));
System.out.println(jedis.get("json2"));
}
}
}
队列中命令出现问题时:
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
import java.util.HashMap;
import java.util.Map;
public class connect_Redis2 {
public static void main(String[] args) throws Exception{
Jedis jedis = new Jedis("127.0.0.1",6379);
jedis.flushAll();
JSONObject jsonObject = new JSONObject();
Map<String,String> map = new HashMap<>();
map.put("demo1","dmeo1");
map.put("demo2","demo2");
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
jsonObject.put("name","myc");
jsonObject.put("sex","man");
Transaction mutil =jedis.multi();
try{
mutil.set("json","json");
int i = 5/0; //模拟问题的出现
mutil.set("json2",s);
mutil.exec();
}
catch (Exception e){
e.printStackTrace();
mutil.discard();
}
finally {
System.out.println(jedis.get("json"));
System.out.println(jedis.get("json2"));
}
}
}
事务队列中的命令没有被执行