Updated February 15, 2023
Introduction to Redis HGETALL
Redis HGETALL returns the value and field from the specified key that we used in the HGETALL command. Each field name in a returned value is followed by using value, so the reply length is twice the size of the specified hash. When working with a redis hash key, we use the HGETALL command to retrieve all of the values and fields from the hash.
Key Takeaways
- Redis HGETALL is an important command that is used to get all the fields and values from the specified key that we used with the HGETALL command.
- If key is not present in the database, the redis HGETALL function returns an empty array as an output.
What is Redis HGETALL?
The redis HGETALL command is used to return the field and its specified value from the key used in the HGETALL command. The operation returns an array that is twice the size of the field number because it is filled with the values and fields of each field. The field is following the order of its respective value.
When the HGETALL command is executed on a non-existing field value, it returns the empty set value. As we know, the keys command is used to get all the keys from the Redis database; however, it only displays the key name. The command is required to display the key name, as well as its field and value.
Redis HGETALL Command
At the time using HGETALL we need to create the key and its field value by using the hset command. If the key does not exist then it will return the empty set.
Below syntax shows the redis HGETALL command as follows:
HGETALL name_of_key
The redis HGETALL command syntax contains only one parameter with HGETALL command.
Below is the parameter description of the redis HGETALL command as follows:
1. HGETALL – This is the redis command that is used to display the field and value of the specified key that we are using with this command.
2. Name of key – This is the key name that we use with the HGETALL command. If the key exists in the database, the HGETALL command returns its field and value; if the key does not exist in the database, the command returns an empty set.
The below example shows how redis HGETALL is working. In the below example, we are creating the key name as key_HGETALL and we are defining the field as field1, field2, and field3. Also, we are defining the value of the field as val1, val2, and val3 respectively. After creating the key and assigning the value and field we are executing the HGETALL command on key name as key_hgetall as follows.
Command:
HSET key_hgetall field1 val1 field2 val2 field3 val3
HGETALL key_hgetall
Output:
In the below example, we are adding more fields into the key name as key_hgetall. We are adding the field as field4, field5, and field6. Also, we are defining the value of the field as val4, val5, and val6 respectively. After adding the field and value we are executing the HGETALL command, we can see that it is showing all the fields and values for which a key exists.
Command:
HSET key_hgetall field4 val4 field5 val5 field6 val6
HGETALL key_hgetall
Output:
Redis HGETALL Key
Redis HGETALL key contains the key which we are using with HGETALL command. At the time of using HGETALL command in redis, the key is an important parameter. Below example shows how key is working with HGETALL command. In the below example, we are creating the key name as key_hgetall2 and defining the field name as hgetall1 and hgetqall2, also we are assigning the value as val1 and val2.
Command:
HSET key_hgetall2 hgetall1 val1 hgetall2 val2
HGETALL key_hgetall2
Output:
In the below example, we are deleting the field from key_hgetall1 key. After deleting the field from the key, we can see that the HGETALL command will return the empty array list in the output.
Command:
HSET key_hgetall1 field1 val1
HDEL key_hgetall1 field1
HGETALL key_hgetall1
Output:
If the key does not exist in the database, the command returns an empty array in output. In the below example, we are using the key name key_hgetall12 with the HGETALL command, this key does not exist in the database, so it will return the empty array as follows.
Command:
HGETALL key_hgetall12
Output:
Redis HGETALL vs HGET
Basically, the main difference between HGETALL and HGET is that HGETALL is used to retrieve all the values as fields from the specified key, whereas the hget command is used to retrieve the specified field and value from the specified key. Hget does not accept multiple fields whereas HGETALL accepts all the fields which existed in the specified key.
The below example shows how HGETALL works with specified key. We are using the key_hgetall21 key as follows:
Command:
HSET key_hgetall21 field1 val1 field2 val2 field3 val3
HGETALL key_hgetall21
Output:
The below example shows how hget is working with the specified keys. We are using the key_hgetall22 key as follows. We can see that we need to use a specified field with the redis hget command.
Command:
HSET key_hgetall22 field1 val1 field2 val2 field3 val3
HGET key_hgetall22 field1
Output:
To use the command, we need to pass only a single parameter i.e. name of the key. At the time using the hget command in redis we need to use the key name as well as the specified key parameter.
Examples of Redis HGETALL
Given below are the examples of redis HGETALL:
Example #1
In the below example we are creating the key name as hgetall_exp, at the time of creating the key we are defining the field name as field1 and field2, also assigning the value as val1 and val2 respectively.
Command:
HSET hgetall_exp field1 val1 field2 val2
HGETALL hgetall_exp
Output:
Example #2
In the below example, we are using the key name hgetall_exp1, but this key does not exist in the database, so it will return an empty set as follows.
Command:
HGETALL hgetall_exp1
Output:
Example #3
In the below example, we are using the key name hgetall_exp1, this key is present in the DB but the key field does not exist in the database so it will return the empty set as follows.
Command:
HGETALL hgetall_exp1
Output:
Conclusion
The redis HGETALL operation returns an array that is twice the size of the field number because it is filled with the values and fields of each field. It returns the value and field from the specified key that we used in the HGETALL command.
Recommended Articles
This is a guide to Redis HGETALL. Here we discuss the introduction, redis HGETALL command & key, examples respectively. You can also look at the following articles to learn more –