-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJedis_hash.java
More file actions
49 lines (38 loc) · 1.11 KB
/
Jedis_hash.java
File metadata and controls
49 lines (38 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package jedis;
// hash 数据结构操作
// 哈希类型 hash : map格式
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import redis.clients.jedis.Jedis;
import java.util.Map;
import java.util.Set;
public class Jedis_hash {
public static void main(String[] args){
//1. 获取连接
//如果使用空参构造,默认值 "localhost",6379端口
Jedis jedis = new Jedis();
//2. 操作
// 存储hash
jedis.hset("myhash","name", "cpu");
jedis.hset("myhash", "age", "22");
jedis.hset("myhash", "gender", "female");
// 获取hash
String name = jedis.hget("myhash", "name");
System.out.println("name = " + name);
// 获取hash的所有map中的数据
Map<String, String> user = jedis.hgetAll("myhash");
// keyset
Set<String> keySet = user.keySet();
for (String key : keySet){
String value = user.get(key);
System.out.println(key + " : " + value);
}
//3. 关闭连接
jedis.close();
}
}
/*
name = cpu
gender : female
name : cpu
age : 22
* */