Updated February 17, 2023
Introduction to Redis HMSET
Redis HMSET is used to replace the HSET with multiple fields of pairs value at the time of migrating and writing the new code. It is used to set the specified field for the respective values stored in the key. This command overwrites the specified field which exists in the hash value. If suppose key does not exist then a new key is holding and a hash is created.
Key Takeaways
- At the time of using the HMSET command, we need to use the redis hget command to retrieve the value which we have defined in the HMSET command.
- We are defining single as well as multiple field values to define the command of redis HMSET.
What is Redis HMSET?
The HMSET command is used to set the specified fields for the respective values into the hash key stored. The redis HMSET command is used to overwrite existing hash fields. If the key does not exist, a new key is held, and the hash is created. When working with Redis, we must use the hash data structure.
A hash is nothing but the container, which holds the name of fields, also it will map the name of the string into the value of string. We are using HMSET command for setting specific fields of value. Basically, the command will be deprecated while using the hset command. Basically, we can say that HMSET and hset will work the same.
How to Create Redis HMSET Command?
At the time of building an application by using redis, we need to find the way at the time. Hash is nothing but the primitive type which allows storing of the value and field mapping. It is very essential for adding and understanding the commands which we are using for working with types of hash in redis.
The below syntax shows how we can create the hash redis HMSET command as follows. The syntax will contain the key, field, and value. We can use multiple key, field, and values by using a single command as follows.
Syntax:
HMSET name_of_key name_of_filed value
In the above syntax HMSET is the command which we are using in redis for replacing HMSET with multiple fields.
The name of the key argument is defined as the key name that we use with the HMSET command. The name of the field argument is the same as the field name used with the HMSET command. The value argument is simply the value we defined for the field.
The below example shows how we can create the HMSET command as follows. At the time of using the HMSET command to retrieve the value, we are using hget command. Below we are using the single field to define the HMSET command. Below we have defined the key name as redis_hmset, we have defined the field name as hmset_field, and the value defined as “redis hmset command” as follows.
Code:
HMSET redis_hmset hmset_field "redis hmset command"
HGET redis_hmset hmset_field
Output:
In the below example, we are using two fields to define the HMSET command as follows. In the below example, we have defined the key name as redis_ hmset, we have defined the field name as hmset _field and hmset _field2 also value defined as “redis hmset command” and “redis hmset command second field” as follows.
Code:
HMSET redis_hmset hmset_field "redis hmset command" hmset_field2 "redis hmset command second field"
HGET redis_hmset hmset_field
HGET redis_hmset hmset_field2
Output:
How to Use Redis HMSET?
We need to install redis server in our system.
1. First we are installing the server in the ubuntu system by using the apt command. The below example shows the installation of redis.
Code:
apt install redis-server
Output:
2. Now we are starting our redis server by using the redis-server command. The below example shows to start the redis server.
Code:
# redis-server
Output:
3. Now we login into the redis server by using the below command.
Code:
# redis-cli
Output:
4. While login into the redis server now in this step we are using the HMSET command.
Code:
HMSET hmset_key hmset_field "how to use redis hmset"
Output:
5. Now we are using the HGET command for retrieving the value which we have defined in the HMSET command as follows.
Code:
HGET hmset_key hmset_field
Output:
Redis HMSET Command
Basically, HMSET command is used in redis. Below is the full syntax, as we know that we are using multiple fields and values with the HMSET command. The below syntax shows multiple fields and values as follows.
Redis HMSET command syntax:
HMSET name_of_key1 name_of_filed1 value … name_of_keyN name_of_filed valueN
In the above syntax, we can see that we have defined multiple field name and value, but we have defined a single key. So, we can say that we can use multiple field and its value but we need to use a single key in the HMSET command. We can use single as well as multiple field and values with the HMSET command.
In the below example we are using three fields as follows:
Code:
HMSET redis_key_hmset field1 "value1" field2 "value2" field3 "value3"
Output:
In the above command of HMSET, we are defining the HGET command to retrieve the value from the HMSET command as follows.
Code:
HMSET redis_key_hmset1 field1 "value1" field2 "value2" field3 "value3"
HGET redis_key_ hmset1 field1
HGET redis_key_ hmset1 field2
HGET redis_key_ hmset1 field3
Output:
Redis HMSET Setting
To define the setting of HMSET we need to use the HMSET command in redis. The below example shows that we are using the HMSET setting with a single value and single field as follows. In the below example, we are using the key name as hmset_settting, the field name as redis_hmset, and the value as value 1 as follows.
Code:
HMSET hmset_settting redis_hmset "value 1"
HGET hmset_settting redis_hmset
Output:
In the below example, we are using another field that we have not defined so it will return the null value as follows. We are using the field name redis_hmset1 but it is not defined in the HMSET command so it will return nil value.
Code:
HMSET hmset_settting redis_hmset "value 1"
HGET hmset_settting redis_hmset1
Output:
Examples of Redis HMSET
Given below are the examples mentioned:
Example #1
In the below example, we are using a single field to define the example of HMSET as follows.
Code:
HMSET hmset_example example_field "exampele1"
HGET hmset_example example_field
Output:
Example #2
In the below example, we are using four fields to define the example of HMSET as follows.
Code:
HMSET hmset_example example_field1 "exampele1" example_field2 "exampele2" example_field3 "exampele3" example_field4 "exampele4"
HGET hmset_example example_field1
HGET hmset_example example_field2
HGET hmset_example example_field3
HGET hmset_example example_field4
Output:
Example #3
In the below example, we are using another field that we have not defined as follows.
Code:
HMSET hmset_example example_field1 "exampele1" example_field2 "exampele2" example_field3 "exampele3" example_field4 "exampele4"
HGET hmset_example example_field5
HGET hmset_example example_field6
HGET hmset_example example_field7
HGET hmset_example example_field8
Output:
Conclusion
If the key does not exist, the new key will be used to create the hash.. While working with redis we need to use the hash data structure. Redis HMSET is used to replace the hset with multiple fields of pairs value at the time of migrating and writing the new code.
Recommended Articles
This is a guide to Redis HMSET. Here we discuss the introduction, how to create a redis HMSET command, and examples. You may also have a look at the following articles to learn more –