EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis HGETALL
Secondary Sidebar
Redis Tutorial
  • Redis
    • Redis Pubsub
    • Redis GUI
    • Redis Database
    • Redis Get All Keys
    • Redis TTL
    • Redis EXPIRE
    • Redis Version
    • Redis EXPIRE
    • Redis List
    • Redis CLI Commands
    • Redis HSET
    • Redis FLUSHDB
    • Redis Key
    • Redis Persistence
    • Redis FLUSHALL
    • Redis pipeline
    • Redis SADD
    • Redis ZADD
    • Redis ZSET
    • Redis Get
    • Redis Replication
    • Redis Delete All Keys
    • Redis Use Cases
    • Redis HMSET
    • Redis Transactions
    • Redis Monitor
    • Redis HGET
    • Redis SETEX
    • Redis Rails
    • RedisJSON
    • Redis Get Key
    • Redis Auth
    • Redis gem
    • Redis Key Count
    • RedisTimeSeries
    • Redis Timeout
    • Redis LPUSH
    • Redis Architecture
    • Redis API
    • Redis Message Queue
    • Redis RPUSH
    • Redis Protocol
    • Redis Data Types
    • Redis Namespace
    • Redis ZRANGE
    • Redis Master Slave
    • Redis Memory Usage
    • Redis WATCH
    • Redis URL
    • Redis Key Value
    • Redis INCR
    • Redis EXISTS
    • Redis INFO
    • Redis HMGET
    • Redis Operator
    • Redis NOAUTH Authentication Required
    • Redis LRANGE
    • Redis RDB
    • Redis TLS
    • Redis HDEL
    • Redis WebSocket
    • Redis HGETALL
    • Redis GETSET
    • Node-RED WebSocket

Redis HGETALL

Introduction to Redis HGETALL

Redis HGETALL returns the value and field from the specified key that we used in the HGETALL command. Each field name in a returned value is followed by using value, so the reply length is twice the size of the specified hash. When working with a redis hash key, we use the HGETALL command to retrieve all of the values and fields from the hash.

Redis HGETALL

Key Takeaways

  • Redis HGETALL is an important command that is used to get all the fields and values from the specified key that we used with the HGETALL command.
  • If key is not present in the database, the redis HGETALL function returns an empty array as an output.

What is Redis HGETALL?

The redis HGETALL command is used to return the field and its specified value from the key used in the HGETALL command. The operation returns an array that is twice the size of the field number because it is filled with the values and fields of each field. The field is following the order of its respective value.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

When the HGETALL command is executed on a non-existing field value, it returns the empty set value. As we know, the keys command is used to get all the keys from the Redis database; however, it only displays the key name. The command is required to display the key name, as well as its field and value.

Redis HGETALL Command

At the time using HGETALL we need to create the key and its field value by using the hset command. If the key does not exist then it will return the empty set.

Below syntax shows the redis HGETALL command as follows:

HGETALL name_of_key

The redis HGETALL command syntax contains only one parameter with HGETALL command.

Below is the parameter description of the redis HGETALL command as follows:

1. HGETALL – This is the redis command that is used to display the field and value of the specified key that we are using with this command.

2. Name of key – This is the key name that we use with the HGETALL command. If the key exists in the database, the HGETALL command returns its field and value; if the key does not exist in the database, the command returns an empty set.

The below example shows how redis HGETALL is working. In the below example, we are creating the key name as key_HGETALL and we are defining the field as field1, field2, and field3. Also, we are defining the value of the field as val1, val2, and val3 respectively. After creating the key and assigning the value and field we are executing the HGETALL command on key name as key_hgetall as follows.

Command:

HSET key_hgetall field1 val1 field2 val2 field3 val3
HGETALL key_hgetall

Output:

Redis HGETALL Command

In the below example, we are adding more fields into the key name as key_hgetall. We are adding the field as field4, field5, and field6. Also, we are defining the value of the field as val4, val5, and val6 respectively. After adding the field and value we are executing the HGETALL command, we can see that it is showing all the fields and values for which a key exists.

Command:

HSET key_hgetall field4 val4 field5 val5 field6 val6
HGETALL key_hgetall

Output:

Redis HGETALL - Fields and Values

Redis HGETALL Key

Redis HGETALL key contains the key which we are using with HGETALL command. At the time of using HGETALL command in redis, the key is an important parameter. Below example shows how key is working with HGETALL command. In the below example, we are creating the key name as key_hgetall2 and defining the field name as hgetall1 and hgetqall2, also we are assigning the value as val1 and val2.

Command:

HSET key_hgetall2 hgetall1 val1 hgetall2 val2
HGETALL key_hgetall2

Output:

Redis HGETALL Value

In the below example, we are deleting the field from key_hgetall1 key. After deleting the field from the key, we can see that the HGETALL command will return the empty array list in the output.

Command:

HSET key_hgetall1 field1 val1
HDEL key_hgetall1 field1
HGETALL key_hgetall1

Output:

deleting the field

If the key does not exist in the database, the command returns an empty array in output. In the below example, we are using the key name key_hgetall12 with the HGETALL command, this key does not exist in the database, so it will return the empty array as follows.

Command:

HGETALL key_hgetall12

Output:

Redis HGETALL - Empty Array

Redis HGETALL vs HGET

Basically, the main difference between HGETALL and HGET is that HGETALL is used to retrieve all the values as fields from the specified key, whereas the hget command is used to retrieve the specified field and value from the specified key. Hget does not accept multiple fields whereas HGETALL accepts all the fields which existed in the specified key.

The below example shows how HGETALL works with specified key. We are using the key_hgetall21 key as follows:

Command:

HSET key_hgetall21 field1 val1 field2 val2 field3 val3
HGETALL key_hgetall21

Output:

Specified Key

The below example shows how hget is working with the specified keys. We are using the key_hgetall22 key as follows. We can see that we need to use a specified field with the redis hget command.

Command:

HSET key_hgetall22 field1 val1 field2 val2 field3 val3
HGET key_hgetall22 field1

Output:

Redis HGETALL - Specified Field

To use the command, we need to pass only a single parameter i.e. name of the key. At the time using the hget command in redis we need to use the key name as well as the specified key parameter.

Examples of Redis HGETALL

Given below are the examples of redis HGETALL:

Example #1

In the below example we are creating the key name as hgetall_exp, at the time of creating the key we are defining the field name as field1 and field2, also assigning the value as val1 and val2 respectively.

Command:

HSET hgetall_exp field1 val1 field2 val2
HGETALL hgetall_exp

Output:

Defining the field name as field1 and field2

Example #2

In the below example, we are using the key name hgetall_exp1, but this key does not exist in the database, so it will return an empty set as follows.

Command:

HGETALL hgetall_exp1

Output:

return an empty set

Example #3

In the below example, we are using the key name hgetall_exp1, this key is present in the DB but the key field does not exist in the database so it will return the empty set as follows.

Command:

HGETALL hgetall_exp1

Output:

Redis HGETALL - Key name

Conclusion

The redis HGETALL operation returns an array that is twice the size of the field number because it is filled with the values and fields of each field. It returns the value and field from the specified key that we used in the HGETALL command.

Recommended Articles

This is a guide to Redis HGETALL. Here we discuss the introduction, redis HGETALL command & key, examples respectively. You can also look at the following articles to learn more –

  1. Redis TLS
  2. Redis RDB
  3. Redis INFO
  4. Redis ZRANGE
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more