Updated February 18, 2023
Introduction to Redis HSET
Redis HSET command is primarily used to set column fields in pre-defined stored areas like hash keys to values and to hold the hash values which returns the integer called 0 and 1 if the return is 0 then it shows already existed on the hash and value otherwise if the return is 1 the hash and value was set.
Key Takeaways
- HSET is the hash table’s keyword for storing values in key format.
- HASHes and standard STRINGs both support the same set of values for storage.
- Maps between string fields and string values are Redis Hashes.
- As a result, they are employed to symbolize objects.
- Redis’s HMSET and HGETALL commands are used, and the key is user 1.
What is Redis HSET?
Redis HSET is both a command and a data type that can be used to store a hash map. The key-value pairs relate to a complex set of data items and contain a range of key-value pairs with limited regions. Data like id, name and other details are stored in redis hashes. Redis hashes are referred to as the fundamental data types in order to perform operations on replicated tables with document regions for one table to another table that are mapped to connect each other. The term “relational database management system” refers to this kind of system because of this. In order to rearrange the data, it can also access one or more area fields simultaneously.
How to Create Redis HSET?
For hash storage, the sets field acts as the storage area and, in the absence of an existing key, retains the new key. Additionally, the database’s cached hash index for each column was changed. Before using the HSET command to set a field in the hash set, the key-value pair Redis tool must be properly installed and set up. It is appropriate at the column field for related values that are rewritten to hold the key value reference. Use the HSET command to activate the key, which was generated and validated behind the scenes.
The HGET command will also retrieve the key value for a specific hash index, just like how HGETALL retrieves the keys and values on the hash. Values can be returned in any language, including java, python, php, etc. We can also use web-based languages to perform web tasks like sending and retrieving requests. Depending on the object and dictionary used to refer to the data, user requests may vary. The redis HSET field would continue to use hash values if the key supplied in the pre-defined redis-cache key does not exist.
Basic syntax for HSET creation is:
Hset key field or columnname value
Steps to create Redis HSET:
1. Login to the Ubuntu terminal.
2. Enter the below command for setting the hset or hashset on the redis cache.
Command:
Hset new id 1
3. Here the hset is the keyword which is defined as the hashset and the new is the key whereas the id is the column name, and its value is 1.
4. So, the new key is the id column along with value 1.
5. We can retrieve the hset column values by using the command called hmget. Here the example for the above hset value retrieve is:
Command:
Hmget new id
Redis HSET Key
This command makes it possible to use keys to set the values of column fields in hash memory. Each key is distinct and has a value of its own. Other commands like hmget and hgetall, as well as established methods for carrying out the tasks, can also be used to retrieve the keys.
The above screenshot shows complete steps to perform the hset operations on the redis database cache. Each key has a separate index, and the values are stored and retrieved from the database cache.
Redis HSET Data Type
It supports different data types and each has a separate function to trigger the operations.
Strings and Hashes:
Even though numeric kinds are provided, we are aware that a string is made up only of characters or alphabetical formats. It is a crucial type, and the hset operations are carried out using the key-value pairs format.
The above diagram shows to set the hash key along with its value. Here we can set n number of keys along with value each key has its own unique value, and the index is different.
Redis HSET Command
In the hash, add a value to the field (key). The hash will be created if it doesn’t already have one. In the event that the field (key) already exists in an existing hash, the new value will take its place. The Redis Hset command is used to hash the table field assignments. The HSET procedure builds a new hash table if one doesn’t already exist. If the field already exists in the hash table, the old value will be replaced. A hash table returns 1 if a new field is added and set to the success value. If the hash table for the domain field already exists and the new value overrides the previous one, return 0.
The above diagram explains the hash key operations like key-value pair format. And also multiple key-values pairs are associated with the same operation that denoted the sub-key and its value.
Example of Redis HSET
Given below is the example mentioned:
Command:
hset first name siva
Output:
Command:
hget first name
Output:
Command:
hmset new id 1 empname sivaraman empdesg softwaresupportengineer
Output:
Command:
hgetall new
Output:
The above example describes step by step procedure for performing the hashset operations.
FAQ
Given below are the FAQs mentioned:
Q1. What is HSET?
Answer:
Redis HSET command assigns a value to each field within the hash that is stored with the specified key.
Q2. What is the syntax for HSET?
Answer:
HSET key column name value.
Example:
Command:
HSET sept27 id 1
Output:
Q3. Whether a field that already exists in the hash is overwritten.
Answer:
Yes, the field is overwritten if the hash key exists.
Conclusion
Understanding data types like hashes is crucial when using Redis to store data. Redis provides an effective method for storing complex data: hashes. The HSET command can be used to change the value of a hash field kept with the key. A general introduction of the Redis HSET command and some straightforward illustrations of how the operation is to be performed.
Recommended Articles
This is a guide to Redis HSET. Here we discuss the introduction, redis HSET key, data type, command, example, and FAQ. You may also have a look at the following articles to learn more –