我可以拥有同名的集合和 hset 吗?
Can I have a set and a hset with same name?
给定代码的输出是什么?
Jedis 可以在数据库中保存两个不同类型但名称相同的数据集吗?
如果不是,在印刷线上,j.get returns 会是什么? j.set 是否转换为 j.hset?
我是 Redis 数据库的新手
public static void main(String[] args) {
Jedis j = new Jedis("slc09sro");
try {
j.set("alex1", "1");
}
catch (Exception e){}
try {
j.set("alex1", "2");
}
catch (Exception e){}
try {
j.hset("alex1", "3", "4");
}
catch (Exception e){}
System.out.println(j.get("alex1"));
j.close;
}
您的示例在尝试使用 hset
方法时将失败。不需要 Java 的简单示例:
$ redis-cli
127.0.0.1:6379> set alex1 1
OK
127.0.0.1:6379> set alex1 2
OK
127.0.0.1:6379> hset alex1 3 4
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> get alex1
"2"
127.0.0.1:6379>
这是因为键 alex1
保存的是字符串值,而不是哈希值。在不成功的 HSET
调用之后,密钥与上次 SET
调用保持不变,因此包含字符串值 "2"
.
给定代码的输出是什么? Jedis 可以在数据库中保存两个不同类型但名称相同的数据集吗? 如果不是,在印刷线上,j.get returns 会是什么? j.set 是否转换为 j.hset?
我是 Redis 数据库的新手
public static void main(String[] args) {
Jedis j = new Jedis("slc09sro");
try {
j.set("alex1", "1");
}
catch (Exception e){}
try {
j.set("alex1", "2");
}
catch (Exception e){}
try {
j.hset("alex1", "3", "4");
}
catch (Exception e){}
System.out.println(j.get("alex1"));
j.close;
}
您的示例在尝试使用 hset
方法时将失败。不需要 Java 的简单示例:
$ redis-cli
127.0.0.1:6379> set alex1 1
OK
127.0.0.1:6379> set alex1 2
OK
127.0.0.1:6379> hset alex1 3 4
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> get alex1
"2"
127.0.0.1:6379>
这是因为键 alex1
保存的是字符串值,而不是哈希值。在不成功的 HSET
调用之后,密钥与上次 SET
调用保持不变,因此包含字符串值 "2"
.