EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis HMSET

Redis HMSET

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.

Redis HMSET

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Redis HMSET Command

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:

hmset command second field

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:

Ubuntu system

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:

Start Redis Server

3. Now we login into the redis server by using the below command.

Code:

# redis-cli

Output:

Login to Redis Server

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:

HMSET command

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:

Retrieving the value

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:

Redis Three Fields

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:

Defining Redis HMSET Command

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:

Setting of HMSET

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:

returning nill value

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:

Single Filed

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:

four fields

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:

Using another field

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 –

  1. Redis Get All Keys
  2. Redis GUI
  3. Redis Pubsub
  4. Redis Version
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Software Development Bundle5000+ Hours of HD Videos | 149 Learning Paths | 1050+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program2000+ Hours of HD Videos | 43 Learning Paths | 550+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more