How to Use Redis HKEYS
Introduction
If you’re just getting started with Redis, you may not yet have spent much time working with hashes. However, you’ll soon find that these structures can be incredibly useful for grouping related data. In this article, we’ll discuss the Redis HKEYS command– a handy tool that allows you to query for all keys of a particular hash.
Prerequisites
Before we dive any deeper into our discussion of the Redis HKEYS command, it’s important to review any prerequisites that must be in place. For this task, you only need to ensure that Redis has been properly configured on your system and that you have some basic knowledge of Redis commands.
Redis Overview
The Remote Dictionary Server, commonly known as Redis, is a highly performant, in-memory, key-value data store. Not only is Redis commonly used as a database, but it’s also utilized for caching and message brokering. What sets Redis apart from other databases is its ability to handle high-level data types, along with its lightning-fast speed and in-memory storage. These benefits make Redis an effective solution in many cases where a traditional relational database falls short.
What is a Redis Hash?
Redis hashes allow you to save groups of key-value pairs mapped to an individual higher-level Redis key. You can store values in hashes that are similar to the values of regular STRING(s) or the actual STRING(s) themselves; the value can also be read as a numeric value or a number that can be increased or decreased. A Redis hash is sometimes described as a scaled-down version of Redis itself due to the structural similarities between the two.
How to Store Values in a Hash
Now that we’ve talked about what a Redis hash is, let’s discuss some commands that can be used to manipulate a hash. We’ll start by discussing how to set the values of fields within a hash.
To accomplish this task, we’ll be using the hash command HSET
. This command will set a field within the hash mapped to a certain key. A brand-new key that holds a hash will be created if there is no key exists; however, the key will be overwritten if one already exists.
The HSET
command will return the value “1” if the field is a newly created field within the hash. It will return “0” if the field is already present within the hash and will be updated.
We can use the following syntax for the Redis command HSET
:
1 | 127.0.0.1:6379> HSET KEY_NAME FIELD VALUE |
Shown below is an example of how to use the HSET
command:
1 2 | 127.0.0.1:6379> HSET sample_hash field1 "yeshua" (integer) 1 |
How to Retrieve a Value from a Hash
So far, we’ve discussed how hashes work in Redis, and we looked at an example of how to use the HSET
command to set fields within a hash. In this section, we’ll build on that knowledge and look at the Redis HKEYS command, which returns all the keys of a hash. If the hash is empty or doesn’t exist, the command will return an empty list.
We use the following syntax for the Redis command HKEYS
:
1 | 127.0.0.1:6379> HKEYS KEY_NAME |
Remember that we set a field in our hash using HSET
in the previous section. Now, we will use the HKEYS
command to retrieve that.
1 | 127.0.0.1:6379> HKEYS sample_hash |
The result should look something like this:
1 | 1) "field1" |
We can see that we were able to retrieve the key named field1
, which we set in the previous section.
Conclusion
It’s clear that Redis hashes offer a simple and powerful way to map keys to values. In this article, we provided an overview of hashes and how they work; we also discussed the Redis HKEYS command, which allows you to retrieve all keys in a specific hash. With the examples provided in this article, you’ll be prepared to harness the powers of hashes and use the Redis HKEYS command in your own applications.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started