Redis java操作客户端
Jedis常用操作
1.测试连通性
Jedis jedis = new Jedis("192.168.1.201",6380,10000); System.out.println(jedis.ping());
控制台打印
PONG
2. 5+1操作
5中数据类型的操作:string list set hash zset
String
String 没什么好说的,最长用的一种
Jedis jedis = new Jedis("192.168.1.201",6379); System.out.println(jedis.ping()); jedis.set("k1", "v1"); jedis.set("k2", "v2"); jedis.set("k3", "v3"); System.out.println(jedis.get("k1")); //"v1"
List
lpush
jedis.lpush("list01", "1","2","3","4","5"); //lpush为先进后出 可以这么理解,从左侧开始入栈 System.out.println(jedis.lrange("list01", 0, -1)); //[5, 4, 3, 2, 1]
rpush
jedis.rpush("list02", "1","2","3","4","5"); //rpush为先进先出 System.out.println(jedis.lrange("list02", 0, -1)); //[1, 2, 3, 4, 5]
lpop
System.out.println(jedis.lpop("list01")); //lpop 弹出栈顶数据 5 System.out.println(jedis.lrange("list01", 0, -1)); //[4, 3, 2, 1]
rpop
System.out.println(jedis.rpop("list02")); //rpop 弹出栈底数据 System.out.println(jedis.lrange("list02", 0, -1)); //[1, 2, 3, 4]
lindex
System.out.println(jedis.lrange("list02", 0, -1)); System.out.println(jedis.lindex("list02", 0)); //LINDEX 按照索引下标获得元素(从上到下)
llen
System.out.println(jedis.lrange("list02", 0, -1)); //[1, 2, 3, 4] System.out.println(jedis.llen("list02")); //LLEN 获取长度 4
lrem
jedis.rpush("list03", "1","1","1","2","2","2","3","3","3","4","4","4","5"); jedis.lrem("list03", 2, "3"); //删除 2 个 "3" System.out.println(jedis.lrange("list03", 0, -1)); //[1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5]
ltrim
jedis.lpush("list04", "1","2","3","4","5","6","7","8"); System.out.println(jedis.lrange("list04", 0, -1)); //[8, 7, 6, 5, 4, 3, 2, 1] jedis.ltrim("list04", 3, 5); //截取下标为3-5的数值,下表从0开始,再赋值给list01 System.out.println(jedis.lrange("list04", 0, -1)); //[5, 4, 3]
rpoplpush
jedis.lpush("list01", "1","2","3","4","5"); System.out.println(jedis.lrange("list01", 0, -1)); jedis.lpush("list02", "1","2","3"); System.out.println(jedis.lrange("list02", 0, -1)); jedis.rpoplpush("list01", "list02");//将一个数据弹出,使用rpop弹出的底部数据,lpush放入另一个中 System.out.println(jedis.lrange("list01", 0, -1)); //[5, 4, 3, 2] System.out.println(jedis.lrange("list02", 0, -1)); //[1, 3, 2, 1]
lset
jedis.lset("list01", 1, "x"); //lset key index value 对index位置的进行赋值value [5, x, 3, 2] System.out.println(jedis.lrange("list01", 0, -1));
linsert
System.out.println(jedis.lrange("list01", 0, -1)); //[5, x, 3, 2] jedis.linsert("list01", LIST_POSITION.BEFORE, "x", "java"); //在 x 之前插入一个 java ,如果指定值有重复,则找到的是第一个 System.out.println(jedis.lrange("list01", 0, -1));
Set
sadd/smembers
jedis.sadd("set01","1","1","2","2","3","3"); //sadd向一个set中添加数据 System.out.println(jedis.smembers("set01")); //使用smembers 取出set中的所有数据 [3, 2, 1]
sismember
boolean flag1 = jedis.sismember("set01", "x"); //判断set中是否包含某个值 boolean flag2 = jedis.sismember("set01", "1"); System.out.println(flag1); //false System.out.println(flag2); //true
scard
long count = jedis.scard("set01"); //scard 获取set集合中元素个数 System.out.println(count); //3
srem
long remCount = jedis.srem("set01", "3"); //srem 删除set集合中的某个元素 System.out.println(remCount); //1 表示删除成功 System.out.println(jedis.smembers("set01")); //[2, 1]
srandmember
jedis.sadd("set01", "3","4","5","6","7"); String srandmember = jedis.srandmember("set01"); //随机取出一个数 System.out.println(srandmember); //2
spop
System.out.println(jedis.smembers("set01")); //[3, 2, 1, 7, 6, 5, 4] jedis.spop("set01"); //随机出栈 System.out.println(jedis.smembers("set01")); //[3, 2, 1, 7, 5, 4]
smove
System.out.println(jedis.smembers("set01")); //[3, 2, 1, 7, 5, 4] jedis.smove("set01", "set02", "2"); //将set01 中的值 2 移动到 set02 中 System.out.println(jedis.smembers("set01")); //[3, 1, 7, 5, 4] System.out.println(jedis.smembers("set02")); //[2]
sdiff
System.out.println(jedis.smembers("set01")); jedis.sadd("set02", "3","4","6"); System.out.println(jedis.smembers("set02")); System.out.println(jedis.sdiff("set01","set02")); //在set01中,不在set02中的元素
sinter
System.out.println(jedis.smembers("set01")); System.out.println(jedis.smembers("set02")); System.out.println(jedis.sinter("set01","set02")); //交集 两个集合中都含有的元素 [3, 4]
sunion
System.out.println(jedis.smembers("set01")); //[3, 1, 7, 5, 4] System.out.println(jedis.smembers("set02")); //[3, 2, 6, 4] System.out.println(jedis.sunion("set01","set02")); //并集 [3, 2, 1, 7, 6, 5, 4]
Hash
hset/hget
jedis.hset("user", "id", "1"); //hset K V 赋值一个hash 其中 V 为(key,value) jedis.hset("user", "name", "z3"); //user为K, ("name","z3")为V System.out.println(jedis.hget("user", "id")); //1 hget获取hash值 System.out.println(jedis.hget("user", "name")); //z3
hmset/hmget
Map<String, String> customerMap = new HashMap<String, String>(); customerMap.put("id", "1"); customerMap.put("name", "z3"); customerMap.put("age", "12"); jedis.hmset("customer", customerMap); //使用hmset批量存储 System.out.println(jedis.hmget("customer", "id","name","age")); //hmget 批量获取 [1, z3, 12]
hgetAll
System.out.println(jedis.hgetAll("customer")); //取出所有的key=value {id=1, name=z3, age=12}
hdel
System.out.println(jedis.hgetAll("user")); //{id=1, name=z3} jedis.hdel("user", "name"); System.out.println(jedis.hgetAll("user")); //{id=1}
hlen
System.out.println(jedis.hgetAll("customer")); //{id=1, name=z3, age=12} System.out.println(jedis.hlen("customer")); //获取长度 3
hexist
System.out.println(jedis.hgetAll("customer")); //{id=1, name=z3, age=12} System.out.println(jedis.hexists("customer", "id")); //true 判断某个map的key是否存在 System.out.println(jedis.hexists("customer", "name")); //true System.out.println(jedis.hexists("customer", "email")); //false
hkeys/hvals
System.out.println(jedis.hgetAll("customer")); //{id=1, name=z3, age=12} System.out.println(jedis.hkeys("customer")); //获取所有的key [id, name, age] System.out.println(jedis.hvals("customer")); //h获取所有的value [12, z3, 1]
hincrBy
System.out.println(jedis.hgetAll("customer")); //{id=1, name=z3, age=12} System.out.println(jedis.hincrBy("customer", "age", 1)); //13
hsetnx
//hsetnx 如果不存在则添加,如果存在不改变 System.out.println(jedis.hsetnx("customer", "age", "20")); //0 jedis.hsetnx("customer", "email", "aa@aa.com"); System.out.println(jedis.hgetAll("customer")); //{id=1, email=aa@aa.com, name=z3, age=14}
Zset (Sorted set 有序集合)
zadd/zrange
Map<Double, String> scoreMembers = new HashMap<>(); scoreMembers.put(60.0, "v1"); scoreMembers.put(70.0, "v2"); scoreMembers.put(90.0, "v3"); scoreMembers.put(85.5, "v4"); scoreMembers.put(80.1, "v5"); jedis.zadd("zset01", scoreMembers); System.out.println(jedis.zrange("zset01", 0, -1)); //[v1, v2, v5, v4, v3]
zrangeByScore key 开始score 结束score (获取一个范围内的)
System.out.println(jedis.zrangeByScore("zset01", 60.0, 80.2)); //[v1, v2, v5]
ZRANGEBYSCORE zset01 60 90 limit 2 2 (在结果集中在截取)
System.out.println(jedis.zrangeByScore("zset01", 60.0, 80.2, 2, 2)); //[v5]
zrem
jedis.zrem("zset01", "v5"); //移除一个元素 System.out.println(jedis.zrange("zset01", 0, -1)); //[v1, v2, v4, v3]
zcard
System.out.println(jedis.zcard("zset01")); //4 zcard 获取个数
zcount
System.out.println(jedis.zcount("zset01", 60, 80)); //2 获取符合范围内的个数
zrank
System.out.println(jedis.zrange("zset01", 0, -1)); //[v1, v2, v4, v3] System.out.println(jedis.zrank("zset01", "v2")); //1 获取元素的下标 下标从0开始
zscore
System.out.println(jedis.zrange("zset01", 0, -1)); //[v1, v2, v4, v3] System.out.println(jedis.zscore("zset01", "v2")); //获取分数值 70.0
zrevrank
System.out.println(jedis.zrevrank("zset01", "v3")); //0 逆序获得下标值
zrevrangeByScore
System.out.println(jedis.zrevrangeByScore("zset01", 80, 60)); //[v2, v1]
3.事务提交
日常:
Transaction transaction = jedis.multi(); transaction.set("k4", "v44"); transaction.set("k5", "v55"); // transaction.exec(); transaction.discard();
加锁:
public class TestTx { public boolean transMethord() { Jedis jedis = new Jedis("192.168.1.201", 6380); int balance; // 余额 int debt; // 欠额 int amtToSubtract = 10; // 实刷额度 jedis.set("balance", "100"); jedis.set("debt", "0"); jedis.watch("balance");
//Thread.sleep(7000); balance = Integer.parseInt(jedis.get("balance")); if (balance < amtToSubtract) { jedis.unwatch(); System.out.println("modify"); return false; } else { System.out.println("*********transaction***********"); Transaction transaction = jedis.multi(); transaction.decrBy("balance", amtToSubtract); transaction.incrBy("debt", amtToSubtract); transaction.exec(); balance = Integer.parseInt(jedis.get("balance")); debt = Integer.parseInt(jedis.get("debt")); System.out.println("*******************" + balance); System.out.println("*******************" + debt); return true; } } /** * 通俗的讲,watch命令就是标记一个键,如果标记了一个键, * 在提交事务前如果该键被别人修改过,那事务就会失败,这种情况通常可以在程序中重新再尝试一次。 * 首先标记了键balance,然后检查余额是否足够,不足就取消标记,并不做扣减; * 足够的话,就启动事务进行更新操作,如果在此期间键balance被其他人修改,那在提交事务(执行exec)时就会报错, * 程序中通常可以捕获这类错误在重新执行一次,直到成功。 * @param args */ public static void main(String[] args) { TestTx test = new TestTx(); boolean retValue = test.transMethord(); System.out.println("main retValue--------- " + retValue); } }
如果在事务执行期间有进程修改了watch 的key ,则回滚事务
修改程序 打开注释
Thread.sleep(7000);
在期间 修改balance的值
127.0.0.1:6380> set balance 5
OK
127.0.0.1:6380>
则程序运行结果为:
modify
main retValue--------- false
4.主从复制
6379
127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6380,state=online,offset=232636,lag=1 master_repl_offset:232636 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:232609 repl_backlog_histlen:28 127.0.0.1:6379>
6380
127.0.0.1:6380> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_repl_offset:232650 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:232718 127.0.0.1:6380>
public class TestAPI { public static void main(String[] args) { Jedis jedis_M = new Jedis("192.168.1.201", 6379); Jedis jedis_S = new Jedis("192.168.1.201", 6380); jedis_M.set("k1", "v1"); String v1 = jedis_S.get("k1"); System.out.println(v1); //"v1" } }