How to Use the Redis HMSET Command

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re working with data in Redis, you’re likely to find yourself using the hash data structure. A hash can be thought of as a container that holds field names and values, mapping those string names to string values. You can use the HMSET command to set a specific field to a value in a hash. Although this command has been deprecated with the HSET command serving as its replacement, it’s still helpful to understand how it’s used. In this article, we’ll discuss the Redis HMSET command and look at some simple examples of its use.

Prerequisites

Before attempting the examples in this tutorial, you’ll need to have a Redis server installed and working on your machine. You can use the command redis-server --version to check the version number of your Redis installation.

Using the Hash in Redis

As we mentioned earlier, the Redis hash is a data structure that maps string names to string values. It implements an array abstract data type to do so. Since hashes acts like unique containers for fields and values, they provide an excellent way to view an object as a Redis data structure.

In this tutorial, we’ll show how to use the Redis HMSET command by using the command-line interface in Redis and also with the Node.js and Python programming languages.

Accessing the Redis Command-Line Interface

Before we can use any commands with our Redis database, we first need to enter the Redis command-line interface:

1
redis-cli

Using the Redis HMSET Command

The Redis HMSET command is used to set the value of a field that is specified to the stored key of the hash.

Here’s an example:

1
2
3
4
5
6
127.0.0.1:6379> HMSET test demo "example1" demo1 "example2"
OK
127.0.0.1:6379> HGET test demo
"example1"
127.0.0.1:6379> HGET test demo1
"example2"

NOTE: Keep in mind that the HMSET command has been deprecated since Redis version 4.0.0. The command HSET should be used instead.

Using Node.js for Redis HMSET Command

Node.js can also be used to create and store a hash in Redis with the help of the node_redis module. This module is able to interact with the Redis database. We’ll start by navigating to the directory where we want our script to live, and we’ll create a file with a .js extension. We’ll also use the npm package manager to install the node-redis module.

We’ll execute the following command to install the module:

1
npm install node_redis

Next, let’s look at the code:

app.js

  • First, we create a variable that requires the Redis database and another variable for the client.
1
2
var redis_node = require('redis');
var client_conn = redis_node.createClient();
  • We then create a function that will connect to the client.
1
2
3
client_conn.on('connect', function(){
    console.log('Successfully connected')
});
  • The function that uses the HMSET command will now create and store a hash value in Redis.
1
2
3
4
5
client_conn.hmset('test',
    {'demo2': 'example3',
     'demo3': 'example4',
     'demo4': 'example5'
});
  • Next, we use the HGETALL command to retrieve all of the objects that are stored in the Redis hash.
1
2
3
client_conn.hgetall('test', function(err, object) {
    console.log(object);
});

To verify that our application is running, we can run the command node app.js in the terminal. We should see results that look something like this:

1
2
Successfully connected
{ demo2: 'example3', demo3: 'example4', demo4: 'example5' }

Using Python for Redis HMSET Command

We can also use the HMSET command in Python with the help of the redis-py module, which is integrated with the Redis client.

To use Redis with Python, we first need to install the Redis module in our Python virtual environment using pip3. Once we install the module, we can dive into the code:

app.py

1
2
3
4
5
6
7
8
9
10
11
import redis

r = redis.Redis(
host='127.0.0.1',
port=6379)

r.hmset('test', {"demo2": "example3", "demo3": "example4", "demo4": "example5"})
result = r.hgetall('test')


print(result)

The output will look something like this:

1
{b'HR': b'Anthony', b'MIS': b'Clint', b'Accounting': b'Mark'}

Conclusion

If you’re planning to use Redis to store and manage data, it’s important to know how to work with hashes. One command that can be used to set values within a hash is the Redis HMSET command. This command has been deprecated in favor of the HSET command; however, it’s still valuable to understand how it works and know how to use it. In this article, we provided examples of how to use the Redis HMSET command with both Node.js and Python. With these examples to guide you, you’ll be able to utilize this command in your own Redis environment.

Just the Code

Throughout this article, we looked at two different types of code: Node.js and Python. Here’s the complete code you’ll need for your app.js and app.py files:

app.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var redis_node = require('redis');
var client_conn = redis_node.createClient();

client_conn.on('connect', function(){
    console.log('Successfully connected')
});

client_conn.hmset('test',
    {'demo2': 'example3',
     'demo3': 'example4',
     'demo4': 'example5'
});

client_conn.hgetall('test', function(err, object) {
    console.log(object);
});

app.py

1
2
3
4
5
6
7
8
9
10
11
import redis

r = redis.Redis(
    host='127.0.0.1',
    port=6379)
   
r.hmset('test', {"demo2": "example3", "demo3": "example4", "demo4": "example5"})
result = r.hgetall('test')


print(result)

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.