Redis HSET Command

Discover how you can create and manage redis SETS using the HSET command.

Redis HSET Command

If you're looking for an easy way to store data in Redis, the hset command is perfect. For example, with this command, you can store key-value pairs very easily. This article will discuss how to use the hset command and some of its features.

So, what is Redis hset? In simple terms, it is a command that allows you to store data in a hash table. A hash table is a data structure that holds key-value pairs. The keys help to look up values in the table. The values can be anything from strings to integers to lists.

Redis hset syntax

The hset command is easy to use. All you got to do is specify the key and value you want to store.

HSET key field value

For example, if we wanted to store the value "#ff0000" under the field "red", we would use the following command:

127.0.0.1:6379> HSET color red #ff0000
Returns (interger) 1

(integer) 1 means that the hash creation is successful.

Keep in mind that failure to provide all the arguments leads to an error:

(error) ERR wrong number of arguments for 'hset' command 

Reading data from a hash (Redis HGET)

Let's say we want to retrieve the value of "red". We would use the following command:

127.0.0.1:6379> HGET color red
Returns "#ff0000"

Rewriting the value using Redis HSET

If we wanted to change the value of the red field to #aa0000, we would use the HSET command like this:

127.0.0.1:6379> HSET key red #aa0000

The new value of the red field changes from "ff0000" to "aa0000". Running this command on an already existing hash overwrites the existing data with the new data.

confirmation:

127.0.0.1:6379> HGET color red
Returns "#aa000"

As you can see, HSET allows us to create or rewrite the data in a hash efficiently. But what if we want to set multiple fields at once? That's where HMSET comes in.

Using Redis HMSET

HMSET is similar to HSET but allows us to set multiple fields simultaneously.

The syntax for HMSET is as follows:

HMSET key field value [field value ...]

For example, if we wanted to set the values of the red, blue, and green fields all at once, we would use the HMSET command like this:

127.0.0.1:6379> HMSET color red #aa0000 blue #0000ff green #00ff00
Returns OK

The command return "OK" meaning that it was successful. We can confirm this with the HGETALL command.

Using Redis HGETALL command

The HGETALL command takes the key as the argument.

The syntax for HGETALL is as follows:

HGETALL key

Let's see it in practice.

127.0.0.1:6379> HGETALL color
Returns  the field and value
1) "red"                                                                              
2) "#aa0000"                                                                          
3) "blue"                                                                             
4) "#0000ff"                                                                          
5) "green"                                                                            
6) "#00ff00" 

That's all there is to using the hset command! As you can see, it's simple and easy to use.

Note

There are a few things to note when using hset.

  1. The keys are case-sensitive, meaning that "Foo" and "foo" are two different keys
  2. hset only works with strings. If you try to store other data types (such as integers or lists), they will be converted to strings.

Conclusion

As you can see, hset is a very versatile command that is useful in various applications. If you're looking for an easy way to store key-value pairs, hset is the perfect solution. Give it a try in your projects and see how it can help you simplify your data storage needs.

Thanks for reading!

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.