Redis is an incredible tool. Whether you are looking to expand your knowledge on in-memory key valur databases or setup a message broker, caching mechanism or a message queue. Redis has you covered.
One of the most influential and useful database in the Redis ecosystem is a hash. A hash refers to a mapping between a string field and a string value. Remember dictionaries or JavaScript objects? Redis hash are not that far off.
Redis hashes are easy to implement and consume minimal space making them wildly efficient for larget set of objects.
Requirements
Luckily, to follow along with this guide, you will need the following:
- A Redis server
You can learn more about how to install the Redis server on Linux in our tutorials.
Redis Create Hash
Before we can learn how to use the Redis HGETALL
command, let's start by creating a Redis hash.
To create a hash in Redis, we can use the HSET
or HMSET
commands to add a single or multiple fields, respectively. The command syntax is expressed as shown:
HSET key field value
HMSET key field value [field, value ...]
Where the key refers to the name of the hash, the hash field and the value. Keep in mind that if the specified hash key does not exist on the database, Redis will automatically create it.
For example, we can create a hash that stores user data as shown in the command below:
HMSET user_info username geekbits password "mypassword"
In this command, we are creating a hash called user_info
with two fields and values. The first is the username and the other is the password.
Notice that each field and its corresponding value is specified one after another.
Redis Fetch Field and Values
Once we have created a hash, we can retrieve the field and its associated value using the HGET
and HGETALL
commands.
Let us explore how we can do this.
The HGET
command allow us to specify the hash name and the field whose value we wish to retrieve. The command will then return the value of the specified value.
HGET user_info username
"geekbits"
In the command above, we specify the hash user_info
and the target field as username
.
The command above should return the value associated with the username
field.
127.0.0.1:6379> HGET user_info username
"geekbits"
To retrieve all the fields from a Redis hash, we use the Redis HGETALL
command. The command synyax is as shown below:
HGETALL key
Where the key refers to the name of the target hash. For example, to get all the fields and associated values of the user_info
hash, we can run the command:
HGETALL user_info
The command above will return an output as shown:
7.0.0.1:6379> HGETALL user_info
1) "username"
2) "geekbits"
3) "password"
4) "mypassword"
As we can see from the output above, the command returns the field and the associated values one after another.
For example, the username
field and its value geekbits
as shown in the command output above.
Keep in mind that Redis will return an empty array if you query a non-existent hash.
Take the example below:
HGETALL no_key
The command would return an output as shown:
127.0.0.1:6379> HGETALL no_key
(empty array)
And that's it.
Closing
That's it for this article. In this tutorial, you learned how to create and fetch fields from a Redis hash. You also learned how to fetch all the fields of a hash using HGETALL
command.
If you enjoyed this tutorial, feel free to share with your friends.
Leave us a comment below if you have any questions.
That being said, thanks for reading && see you in the next one.