HGET
Syntax
HGET key field
- Available since:
- 2.0.0
- Time complexity:
- O(1)
- ACL categories:
-
@read
,@hash
,@fast
,
返回存储在key
中的哈希表中与field
相关联的值。
示例
> HSET myhash field1 "foo"
(integer) 1
> HGET myhash field1
"foo"
> HGET myhash field2
(nil)
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.
import redis
r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
res1 = r.hset("myhash", "field1", "Hello")
print(res1)
# >>> 1
res2 = r.hget("myhash", "field1")
print(res2)
# >>> Hello
res3 = r.hset("myhash", mapping={"field2": "Hi", "field3": "World"})
print(res3)
# >>> 2
res4 = r.hget("myhash", "field2")
print(res4)
# >>> Hi
res5 = r.hget("myhash", "field3")
print(res5)
# >>> World
res6 = r.hgetall("myhash")
print(res6)
# >>> { "field1": "Hello", "field2": "Hi", "field3": "World" }
res7 = r.hset("myhash", "field1", "foo")
print(res7)
# >>> 1
res8 = r.hget("myhash", "field1")
print(res8)
# >>> foo
res9 = r.hget("myhash", "field2")
print(res9)
# >>> None
import assert from 'node:assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
const res1 = await client.hSet('myhash', 'field1', 'Hello')
console.log(res1) // 1
const res2 = await client.hGet('myhash', 'field1')
console.log(res2) // Hello
const res3 = await client.hSet(
'myhash',
{
'field2': 'Hi',
'field3': 'World'
}
)
console.log(res3) // 2
const res4 = await client.hGet('myhash', 'field2')
console.log(res4) // Hi
const res5 = await client.hGet('myhash', 'field3')
console.log(res5) // World
const res6 = await client.hGetAll('myhash')
console.log(res6)
const res7 = await client.hSet('myhash', 'field1', 'foo')
console.log(res7) // 1
const res8 = await client.hGet('myhash', 'field1')
console.log(res8) // foo
const res9 = await client.hGet('myhash', 'field2')
console.log(res9) // null
await client.quit();
import redis.clients.jedis.UnifiedJedis;
public class CmdsHashExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
// Tests for 'hdel' step.
// Tests for 'hexists' step.
// Tests for 'hexpire' step.
// Tests for 'hexpireat' step.
// Tests for 'hexpiretime' step.
Map<String, String> hGetExampleParams = new HashMap<>();
hGetExampleParams.put("field1", "foo");
long hGetResult1 = jedis.hset("myhash", hGetExampleParams);
System.out.println(hGetResult1); // >>> 1
String hGetResult2 = jedis.hget("myhash", "field1");
System.out.println(hGetResult2); // >>> foo
String hGetResult3 = jedis.hget("myhash", "field2");
System.out.println(hGetResult3); // >>> null
// Tests for 'hget' step.
// Tests for 'hgetall' step.
// Tests for 'hincrby' step.
// Tests for 'hincrbyfloat' step.
// Tests for 'hkeys' step.
// Tests for 'hlen' step.
// Tests for 'hmget' step.
// Tests for 'hmset' step.
// Tests for 'hpersist' step.
// Tests for 'hpexpire' step.
// Tests for 'hpexpireat' step.
// Tests for 'hpexpiretime' step.
// Tests for 'hpttl' step.
// Tests for 'hrandfield' step.
// Tests for 'hscan' step.
Map<String, String> hSetExampleParams = new HashMap<>();
hSetExampleParams.put("field1", "Hello");
long hSetResult1 = jedis.hset("myhash", hSetExampleParams);
System.out.println(hSetResult1); // >>> 1
String hSetResult2 = jedis.hget("myhash", "field1");
System.out.println(hSetResult2); // >>> Hello
hSetExampleParams.clear();
hSetExampleParams.put("field2", "Hi");
hSetExampleParams.put("field3", "World");
long hSetResult3 = jedis.hset("myhash",hSetExampleParams);
System.out.println(hSetResult3); // >>> 2
String hSetResult4 = jedis.hget("myhash", "field2");
System.out.println(hSetResult4); // >>> Hi
String hSetResult5 = jedis.hget("myhash", "field3");
System.out.println(hSetResult5); // >>> World
Map<String, String> hSetResult6 = jedis.hgetAll("myhash");
for (String key: hSetResult6.keySet()) {
System.out.println("Key: " + key + ", Value: " + hSetResult6.get(key));
}
// >>> Key: field3, Value: World
// >>> Key: field2, Value: Hi
// >>> Key: field1, Value: Hello
// Tests for 'hset' step.
// Tests for 'hsetnx' step.
// Tests for 'hstrlen' step.
// Tests for 'httl' step.
// Tests for 'hvals' step.
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_hset() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res1, err := rdb.HSet(ctx, "myhash", "field1", "Hello").Result()
if err != nil {
panic(err)
}
fmt.Println(res1) // >>> 1
res2, err := rdb.HGet(ctx, "myhash", "field1").Result()
if err != nil {
panic(err)
}
fmt.Println(res2) // >>> Hello
res3, err := rdb.HSet(ctx, "myhash",
"field2", "Hi",
"field3", "World",
).Result()
if err != nil {
panic(err)
}
fmt.Println(res3) // >>> 2
res4, err := rdb.HGet(ctx, "myhash", "field2").Result()
if err != nil {
panic(err)
}
fmt.Println(res4) // >>> Hi
res5, err := rdb.HGet(ctx, "myhash", "field3").Result()
if err != nil {
panic(err)
}
fmt.Println(res5) // >>> World
res6, err := rdb.HGetAll(ctx, "myhash").Result()
if err != nil {
panic(err)
}
fmt.Println(res6)
// >>> map[field1:Hello field2:Hi field3:World]
}
func ExampleClient_hget() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
res7, err := rdb.HSet(ctx, "myhash", "field1", "foo").Result()
if err != nil {
panic(err)
}
fmt.Println(res7) // >>> 1
res8, err := rdb.HGet(ctx, "myhash", "field1").Result()
if err != nil {
panic(err)
}
fmt.Println(res8) // >>> foo
res9, err := rdb.HGet(ctx, "myhash", "field2").Result()
if err != nil {
fmt.Println(err)
}
fmt.Println(res9) // >>> <empty string>
}
using NRedisStack.Tests;
using StackExchange.Redis;
public class CmdsHashExample
{
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool res1 = db.HashSet("myhash", "field1", "foo");
RedisValue res2 = db.HashGet("myhash", "field1");
Console.WriteLine(res2); // >>> foo
RedisValue res3 = db.HashGet("myhash", "field2");
Console.WriteLine(res3); // >>> Null
// Tests for 'hget' step.
bool res4 = db.HashSet("myhash", "field1", "Hello");
RedisValue res5 = db.HashGet("myhash", "field1");
Console.WriteLine(res5); // >>> Hello
db.HashSet(
"myhash",
new HashEntry[] {
new HashEntry("field2", "Hi"),
new HashEntry("field3", "World")
}
);
RedisValue res6 = db.HashGet("myhash", "field2");
Console.WriteLine(res6); // >>> Hi
RedisValue res7 = db.HashGet("myhash", "field3");
Console.WriteLine(res7); // >>> World
HashEntry[] res8 = db.HashGetAll("myhash");
Console.WriteLine($"{string.Join(", ", res8.Select(h => $"{h.Name}: {h.Value}"))}");
// >>> field1: Hello, field2: Hi, field3: World
// Tests for 'hset' step.
}
}
在交互式控制台中尝试这些命令:
RESP2 回复
以下之一:
- Bulk string reply: 与字段关联的值。
- Nil reply: 如果字段不在哈希中或键不存在。
RESP3 回复
以下之一:
- Bulk string reply: 与字段关联的值。
- Null reply: 如果字段不在哈希中或键不存在。