Java对Redis数据库的常用操作
下面代码是Java对redis数据库的常用的Api需要用到Redis的库。
Maven设置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itcast</groupId>
<artifactId>redis_op</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<!-- <verbal>true</verbal>-->
</configuration>
</plugin>
</plugins>
</build>
</project>
Demo代码
下面是对Redis常用数据类型的操作,分别是String,Hash列表,List,Set集合,Zset有序集合。
package bigdata.itcast.cn.jedis;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.*;
import java.util.*;
/**
* @ClassName RedisClientTest
* @Description TODO 开发Jedis客户端,通过Java代码操作Redis
* @Create By Frank
*/
public class RedisClientTest {
//todo:1-构建客户端连接对象
//单节点连接对象:Jedis
Jedis jedis = null;
@Before
//用于给Jedis对象赋值,每次test之前先执行
public void getConnection(){
//方式一:直接构建
// jedis = new Jedis("node1",6379);
//方式二:基于连接池来获取连接
//构建连接池配置对象
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10);//最大连接数
config.setMaxIdle(5);
config.setMinIdle(3);
//构建连接池对象
JedisPool jedisPool = new JedisPool(config,"node1",6379);
//从池子中获取连接对象
jedis = jedisPool.getResource();
}
//todo:2-执行操作
@Test
public void testString(){
//set/get/incr/exists/expire/setex/ttl
// jedis.set("s1","hadoop");
// System.out.println(jedis.get("s1"));
// jedis.set("s2","5");
// jedis.incr("s2");
// System.out.println(jedis.get("s2"));
// jedis.expire("s2",15);
// while (true){
// System.out.println(jedis.ttl("s2"));
// }
// System.out.println(jedis.exists("s2"));
// System.out.println(jedis.exists("s1"));
//setex = set + expire
jedis.setex("s2",10,"hadoop");
}
@Test
public void testHash(){
//hset/hmset/hget/hgetall/hdel/hlen/hexists
// jedis.hset("m1","name","zhangsan");
// System.out.println(jedis.hget("m1","name"));
// Map<String,String> maps = new HashMap<>();
// maps.put("age","18");
// maps.put("phone","110");
// jedis.hmset("m1",maps);
// List<String> hmget = jedis.hmget("m1", "name", "age");
// System.out.println(hmget);
// System.out.println("=");
// Map<String, String> m1 = jedis.hgetAll("m1");
// for(Map.Entry map : m1.entrySet()){
// System.out.println(map.getKey()+"\t"+map.getValue());
// }
// System.out.println("=");
System.out.println(jedis.hlen("m1"));
jedis.hdel("m1","name");
System.out.println(jedis.hlen("m1"));
System.out.println(jedis.hexists("m1","name"));
System.out.println(jedis.hexists("m1","age"));
}
@Test
public void testList(){
//lpush/rpush/lrange/llen/lpop/rpop
jedis.lpush("list1","1","2","3");
System.out.println(jedis.lrange("list1",0,-1));
jedis.rpush("list1","4","5","6");
System.out.println(jedis.lrange("list1",0,-1));
System.out.println(jedis.llen("list1"));
jedis.lpop("list1");
jedis.rpop("list1");
System.out.println(jedis.lrange("list1",0,-1));
}
@Test
public void testSet(){
//sadd/smembers/sismember/scard/srem
jedis.sadd("set1","1","2","3","1","2","3","4","5","6");
System.out.println("长度:"+jedis.scard("set1"));
System.out.println("内容:"+jedis.smembers("set1"));
System.out.println(jedis.sismember("set1","1"));
System.out.println(jedis.sismember("set1","7"));
jedis.srem("set1","2");
System.out.println("内容:"+jedis.smembers("set1"));
}
@Test
public void testZset(){
//zadd/zrange/zrevrange/zcard/zrem
jedis.zadd("zset1",20.9,"yuwen");
jedis.zadd("zset1",10.5,"yinyu");
jedis.zadd("zset1",70.9,"shuxue");
jedis.zadd("zset1",99.9,"shengwu");
Set<String> zset1 = jedis.zrange("zset1", 0, -1);
System.out.println(zset1);
System.out.println(jedis.zrevrange("zset1",0,-1));
System.out.println(jedis.zcard("zset1"));
jedis.zrem("zset1","yuwen");
System.out.println(jedis.zrangeWithScores("zset1",0,-1));
}
//todo:3-释放连接
@After
//释放连接
public void closeConnection(){
jedis.close();
}
}