The Redis HGET
command is used to retrieve the value associated with a particular field in a hash stored at a key. Its common use cases include:
Here's an example using redis-py, the Python interface to the Redis key-value store.
First, you need to establish a connection to the Redis server:
import redis r = redis.Redis(host='localhost', port=6379, db=0)
You can set a hash value with the hset
method:
r.hset('hash_key', 'field1', 'value1')
To retrieve the value using HGET
, use the hget
method:
value = r.hget('hash_key', 'field1') print(value) # Output: b'value1'
Note: The result is returned as bytes, so you may want to decode it to a string depending on your use case:
value = r.hget('hash_key', 'field1').decode('utf-8') print(value) # Output: 'value1'
HGET
will return None
.Q: What happens if the key does not exist in Redis when using HGET?
A: The HGET
command will return None
.
Q: Can I use HGET to get multiple fields at once?
A: No, you can't. HGET
only retrieves one field. If you need to retrieve multiple fields at once, consider using the HMGET
command.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.