Example usage of a Redis hash is to store information about a post. A hash can be used to store information such as post_id, author_name, publish_data, category, and more.
Hashes are one of the fundamental data types, and whether you are new to Redis or a seasoned pro, knowing how to work with Hashes can be very beneficial.
The essence of this tutorial is to provide you with the fundamental knowledge to work with Hashes in Redis.
In this guide, we have used the latest version of the Redis Server running on a Debian 11 system. Although you do not have to replicate this environment, we recommend using the native Redis CLI. Using the Redis CLI will ensure you get similar outputs as this guide.
How to create a Hash
The first thing we will learn is how to create a hash in Redis.
Open the Redis CLI and use the HSET command to create a hash. The command takes the name of the hash key, the field, and the value as its arguments.
Take the example shown below:
The above command creates a hash with the key of post and a field of title and its corresponding value.
The command returns an integer 1 if the hash is created successfully.
It is good to ensure you provide all the arguments in the HSET command to avoid any errors. For example, the following command returns an error as one argument is missing from the command:
(error) ERR wrong number of arguments for ‘hset’ command
NOTE: If you run the HSET command where a hash already exists, Redis will overwrite the content of the old hash with the new one.
For example, take a look at the command below:
In this case, the HSET command returns an integer 0 if the specified value is updated successfully.
To create multiple fields and their corresponding values in a single command, use the HMSET command.
The command returns the string “OK” if the hash is created successfully.
How to Fetch Info from a Hash
Let us discuss the various commands you can use to fetch information from a Redis hash.
To get the value associated with a specific field, use the HGET command. The command takes the field name as the argument.
"Hello world title."
The command will restore the associated value of the specified key.
To get all keys in a hash, use the HGETALL command. This command takes the hash key as the argument.
Consider the example shown below:
2) "Hello world title."
As shown in the example output above, the HGETALL command returns the fields and their associated values.
If you want to fetch values from multiple fields simultaneously, you can use the command HMGET. The command takes the key and the fields you wish to retrieve as the arguments.
Take a look at the example shown below:
2) "Hello world title."
The example above specifies the key and multiple fields, and the command returns the corresponding values.
Ensure the specified field exists; otherwise, Redis will return a nil value.
Suppose you only want to view the fields in a specific hash but not their corresponding values? For that, you can use the HKEYS command:
In the example above, the HKEY command takes the key as the argument and returns all the fields in the hash.
Similarly, you can use the HVALS command to retrieve the values in the hash.
1) "Hello world title."
The command will only return the values and not their fields.
To get an integer value of the total number of fields in the hash, use the HVAL command.
The example command above shows that the hash contains five fields.
Deleting Keys in a Hash
To remove a field from a specific hash, you can use the HDEL command. The command takes single or multiple fields as arguments.
The command returns an integer value indicating the number of fields removed from the hash.
If the field does not exist, the command ignores it and only removes the existing ones.
To check if a field exists in the hash, use the HEXISTS command.
The command returns integer 1 if the key exists and 0 if not.
The tutorial covers commands and examples to use when creating and working with Hashes in Redis. There are other commands beyond the scope of this tutorial. Check the documentation to learn more.
Thank you for reading!