Jedis的常用操作
转载至:
https://blog.csdn.net/qq_27026603/article/details/81865604
一、jedis
jedis 是 redis推荐的java客户端。通过Jedis我们可以很方便地使用java代码的方式,对redis进行操作。jedis使用起来比较简单,它的操作方法与redis命令相类似。对于初次使用redis的人来说,上手更快,更能适应。jedis在github上的下载地址为https://github.com/xetorthio/jedis 。本例子使用maven,需要添加如下依赖:
redis.clients jedis 2.6.0 二、相关实例 以下只是对数据的一些基本操作。 获取Jedis操作对象
Jedis jedis;
@Before
public void connectionTest() {
jedis = new Jedis(“127.0.0.1”, 6379);//redis的地址以及连接端口
//jedis.auth(“helloworld”); //开启密码验证(配置文件中为 requirepass helloworld)的时候需要执行该方法
}
Jedis对key的操作
@Test
public void keyTest() throws UnsupportedEncodingException {
System.out.println(jedis.flushDB());// 清空数据
System.out.println(jedis.echo(“hello”));
// 判断key否存在
System.out.println(jedis.exists(“foo”));
jedis.set(“key”, “values”);
jedis.set(“key2”, “values”);
System.out.println(jedis.exists(“key”));// 判断是否存在
// 如果数据库没有任何key,返回nil,否则返回数据库中一个随机的key。
String randomKey = jedis.randomKey();
System.out.println("randomKey: " + randomKey);
// 设置60秒后该key过期
jedis.expire(“key”, 60);
// key有效毫秒数
System.out.println(jedis.pttl(“key”));
// 移除key的过期时间
jedis.persist(“key”);
// 获取key的类型, “string”, “list”, “set”. “none” none表示key不存在
System.out.println("type: " + jedis.type(“key”));
// 导出key的值
byte[] bytes = jedis.dump(“key”);
System.out.println(new String(bytes));
// 将key重命名
jedis.renamenx(“key”, “keytest”);
System.out.println("key是否存在: " + jedis.exists(“key”));// 判断是否存在
System.out.println("keytest是否存在: " + jedis.exists(“keytest”));// 判断是否存在
// 查询匹配的key
// KEYS * 匹配数据库中所有 key 。
// KEYS h?llo 匹配 hello , hallo 和 hxllo 等。
// KEYS hllo 匹配 hllo 和 heeeeello 等。
// KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。
// 特殊符号用 \ 隔开。
Set set = jedis.keys("k");
System.out.println(set);
// 删除key
jedis.del(“key”);
System.out.println(jedis.exists(“key”));
}
Jedis对字符串(String)的相关操作
@Test
public void stringTest() {
jedis.set(“hello”, “hello”);
System.out.println(jedis.get(“hello”));
// 使用append 向字符串后面添加
jedis.append(“hello”, " world");
System.out.println(jedis.get(“hello”));
// set覆盖字符串
jedis.set(“hello”, “123”);
System.out.println(jedis.get(“hello”));
// 设置过期时间
jedis.setex(“hello2”, 2, “world2”);
System.out.println(jedis.get(“hello2”));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
System.out.println(jedis.get(“hello2”));
// 一次添加多个key-value对
jedis.mset(“a”, “1”, “b”, “2”);
// 获取a和b的value
List valus = jedis.mget(“a”, “b”);
System.out.println(valus);
// 批量删除
jedis.del(“a”, “b”);
System.out.println(jedis.exists(“a”));
System.out.println(jedis.exists(“b”));
}
Jedis对链表(Lists)的操作
@Test
public void listTest() {
String key = “mylist”;
jedis.del(key);
// 队列添加元素
jedis.rpush(key, “aaaa”);
jedis.rpush(key, “aaaa”);
jedis.rpush(key, “bbbb”);
jedis.rpush(key, “cccc”);
jedis.rpush(key, “cccc”);
// 队列长度
System.out.println("lenth: " + jedis.llen(key));
// 打印队列,从索引0开始,到倒数第1个(全部元素)
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
// 索引为1的元素
System.out.println("index of 1: " + jedis.lindex(key, 1));
// 设置队列里面一个元素的值,当index超出范围时会返回一个error。
jedis.lset(key, 1, “aa22”);
System.out.println("index of 1: " + jedis.lindex(key, 1));
// 从队列的右边入队一个元素
jedis.rpush(key, “-2”, “-1”);// 先-2,后-1入队列
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
// 从队列的左边入队一个或多个元素
jedis.lpush(key, “second element”, “first element”);// 先second
// element,后first
// elementF入队列
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
// 从队列的右边出队一个元素
System.out.println(jedis.rpop(key));
// 从队列的左边出队一个元素
System.out.println(jedis.lpop(key));
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
// count > 0: 从头往尾移除值为 value 的元素,count为移除的个数。
// count < 0: 从尾往头移除值为 value 的元素,count为移除的个数。
// count = 0: 移除所有值为 value 的元素。
jedis.lrem(key, 1, “cccc”);
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
// 即最右边的那个元素也会被包含在内。 如果start比list的尾部下标大的时候,会返回一个空列表。
// 如果stop比list的实际尾部大的时候,Redis会当它是最后一个元素的下标。
System.out.println(jedis.lrange(key, 0, 2));
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
// 删除区间以外的元素
System.out.println(jedis.ltrim(key, 0, 2));
System.out.println("all elements: " + jedis.lrange(key, 0, -1));
}
Jedis对集合(Sets)的操作
@Test
public void testSet() {
// 清空数据
System.out.println(jedis.flushDB());
String key = “myset”;
String key2 = “myset2”;
// 集合添加元素
jedis.sadd(key, “aaa”, “bbb”, “ccc”);
jedis.sadd(key2, “bbb”, “ccc”, “ddd”);
// 获取集合里面的元素数量
System.out.println(jedis.scard(key));
// 获得两个集合的交集,并存储在一个关键的结果集
jedis.sinterstore(“destination”, key, key2);
System.out.println(jedis.smembers(“destination”));
// 获得两个集合的并集,并存储在一个关键的结果集
jedis.sunionstore(“destination”, key, key2);
System.out.println(jedis.smembers(“destination”));
// key集合中,key2集合没有的元素,并存储在一个关键的结果集
jedis.sdiffstore(“destination”, key, key2);
System.out.println(jedis.smembers(“destination”));
// 确定某个元素是一个集合的成员
System.out.println(jedis.sismember(key, “aaa”));
// 从key集合里面随机获取一个元素
System.out.println(jedis.srandmember(key));
// aaa从key移动到key2集合
jedis.smove(key, key2, “aaa”);
System.out.println(jedis.smembers(key));
System.out.println(jedis.smembers(key2));
// 删除并获取一个集合里面的元素
System.out.println(jedis.spop(key));
// 从集合里删除一个或多个元素
jedis.srem(key2, “ccc”, “ddd”);
System.out.println(jedis.smembers(key2));
}
Jedis对有序集合(Sorted Sets)的操作
@Test
public void testSortSet() {
// 清空数据
System.out.println(jedis.flushDB());
String key = “mysortset”;
Map<String, Double> scoreMembers = new HashMap<String, Double>();
scoreMembers.put(“aaa”, 1001.0);
scoreMembers.put(“bbb”, 1002.0);
scoreMembers.put(“ccc”, 1003.0);
// 添加数据
jedis.zadd(key, 1004.0, “ddd”);
jedis.zadd(key, scoreMembers);
// 获取一个排序的集合中的成员数量
System.out.println(jedis.zcard(key));
// 返回的成员在指定范围内的有序集合,以0表示有序集第一个成员,以1表示有序集第二个成员,以此类推。
// 负数下标,以-1表示最后一个成员,-2表示倒数第二个成员
Set coll = jedis.zrange(key, 0, -1);
System.out.println(coll);
// 返回的成员在指定范围内的逆序集合
coll = jedis.zrevrange(key, 0, -1);
System.out.println(coll);
// 元素下标
System.out.println(jedis.zscore(key, “bbb”));
// 删除元素
System.out.println(jedis.zrem(key, “aaa”));
System.out.println(jedis.zrange(key, 0, -1));
// 给定值范围内的成员数
System.out.println(jedis.zcount(key, 1002.0, 1003.0));
}
Jedis对哈希(Hashs)的操作
@Test
public void testHash() {
// 清空数据
System.out.println(jedis.flushDB());
String key = “myhash”;
Map<String, String> hash = new HashMap<String, String>();
hash.put(“aaa”, “11”);
hash.put(“bbb”, “22”);
hash.put(“ccc”, “33”);
// 添加数据
jedis.hmset(key, hash);
jedis.hset(key, “ddd”, “44”);
// 获取hash的所有元素(key值)
System.out.println(jedis.hkeys(key));
// 获取hash中所有的key对应的value值
System.out.println(jedis.hvals(key));
// 获取hash里所有元素的数量
System.out.println(jedis.hlen(key));
// 获取hash中全部的域和值,以Map<String, String> 的形式返回
Map<String, String> elements = jedis.hgetAll(key);
System.out.println(elements);
// 判断给定key值是否存在于哈希集中
System.out.println(jedis.hexists(key, “bbb”));
// 获取hash里面指定字段对应的值
System.out.println(jedis.hmget(key, “aaa”, “bbb”));
// 获取指定的值
System.out.println(jedis.hget(key, “aaa”));
// 删除指定的值
System.out.println(jedis.hdel(key, “aaa”));
System.out.println(jedis.hgetAll(key));
// 为key中的域 field 的值加上增量 increment
System.out.println(jedis.hincrBy(key, “bbb”, 100));
System.out.println(jedis.hgetAll(key));
}
Jedis操作事务
@Test
public void testTransaction() {
Transaction t = jedis.multi();
t.set(“hello”, “world”);
Response response = t.get(“hello”);
t.zadd(“foo”, 1, “barowitch”);
t.zadd(“foo”, 0, “barinsky”);
t.zadd(“foo”, 0, “barikoviev”);
Response<Set> sose = t.zrange(“foo”, 0, -1); // 返回全部相应并以有序集合的方式返回
System.out.println(response);
System.out.println(sose);
t.exec(); // 此行注意,不能缺少
String foolbar = response.get(); // Response.get() 可以从响应中获取数据
int soseSize = sose.get().size(); // sose.get() 会立即调用set方法
System.out.println(foolbar);
System.out.println(sose.get());
}
Jedis操作管道
@Test
public void testTransactionPipeling() {
Pipeline p = jedis.pipelined();//开一个管道
p.set("fool", "bar");p.zadd("foo", 1, "barowitch");p.zadd("foo", 0, "barinsky");p.zadd("foo", 0, "barikoviev");Response<String> pipeString = p.get("fool");Response<Set<String>> sose = p.zrange("foo", 0, -1);System.out.println(pipeString);System.out.println(sose);p.sync();//提交System.out.println("==========");System.out.println(p.get("fool"));System.out.println(p.zrange("foo", 0, -1));int soseSize = sose.get().size();Set<String> setBack = sose.get();System.out.println(soseSize);System.out.println(setBack);
}
Jedis的常用操作相关推荐
- TensorFlow常用操作:代码示例
1,定义矩阵代码示例: import tensorflow as tftf.zeros([3,4]) #定义3行4列元素均为0的矩阵tensor=tf.constant([1,2,3,4])#定义一维 ...
- Ubuntu 常用操作
Ubuntu常用操作 外观操作 修改应用icon图标 终端美化 内存管理 内存查询 分区管理 日志清理 操作执行 自动执行多条命令 外观操作 修改应用icon图标 在/usr/share/applic ...
- IOS沙盒Files目录说明和常用操作
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...
- 用Python轻松搞定Excel中的20个常用操作
来源 |早起Python(ID: zaoqi-python) Excel与Python都是数据分析中常用的工具,本文将使用动态图(Excel)+代码(Python)的方式来演示这两种工具是如何实现数据 ...
- python和R对dataframe的常用操作的差异:head、tail、shape、dim、nrow、ncol、descirbe、summary、str
python和R对dataframe的常用操作的差异:head.tail.shape.dim.nrow.ncol.descirbe.summary.str # python df.head() # R ...
- 【node】Sequelize常用操作、基本增删改查
[node]Sequelize常用操作.基本增删改查 sequelize初始化 先初始化一个数据库实例 再初始化一个model实例 sequelize的数据类型 基本增删改查的写法 SELECT * ...
- JavaScript:对Object对象的一些常用操作总结
JavaScript对Object对象的一些常用操作总结. 一.Object.assign() 1.可以用作对象的复制 var obj = { a: 1 }; var copy = Object.as ...
- ubuntu的学习教程(常用操作)
摘要 最近在学习linux,把自己学习过程中遇到的常用操作以及一些有助于理解的内容记录下来.我主要用的是ubuntu系统 命令提示符 '~' 这个是指用户的家目录,用户分为root用户和普通用户,ro ...
- 20180929 VIM进入编辑模式、VIM命令模式常用操作
2019独角兽企业重金招聘Python工程师标准>>> vim进入编辑模式方式 按键 作用 i 在当前字符前插入 I 在光标所在行的行首插入 a 在当前字符后插入 A 在光标所在行的 ...
最新文章
- 你所不知道的Android Studio调试技巧
- python中的引用类型_Python 中的引用和类属性的初步理解
- Android 开发笔记“关闭默认键盘”
- [超享]linux共享3160命令
- 如何去除字符串中的 “\n“ ?80% 的同学错了!
- vue子组件赋值props_vue 父子传值props赋值失效问题
- Redis秒杀功能设计与实现
- 一个成型的awt所必须的frame组件
- JavaScript同步和异步
- 当调用wcf, 小心返回值包含enum越界的错误。
- 阿里云服务器ECS建站教程
- 微信网页授权 Vue
- 修改 exchange服务器地址,绑定exchange邮箱服务器地址
- 鸭梨笔记本上市!!!超越苹果和微软!!
- IoT当前最重要的机遇,全面解读专为边缘计算而生的EdgeX Foundry
- 【微信】微信,是一个生活方式
- Chrome浏览器绿色、免安装版下载,长期更新
- python 'NoneType' object has no attribute '_root'
- java 拒绝连接_java.net.ConnectException:连接被拒绝
- CentOS7(Linux)在VMware Workstation上的 安装使用教程