How to Use the Redis HSET Data Type
Introduction
If you’re getting started with Redis, it’s important to have a good understanding of hashes and how they’re used. A hash is a data structure that stores a mapping between a key and a value. Redis hashes can hold multiple key-value pairs and take up little space, making them well-suited to represent complex data objects. For example, a Redis hash could be created to represent an employee, and it would probably include fields such as name
, employee_id
, email
and job_title
. The Redis HSET command can be used to create a new hash or overwrite an existing one. In this article, we’ll take a closer look at the Redis HSET command and see how it can be used to manage hashes in a Redis data store.
Prerequisites
A few key prerequisites need to be in place before proceeding with this tutorial:
You should have some basic knowledge of Redis and its data types, particularly hashes.
You’ll need to have Redis installed and working on your machine. The command
sudo systemctl status redis
can be used on a Linux system to return the current status of your Redis installation.
Accessing Redis
If you’re on any type of Unix operating system, you can use the command redis-cli
in your terminal to start and access the redis-cli.
The output will look like the following:
1 | 127.0.0.1:6379> |
NOTE: Keep in mind that the default localhost port for Redis is 6379
Redis HSET data type command
The basic syntax for the HSET command looks like this:
1 | HSET key field value |
Let’s look at this command in a bit more detail:
The
key
simply represents the name of the given key. For example:myhashkey
The
field
specifies the value of the hash that is stored at the key. If a field already exists, it will overwrite it.The
value
represents the record that will be saved in your hash field.
The following example shows the HSET command in action:
1 2 | 127.0.0.1:6379> HSET myhashkey somefield demo (integer) 1 |
If a new hash is created, the command returns a ‘1’. If a hash already exists and is overwritten, the HSET
command will return a ‘0’
Once you use HSET to set a field value in your hash, you can also retrieve that value using the command shown below:
1 | HMGET myhashkey samplefield |
As you can see, the HMGET command simply gets the values associated with the field represented by sampleField
and stored at the key represented by myhashkey
. The output will look like this:
1 | "demo" |
NOTE: You can also use the Redis command HGET
to get the value of the field in the hash that is stored by the key.
Adding multiple hash in Redis
The examples we’ve looked at so far use the Redis HSET
command to set a single field in a hash. The HMSET
command can be viewed as the multi-field version of HSET
. You can use this command to set multiple fields with their values stored at the key in the hash.
It’s easier to understand how the HMSET
command works by looking at an example:
1 | HMSET myhashkey first demo second sample third test |
To get all fields and values in the hash that are stored at the key, just use the command HGETALL
. You can see what the output looks like below:
1 2 3 4 5 6 7 8 | 1) "somefield" 2) "demo" 3) "first" 4) "demo" 5) "second" 6) "sample" 7) "third" 8) "test" |
Conclusion
If you’re using Redis to store data, understanding data types such as hashes is essential. Hashes offer an efficient way to store complex data in Redis. You can set the value for a hash field stored at the key using the HSET
command. In this article, we provided an overview of the Redis HSET command and provided some simple examples of its use. With our step-by-step instructions and examples, you’ll be prepared to use the HSET
command to manage hashes in your own Redis installation.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started