Jedis基本用法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jedis基本用法相关的知识,希望对你有一定的参考价值。
Redis(Remote Dictionary Server,远程数据字典服务器)是一个开源的高性能内存数据库,常用作缓存缓存服务器使用,也已做消息队列使用。因其高性能、丰富的数据类型、可扩展等特性受开发者青睐,这里介绍在java中使用Jedis操作Redis的基本用法。
1. 字符串String。
package com.zws.redis; import java.util.concurrent.TimeUnit; import redis.clients.jedis.Jedis; public class RedisString { public static void main(String[] args) throws InterruptedException { String host = "192.168.137.131"; int port = 6379; String password = "redis"; Jedis jedis = new Jedis(host, port); jedis.auth(password); jedis.ping(); jedis.set("name", "张三"); //设置 jedis.set("age", "23"); String name = jedis.get("name");//获取 String age = jedis.get("age"); System.out.println("name:" + name + ",age:" + age); jedis.set("name", "李四"); //覆盖 name = jedis.get("name"); System.out.println("name:" + name); jedis.append("name", "-尼古拉斯"); //追加 name = jedis.get("name");//获取 age = jedis.get("age"); System.out.println("name:" + name + ",age:" + age); jedis.del("name"); //删除 name = jedis.get("name"); System.out.println("name:" + name); jedis.del("name", "age"); //删除多个key name = jedis.get("name");//获取 age = jedis.get("age"); System.out.println("name:" + name + ",age:" + age); 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("key1:" + jedis.get("key1")); System.out.println("key2:" + jedis.get("key2")); System.out.println("===========新增键值对并设置有效时间============="); System.out.println(jedis.setex("key3", 2, "value3")); System.out.println(jedis.get("key3")); TimeUnit.SECONDS.sleep(3); System.out.println("key3:" + jedis.get("key3")); System.out.println("===========获取原值,更新为新值=========="); //GETSET is an atomic set this value and return the old value command. System.out.println(jedis.getSet("key2", "key2GetSet")); System.out.println("key2:" + jedis.get("key2")); System.out.println("截取key2的值:" + jedis.getrange("key2", 2, 4)); jedis.close(); } }
2. 集合List。
package com.zws.redis; import java.util.List; import redis.clients.jedis.Jedis; public class RedisList { public static void main(String[] args) { String host = "192.168.137.131"; int port = 6379; Jedis jedis = new Jedis(host, port); jedis.auth("redis"); jedis.ping(); System.out.println("===========添加一个list==========="); Long cnt = jedis.del("collections"); System.out.println("del cnt:" + cnt); jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap"); jedis.lpush("collections", "HashSet"); jedis.lpush("collections", "TreeSet"); jedis.lpush("collections", "TreeMap"); jedis.lpush("collections", "BrrayList"); jedis.lpush("collections", "CrrayList"); jedis.lpush("collections", "CrrayList"); List<String> collections = jedis.lrange("collections", 0, -1); System.out.println("collections的内容:" + collections);//-1代表倒数第一个元素,-2代表倒数第二个元素, 倒序出来 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.del("sortedList"); jedis.lpush("sortedList", "3","6","2","0","7","4"); System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1)); System.out.println("sortedList返回有序:" + jedis.sort("sortedList")); System.out.println("sortedList排序后:"+jedis.lrange("sortedList", 0, -1)); jedis.close(); //Redis中还有阻塞式的列表弹出命令以及在列表之间移动元素的命令:blpop, brpop, rpoplpush, brpoplpush等。 } }
3. 散列HashMap。
package com.zws.redis; import java.util.HashMap; import java.util.Map; import redis.clients.jedis.Jedis; public class RedisHashMap { public static void main(String[] args) throws InterruptedException { String host = "192.168.137.131"; int port = 6379; String password = "redis"; Jedis jedis = new Jedis(host, port); jedis.auth(password); jedis.ping(); jedis.del("hash"); Map<String,String> map = new HashMap<String, String>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); map.put("key4","value4"); jedis.hmset("hash",map); jedis.hset("hash", "key5", "value5"); System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash")); System.out.println("散列hash的所有键为:"+jedis.hkeys("hash")); 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")); jedis.close(); } }
4. Set。
package com.zws.redis; import redis.clients.jedis.Jedis; public class RedisSet { public static void main(String[] args) throws InterruptedException { String host = "192.168.137.131"; int port = 6379; String password = "redis"; Jedis jedis = new Jedis(host, port); jedis.auth(password); jedis.ping(); jedis.del("eleSet"); 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("e2是否在eleSet中:" + jedis.sismember("eleSet", "e2")); 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")); System.out.println("eleSet1和eleSet2的差集:" + jedis.sdiff("eleSet1", "eleSet2"));// eleSet1中有,eleSet2中没有 //关于Set还有一些其他命令:srandmember, sdiffstore, sinterstore, sunionstore等。 jedis.close(); } }
5. 排序集合SortedSet.
package com.zws.redis; import java.util.HashMap; import java.util.Map; import redis.clients.jedis.Jedis; public class RedisSortedSet { public static void main(String[] args) { String host = "192.168.137.131"; int port = 6379; Jedis jedis = new Jedis(host, port); jedis.auth("redis"); jedis.ping(); jedis.del("englishScore"); Map<String, Double> englishScore = new HashMap<String, Double>(); englishScore.put("熊大", 56D); englishScore.put("熊二", 67.9D); englishScore.put("张三", 90D); englishScore.put("李四", 60D); englishScore.put("王五", 40D); System.out.println(jedis.zadd("englishScore", 39D,"赵六")); System.out.println(jedis.zadd("englishScore", englishScore)); System.out.println("englishScore中的所有元素:"+jedis.zrange("englishScore", 0, -1)); System.out.println("englishScore中的所有元素:"+jedis.zrangeWithScores("englishScore", 0, -1)); System.out.println("englishScore中的所有元素:"+jedis.zrangeByScore("englishScore", 0,100)); System.out.println("englishScore中的所有元素:"+jedis.zrangeByScoreWithScores("englishScore", 0,100)); System.out.println("englishScore中张三的分值:"+jedis.zscore("englishScore", "张三")); System.out.println("englishScore中张三的排名:"+jedis.zrank("englishScore", "张三")); System.out.println("删除englishScore中的元素张三:"+jedis.zrem("englishScore", "张三")); System.out.println("englishScore中的所有元素:"+jedis.zrange("englishScore", 0, -1)); System.out.println("englishScore中元素的个数:"+jedis.zcard("englishScore")); System.out.println("englishScore中分值在60-100之间的元素的个数:"+jedis.zcount("englishScore", 60, 100)); System.out.println("熊大的分值加上5:"+jedis.zincrby("englishScore", 5, "熊大")); System.out.println("熊二的分值加上4:"+jedis.zincrby("englishScore", 4, "熊二")); System.out.println("englishScore中的所有元素:"+jedis.zrange("englishScore", 0, -1)); jedis.close(); } }
本文出自 “埃文” 博客,请务必保留此出处http://wenshengzhu.blog.51cto.com/5151285/1912936
以上是关于Jedis基本用法的主要内容,如果未能解决你的问题,请参考以下文章
Redis使用 Jedis 操作 Redis 数据库 ② ( Jedis API 规律 | Redis 命令与 Jedis 函数名称基本一致 | Jedis API 使用示例 )