Redis HSET Data Type on ObjectRocket

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

Introduction

Hashes provide database managers with an efficient and effective way of storing complex data types in Redis. In Redis, a hash consists of many key value pairs, with each key and value being composed of a data string. The Redis HSET command is used to set a field in a hash stored as a key value. If the key has not been previously established, then a new key holding a hash will be produced. If the field already exists then it will be overwritten. While Redis hashes can hold multiple key-value pairs, hashes will not directly support complex values. However, a hash field can be used to point to other complex values. This tutorial will explain how to use the Redis HSET data type on ObjectRocket.

Prerequisites

  • Redis tools must be properly installed and configured on the local operating system to use the Redis HSET data type on ObjectRocket.

  • An instance of Redis must be set up for an ObjectRocket account via this link to the Mission Control panel. Here is a screenshot of the panel:

Image from Gyazo

  • Stunnel or the same open-source proxy for client and remote server connection must be properly installed and configured the on the local device.

NOTE: The connection is routed to localhost and port 8000.

Connecting to a Redis Server Instance on ObjectRocket

The format for the connection command to the Redis server instance on ObjectRocket is as follows:

1
redis-cli -h -hostname- -p -port-

In the above example, the redis-cli command is followed by the -h flag that will accept the hostname. This is followed by the -p flag that will accept the port number where the server is listening for the client connection.

Now execute the following command in the terminal:

1
redis-cli -h localhost -p 8000

As previously noted, the above connection details are the product of the stunnel configuration.

The results of the command should resemble this:

Screenshot

Now a connection to the Redis server instance on ObjectRocket can be established, authentication can be passed and a test ping command can be executed.

NOTE: The password can be found in the connection settings of the Redis server instance on the ObjectRocket Mission Control panel:

Screenshot

Redis HSET Command

The HSET Redis command sets the value to its respective field within the hash stored in the given key. It is important to note that if the field already exist within the Redis system, its corresponding value will be overwritten. If neither condition exists, then a new key to hold the hash will be created.

Here is the basic form of the HSET command:

1
HSET <key_name> <field> <value>

Following is an explanation of the above code:

  • First, the HSET command is called and followed by the key_name, this will also be the data type’s name.

  • Second, the field is specified that holds the value within the specified key.

  • Third, a value is provided that will be stored within the field specified earlier.

Note that different results can be expected upon executing the HSET command, depending on the configuration. Here are some possibilities:

  1. A 1 (integer) reply can occur if a field is new in the hash and value was set — This simply indicates that the field is a new one in the Redis system and will be stored accordingly.

  2. A 0 (integer) reply can occur if a field already exists in the hash and value was overwritten. — This notification indicates that the specified file name already exists in the Redis system, and as such it will be overwritten.

  3. An Error will occur if the key exists and the value stored in the key is not a hash. — This results in an error in the given command.

Here is an example:

1
2
localhost:8000> HSET OR_redis article_1483 HSET
(integer) 1

Notice that a notification of (integer)1 was received. This means the HSET command was successfully executed. Now execute the following command to verify and retrieve the value:

1
2
localhost:8000> HMGET OR_redis article_1483
1) "HSET"

Conclusion

This tutorial explained how to use the Redis HSET data type on ObjectRocket and provided examples for its use. The tutorial first covered the procedure used to connect to a Redis server instance on ObjectRocket and provided the format for the connection command to the Redis server instance. The article then explained the basic form of the Redis HSET command, provided an explanation of the code and explained how to verify and retrieve the value. It is important to remember that if a field already exist within the Redis system, its corresponding value will be overwritten. Also bear in mind that when using the Redis HSET data type on ObjectRocket that different results may be returned upon executing the HSET command, depending on the configuration or existence of the given field.

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.