Updated February 17, 2023
Introduction to Redis HGET
Redis HGET is used to return all values and fields which was stored in the key. At the time of returning any value every name of the field is followed by the value, so we can say that the reply length is double to the hash size. Basically, it is used for getting the values that were associated with the field into the key stored in the hash value.
What is Redis HGET?
If the hget key does not exist, or if the key exists but the hash value does not contain the field that was specified, it will return nil as an output. If the key exists and is stored in the key, an error will be returned by using the datatype hash. When we use hget, we must first use hget to define the value by using redis hset. We can’t use redis hget without hset. When we use redis hget, we must define the key name and field. When we use the redis hset, we define the key name. The key name and field value is defined in redis hset, and the same key name is used in redis hget.
Redis HGET Key Field
At the time of using redis hget we need to use two parameters first is the key and another parameter is a field. The below syntax shows how we can define the key field in it.
Syntax:
HGET name_of_key name_of_field
The name of the key in the above syntax is simply the key name that we defined using the hset command. We have also defined the field name in redis by using the hset and hmset commands. The hset command is used in the following example to use the redis hget key. In the following example, the key name is hget key, and the key field is hget field. Also, the values of redis hget that are defined as “redis” are as follows.
Code:
HMSET hget_key hget_field "redis"
HGET hget_key hget_field
Output:
In the above example, we can see that after using the key field as hget_field, it will be retrieving all the values from the specified field. We can also use multiple field names with a single key at the time of using hget. In the below example, we can see that we are using multiple field names to define the redis hget. In the below example, we are using the key names redis_hget and field names hget_field1, hget_field2, hget_field3, and hget_field4. The below example shows the redis hget key field as follows.
Code:
HMSET redis_hget hget_field1 "val1" hget_field2 "val2" hget_field3 "val3" hget_field4 "val4"
HGET redis_hget hget_field1
HGET redis_hget hget_field3
HGET redis_hget hget_field4
HGET redis_hget hget_field2
Output:
In the above example, we can see that we are defining multiple key field name at the time of defining a single key. We can see that at the time of using hget and specifying the filed name the specified field value will be returned. It will not retrieve all the values which we have set into the redis field.
Redis Hash HGET Command
At the time using redis hget, we need to use the hset and hget commands. While using hget we need to use the first hset command. In the below example, we are not defining the key value so it will give the nil value. It will not provide any output. In the below example, we can see that we are defining the key name as hget_key and the field name as hget_field. But in the hget command, we are using the key name as hget_key1 and the field name as hget_field so it will retrieve the nil value.
Code:
HMSET hget_key hget_field "redis"
HGET hget_key1 hget_field
Output:
In the below example, we are not defining the key field so it will give the nil value. It will not provide any output. We can see that we are defining the key name as hget_key and the field name as hget_field. But in the hget command, we are using the key name as hget_key and the field name as hget_field1 so it will retrieve the nil value.
Code:
HMSET hget_key hget_field "redis"
HGET hget_key hget_field1
Output:
In the below example, we are defining the key name as hget_key and the field name as hget_field. Also in the hget command, we are using the key name as hget_key and the field name as hget_field so it will retrieve the value as redis.
Code:
HMSET hget_key hget_field "redis"
HGET hget_key hget_field
Output:
In the below example, we are using multiple key fields to define the redis hget command. Also as per filed name, we are defining the specified value. We are retrieving the value as per the field as follows.
Code:
HMSET hget_multiple field_multiple1 "hget_val1" field_multiple2 "hget_val2" field_multiple3 "hget_val3" field_multiple4 "hget_val4"
HGET hget_multiple field_multiple1
HGET hget_multiple field_multiple2
HGET hget_multiple field_multiple3
HGET hget_multiple field_multiple4
Output:
We can also use the hset command with hget. The below example shows the hset command with hget as follows.
Code:
HSET hget_key1 hget_field1 "redis"
HGET hget_key1 hget_field1
Output:
We can use hgetall command to display all the fields as follows. In the below example, we are using hgetall.
Code:
HMSET hget_multiple field_multiple1 "hget_val1" field_multiple2 "hget_val2" field_multiple3 "hget_val3" field_multiple4 "hget_val4"
HGETALL hget_multiple
Output:
Redis HGET Add and Remove Elements
The hset command is used to add the key-value pair to the hash, if hash does not exist then it will create new hash. The hget command is used to retrieve the value from the key which was specified. We are also using hkeys for retrieving the key which was present into the hash. Below example shows add and remove elements by using hget as follows.
Code:
import redis
redisClient = redis.StrictRedis (host='localhost', port=6379, db=0)
redisClient.hset ("NumberVsString", "11", "Eleven")
redisClient.hset ("NumberVsString", "12", "Twelve")
print("Key val twelve")
print(redisClient.hget("NumberVsString", "12"))
print ("Hash key present:");
print(redisClient.hkeys ("NumberVsString"))
print("Redis hash value:");
print(redisClient.hvals ("NumberVsString"))
print("Redis hash key:")
print(redisClient.hgetall ("NumberVsString"))
Output:
The below example shows redis get add and remove elements as follows. We are using the hgetall method as follows.
Code:
import redis
import redis
r_cli = redis.StrictRedis (host='localhost', port=6379, db=0)
h_name = "Color"
r_cli.hset (h_name, 1, "Black")
r_cli.hset (h_name, 2, "White")
print (r_cli.hgetall(h_name))
r_cli.hdel(h_name, 1)
print(r_cli.hgetall(h_name))
Output:
FAQ
Given below are the FAQs mentioned:
Q1. What is the use of redis HGET?
Answer: It is basically used to return the value associated with the stored key and the field.
Q2. Which methods we are using with redis HGET?
Answer: At the time of using it we need to define the method name as hset and hmset. We can use any of the methods for the same.
Q3. Which parameter do we need to pass at the time of using redis hget?
Answer: We need to pass the key name and field name at the time of using it.
Conclusion
If hget key does not exist, or if the key has existed and the hash value does not contain the field which was specified then it will return the nil as an output. Basically, it is used for getting the values that were associated with the field into the key stored in the hash value.
Recommended Articles
This is a guide to Redis HGET. Here we discuss the introduction, key field, redis HGET add and remove elements, and FAQ. You may also have a look at the following articles to learn more –