大数据之Redis:Redis之Jedis-API操作
Posted 浊酒南街
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数据之Redis:Redis之Jedis-API操作相关的知识,希望对你有一定的参考价值。
目录
1、Jedis测试连通性
public class Demo01 {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("127.0.0.1",6379);
//查看服务是否运行,打出pong表示OK
System.out.println("connection is OK==========>: "+jedis.ping());
}
}
2、Jedis-API: Key
public class redis {
@SuppressWarnings("resource")
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.204.101", 6379);
jedis.set("26", "haha");
String str = jedis.get("26");
System.out.println("26的值是"+str);
Set<String> keys = jedis.keys("*");
for(String key : keys) {
System.out.println(key);
}
System.out.println("连接成功"+jedis.ping());
}
}
3、Jedis-API: String
System.out.println(jedis.get("k1"));
jedis.set("k4","k4_Redis");
System.out.println("----------------------------------------");
jedis.mset("str1","v1","str2","v2","str3","v3");
System.out.println(jedis.mget("str1","str2","str3"));
4、Jedis-API: List
List<String> list = jedis.lrange("mylist",0,-1);
for (String element : list) {
System.out.println(element);
}
5、Jedis-API: set
jedis.sadd("orders","jd001");
jedis.sadd("orders","jd002");
jedis.sadd("orders","jd003");
Set<String> set1 = jedis.smembers("orders");
for (String strs : set1) {
System.out.println(strs);
}
jedis.srem("orders","jd002");
6、Jedis-API: hash
jedis.hset("hash1","userName","lisi");
System.out.println(jedis.hget("hash1","userName"));
Map<String,String> map = new HashMap<String,String>();
map.put("telphone","13810169999");
map.put("address","jinghang");
map.put("email","abc@163.com");
jedis.hmset("hash2",map);
List<String> result = jedis.hmget("hash2", "telphone","email");
for (String element : result) {
System.out.println(element);
}
7、Jedis-API: zset
jedis.zadd("zset01",60d,"v1");
jedis.zadd("zset01",70d,"v2");
jedis.zadd("zset01",80d,"v3");
jedis.zadd("zset01",90d,"v4");
Set<String> s1 = jedis.zrange("zset01",0,-1);
for (Iterator iterator = s1.iterator(); iterator.hasNext();) {
String string = (String) iterator.next();
System.out.println(string);
}
以上是关于大数据之Redis:Redis之Jedis-API操作的主要内容,如果未能解决你的问题,请参考以下文章