NoSQL学习:Redis的java方式调用案例指南
1-Redis的Java调用方式
本章介绍通过使用Java方式调用Jedis API,从而来操作Redis进行增删改查操作。
说明:关于Redis的安装和开发环境搭建,详见前面章节。
1. Key操作
代码:
package com.redis.tutorial ;
import redis.clients.jedis.Jedis; public class KeyAPI { private static Jedis redis; static { redis = new Jedis("192.168.32.20", 6379); } public static void main(String[] args) { System.out .println("==============key==============="); // 清空数据 System.out .println("清空库中所有数据:" + redis.flushDB()); ; System.out .println("判断key111是否存在" + redis.exists("key111")); System.out .println("增加key111:" + redis.set("key111", "hello world")); System.out .println("输出key111:" + redis.get("key111")); System.out .println("增加新的key112:" + redis.set("key112", "key112value")); System.out .println("增加新的key113:" + redis.set("key113", "key113value")); // 输出所有key的的值 无序的 for (String key : redis.keys("*")) { System.out .println(key + ":" + redis.get(key)); } System.out .println("删除key111:" + redis.del("key111")); System.out .println("key111是否存在:" + redis.exists("key111")); // 设置key的过期时间 System.out .println("设置key112的过期时间3秒" + redis.expire("key112", 3)); try { Thread.sleep(2000L); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out .println("key112的剩余生存时间:" + redis.ttl("key112")); System.out .println("key112的取消过期时间:" + redis.persist("key112")); System.out .println("查看key112是否还存在:" + redis.exists("key112")); System.out .println("查看key112的value类型:" + redis.type("key112")); System.out .println("给key113改成key114:" + redis.rename("key113", "key114")); System.out .println("查看key114value:" + redis.get("key114")); redis.close(); } }
执行结果
==============key================
清空库中所有数据:OK
判断key111是否存在false
增加key111:OK
输出key111:hello world
增加新的key112:OK
增加新的key113:OK
key113:key113value
key112:key112value
key111:hello world
删除key111:1
key111是否存在:false
设置key112的过期时间3秒1
key112的剩余生存时间:1
key112的取消过期时间:1
查看key112是否还存在:true
查看key112的value类型:string
给key113改成key114:OK
查看key114value:key113value
2. String操作
示例代码:
package com.redis.tutorial ; import redis.clients.jedis.Jedis; public class StringAPI { private static Jedis redis; static { redis = new Jedis("192.168.32.20", 6379); } public static void main(String[] args) { System.out .println("========string==========="); System.out .println("清空数据:"+redis.flushDB()); System.out .println("=========单个增删改========="); System.out .println("增加key001:"+redis.set("key001", "key001value")); System.out .println("修改key001:"+redis.set("key001", "key-update")+" "+redis.get("key001")); System.out .println("最后追加字符串:"+redis.append("key001", "-append")+" "+redis.get("key001")); System.out .println("删除key001:"+redis.del("key001")); System.out .println("=========多个一起添加,删除======="); System.out .println("增减key021,key022,key023:" +redis.mset("key021","key021value","key022", "key022value","key023","key023value")); System.out .println("一次获取多个key值:"+redis.mget("key021","key022","key023")); System.out .println("一次删除多个key:"+redis.del(new String[]{"key021","key022"})); System.out .println("获取key021:"+redis.get("key021")); System.out .println("===========other========="); System.out .println("不在在key时再添加值:"+redis.setnx("key031", "key031value")); System.out .println("不在在key时再添加值:"+redis.setnx("key032", "key032value")); System.out .println("存在在key时再添加值:"+redis.setnx("key032", "key032value_tow")); System.out .println("key031:"+redis.get("key031")); System.out .println("key032值没变:"+redis.get("key032")); System.out .println("添加key033并设置过期时间:"+redis.setex("key033", 2, "key033value")); try { Thread.sleep(3000L); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out .println("获取key033值:"+redis.get("key033")); System.out .println("获取值得子串key032:"+redis.getrange("key032",1, 5)); redis.close(); } }
执行结果:
==============string============
清空数据:OK
============单个增删改=============
增加key001:OK
修改key001:OK key-update
最后追加字符串:17 key-update-append
删除key001:1
============多个一起添加,删除==========
增减key021,key022,key023:OK
一次获取多个key值:[key021value,
key022value, key023value]
一次删除多个key:2
获取key021:null
===========other=======================
不在在key时再添加值:1
不在在key时再添加值:1
存在在key时再添加值:0
key031:key031value
key032值没变:key032value
添加key033并设置过期时间:OK
获取key033值:null
获取值得子串key032:ey032
3. List操作
示例操作代码:
package com.redis.tutorial; import redis.clients.jedis.Jedis; import redis.clients.jedis.SortingParams; public class ListAPI { private static Jedis redis; static { redis = new Jedis("192.168.32.20", 6379); } public static void main(String[] args) { //list存储结构是栈类型的 最后插入的索引是0 System.out .println("==============list=============="); System.out .println("清空数据:"+redis.flushDB()); System.out .println("===========增加============="); System.out .println("添加了字符串list"); System.out .println("listString:"+redis.lpush("listString", "vector")); System.out .println("listString:"+redis.lpush("listString", "vector")); System.out .println("listString:"+redis.lpush("listString", "arraylist")); System.out .println("listString:"+redis.lpush("listString", "hashmap")); System.out .println("listNumber:"+redis.lpush("listNumber", "1")); System.out .println("listNumber:"+redis.lpush("listNumber", "4")); System.out .println("listNumber:"+redis.lpush("listNumber", "3")); System.out .println("listNumber:"+redis.lpush("listNumber", "2")); System.out .println("listString全部元素:"+redis.lrange("listString", 0, -1)); System.out .println("listNumber全部元素:"+redis.lrange("listNumber", 0, -1)); System.out .println(""); System.out .println("============修改================="); System.out .println("对指定下标进行修改:"+redis.lset("listString", -1, "vector-update")); System.out .println("修改后的0下标值:"+redis.lindex("listString", 0)); System.out .println("删除后的元素:"+redis.lrange("listString", 0, -1)); System.out .println(""); System.out .println("==============删除================"); System.out .println("删除指定的元素,重复的删除后添加的:"+redis.lrem("listString", 1, "vector")); System.out .println("删除后的元素:"+redis.lrange("listString", 0, -1)); System.out .println(""); /* * list中存字符串时必须指定参数为alpha,如果不使用SortingParams,而是直接使用sort("list"), * 会出现"ERR One or more scores can't be converted into double" */ SortingParams sortingParameters = new SortingParams(); sortingParameters.alpha();//目测是根据字母顺序排序 System.out .println("排序元素:"+redis.sort("listString",sortingParameters)); System.out .println("排序元素:"+redis.sort("listNumber")); redis.close(); } }
执行结果:
====================list============
清空数据:OK
===========增加=================
添加了字符串list
listString:1
listString:2
listString:3
listString:4
listNumber:1
listNumber:2
listNumber:3
listNumber:4
listString全部元素:[hashmap, arraylist, vector, vector]
listNumber全部元素:[2, 3, 4, 1]
============修改=================
对指定下标进行修改:OK
修改后的0下标值:hashmap
删除后的元素:[hashmap, arraylist, vector, vector-update]
==============删除================
删除指定的元素,重复的删除后添加的:1
删除后的元素:[hashmap, arraylist, vector-update]
排序元素:[arraylist, hashmap, vector-update]
排序元素:[1, 2, 3, 4]
4. Set操作
示例操作代码:
package com.redis.tutorial; import redis.clients.jedis.Jedis; public class SetAPI { private static Jedis redis; static { redis = new Jedis("192.168.32.20", 6379); } public static void main(String[] args) { System.out .println("=========set============"); System.out .println("清空数据:"+redis.flushDB()); System.out .println("============增加=============="); System.out .println("名为sets的集合中添加元素value1"+redis.sadd("sets", "value1")); System.out .println("名为sets的集合中添加元素value2"+redis.sadd("sets", "value2")); System.out .println("名为sets的集合中添加元素value3"+redis.sadd("sets", "value3")); System.out .println("sets所有值:"+redis.smembers("sets")); System.out .println("===============修改============="); System.out .println("无法修改指定的值但是,不能存入相同的值:"+redis.sadd("sets", "value2")); System.out .println("sets:"+redis.smembers("sets")); System.out .println("============查询=================="); for(String value : redis.smembers("sets")){ System.out .println(value); } System.out .println("判断元素是否在集合中:"+redis.sismember("sets", "value2")); System.out .println("==============删除============="); System.out .println("删除指定的值:"+redis.srem("sets", "value2")); System.out .println("元素出栈:"+redis.spop("sets")); System.out .println("sets:"+redis.smembers("sets")); System.out .println(""); System.out .println("==============集合运算============"); System.out .println("名为sets1的集合中添加元素value1"+redis.sadd("sets1", "value1")); System.out .println("名为sets1的集合中添加元素value2"+redis.sadd("sets1", "value2")); System.out .println("名为sets1的集合中添加元素value3"+redis.sadd("sets1", "value3")); System.out .println("名为sets2的集合中添加元素value3"+redis.sadd("sets2", "value3")); System.out .println("名为sets2的集合中添加元素value4"+redis.sadd("sets2", "value4")); System.out .println("名为sets2的集合中添加元素value5"+redis.sadd("sets2", "value5")); System.out .println("sets1所有值:"+redis.smembers("sets1")); System.out .println("sets2所有值:"+redis.smembers("sets2")); System.out .println("sets1和sets2的交集:"+redis.sinter("sets1","sets2")); System.out .println("sets1和sets2的并集:"+redis.sunion("sets1","sets2")); System.out .println("sets1和sets2的差集:"+redis.sdiff("sets1","sets2")); redis.close(); } }
执行结果:
============set============
清空数据:OK
============增加==============
名为sets的集合中添加元素value11
名为sets的集合中添加元素value21
名为sets的集合中添加元素value31
sets所有值:[value1, value3, value2]
===============修改=============
无法修改指定的值但是,不能存入相同的值:0
sets:[value1, value3, value2]
============查询==================
value1
value3
value2
判断元素是否在集合中:true
==============删除=============
删除指定的值:1
元素出栈:value3
sets:[value1]
==============集合运算===========
名为sets1的集合中添加元素value11
名为sets1的集合中添加元素value21
名为sets1的集合中添加元素value31
名为sets2的集合中添加元素value31
名为sets2的集合中添加元素value41
名为sets2的集合中添加元素value51
sets1所有值:[value1, value3, value2]
sets2所有值:[value4, value3, value5]
sets1和sets2的交集:[value3]
sets1和sets2的并集:[value4,
value3, value2, value1, value5]
sets1和sets2的差集:[value2, value1]
5. Hash操作
示例操作代码:
package com.redis.tutorial; import redis.clients.jedis.Jedis; public class HashAPI { private static Jedis redis; static { redis = new Jedis("192.168.32.20", 6379); } public static void main(String[] args) { System.out .println("============hash=========="); System.out .println("清空数据:"+redis.flushDB()); System.out .println("===========增加========"); System.out .println("hashs添加key001=>value001"+redis.hset("hashs", "key001", "value001")); System.out .println("hashs添加key002=>value002"+redis.hset("hashs", "key002", "value002")); System.out .println("hashs添加key003=>value003"+redis.hset("hashs", "key003", "value003")); System.out .println("hashs添加key004=>4L"+redis.hincrBy("hashs", "key004", 4L)); System.out .println("hashs:"+redis.hgetAll("hashs")); System.out .println("=============查询==========="); System.out .println("判断是否存在key001:"+redis.hexists("hashs", "key001")); System.out .println("查找key001的值:"+redis.hget("hashs", "key001")); System.out .println("获得所有集合的key:"+redis.hkeys("hashs")); System.out .println("获得所有集合的value:"+redis.hvals("hashs")); System.out .println("=============修改============="); System.out .println("修改key001值为value001_update:"+redis.hset("hashs", "key001", "value001_update")); System.out .println("修改key004值增加11:"+redis.hincrBy("hashs", "key004", 11L)); System.out .println("hashs:"+redis.hvals("hashs")); System.out .println("=============删除==========="); System.out .println("删除key002:"+redis.hdel("hashs", "key002")); System.out .println("hashs:"+redis.hgetAll("hashs")); redis.close(); } }
执行结果:
================hash=================
清空数据:OK
==================增加================
hashs添加key001=>value0011
hashs添加key002=>value0021
hashs添加key003=>value0031
hashs添加key004=>4L4
hashs:{key004=4, key003=value003,
key002=value002, key001=value001}
===================查询================
判断是否存在key001:true
查找key001的值:value001
获得所有集合的key:[key004, key003, key002, key001]
获得所有集合的value:[value001, value002, value003, 4]
=============修改==========
修改key001值为value001_update:0
修改key004值增加11:15
hashs:[value001_update, value002, value003, 15]
=============删除=======================
删除key002:1
hashs:{key004=15, key003=value003, key001=value001_update}
6. SortSet操作
示例操作代码:
package com.redis.tutorial; import redis.clients.jedis.Jedis; public class SortedSetAPI { private static Jedis redis; static { redis = new Jedis("192.168.32.20", 6379); } public static void main(String[] args) { //有序集合类似 System.out .println("=========SortedSet======"); System.out .println("清空数据:"+redis.flushDB()); System.out .println("==============增加==============="); System.out .println("名为zsets添加数据value1:"+redis.zadd("zsets",1, "value1")); System.out .println("名为zsets添加数据value2:"+redis.zadd("zsets",2, "value2")); System.out .println("名为zsets添加数据value3:"+redis.zadd("zsets",3, "value3")); System.out .println("名为zsets添加数据value4:"+redis.zadd("zsets",4, "value4")); System.out .println("名为zsets添加数据value4:"+redis.zadd("zsets",4, "value4")); System.out .println("zsets:"+redis.zrange("zsets", 0, -1)); System.out .println("===============查询==============="); System.out .println("查找元素的个数:"+redis.zcard("zsets")); System.out .println("查找权重范围内元素的个数:"+redis.zcount("zsets",1,3)); System.out .println("查找元素value4的权重:"+redis.zscore("zsets","value4")); System.out .println("=============修改==================="); System.out .println("=============删除==================="); System.out .println("删除value4:"+redis.zrem("zsets", "value4")); System.out .println("redis:"+redis.zrange("zsets", 0, -1)); redis.close(); } }
执行结果:
============SortedSet========
清空数据:OK
==============增加===============
名为zsets添加数据value1:1
名为zsets添加数据value2:1
名为zsets添加数据value3:1
名为zsets添加数据value4:1
名为zsets添加数据value4:0
zsets:[value1, value2, value3, value4]
===============查询===============
查找元素的个数:4
查找权重范围内元素的个数:3
查找元素value4的权重:4.0
=============修改===================
=============删除===================
删除value4:1
redis:[value1, value2, value3]