知识点14:Jedis:使用方式与Jedis依赖
目标:掌握Redis的使用方式及构建Jedis工程依赖
路径
- step1:Redis的使用方式
- step2:Jedis依赖
实施
Redis的使用方式
命令操作Redis,一般用于测试开发阶段
分布式计算或者Java程序读写Redis,一般用于实际生产开发
Spark/Flink读写Redis
所有数据库使用Java操作方式整体是类似的
代码语言:javascript复制//todo:1-构建客户端连接对象
Connection conn = DriverManager.getConnect(url,username,password)
//todo:2-执行操作:所有操作都在客户端连接对象中:方法
prep.execute(SQL)
//todo:3-释放连接
conn.close
Jedis依赖
- 参考附录一添加依赖
小结
- 掌握Redis的使用方式及构建Jedis工程依赖
知识点15:Jedis:构建连接
目标:实现Jedis的客户端连接
实施
代码语言:javascript复制 //todo:1-构建连接对象
Jedis jedis = null;
@Before
public void getConnection(){
//方式一:直接构建Jedis对象
// jedis = new Jedis("node1",6379);
//方式二:通过连接池构建Jedis
//构建连接池配置对象
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10);//总连接构建
config.setMaxIdle(5);//最大空闲连接
config.setMinIdle(2);//最小空闲连接
//构建连接池对象
JedisPool jedisPool = new JedisPool(config,"node1",6379);
//获取连接
jedis = jedisPool.getResource();
}
//todo:2-调用连接对象的方法实现操作
//todo:3-释放连接
@After
public void closeConnection(){
jedis.close();
}
小结
- 实现Jedis的客户端连接
知识点16:Jedis:String操作
目标:Jedis中实现String的操作
实施
代码语言:javascript复制set/get/incr/exists/expire/setexp/ttl
代码语言:javascript复制 @Test
public void testString(){
//set/get/incr/exists/expire/setexp/ttl
// jedis.set("s1","hadoop");
// System.out.println(jedis.exists("s1"));
// System.out.println(jedis.exists("s2"));
// System.out.println(jedis.get("s1"));
// jedis.set("s2","3");
// jedis.incr("s2");
// System.out.println(jedis.get("s2"));
// jedis.expire("s2",10);
// while(true){
// System.out.println(jedis.ttl("s2"));
// }
//setex:构建KV时,直接设置生命周期
jedis.setex("s3",10,"oozie");
}
小结
- Jedis中实现String的操作
知识点17:Jedis:其他类型操作
目标:Jedis中实现其他类型操作
实施
Hash类型
代码语言:javascript复制hset/hmset/hget/hgetall/hdel/hlen/hexists
代码语言:javascript复制 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"));
}
List类型
代码语言:javascript复制lpush/rpush/lrange/llen/lpop/rpop
代码语言:javascript复制 @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));
}
Set类型
代码语言:javascript复制sadd/smembers/sismember/scard/srem
代码语言:javascript复制 @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"));
}
Zset类型
代码语言:javascript复制zadd/zrange/zrevrange/zcard/zrem
代码语言:javascript复制 @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));
}
小结
- Jedis中实现其他类型操作
后记