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 HGET
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 HGET

Introduction to Redis HGET

Redis HGET is used to return all values and fields which was stored in the key. At the time of returning any value every name of the field is followed by the value, so we can say that the reply length is double to the hash size. Basically, it is used for getting the values that were associated with the field into the key stored in the hash value.

Redis HGET

What is Redis HGET?

If the hget key does not exist, or if the key exists but the hash value does not contain the field that was specified, it will return nil as an output. If the key exists and is stored in the key, an error will be returned by using the datatype hash. When we use hget, we must first use hget to define the value by using redis hset. We can’t use redis hget without hset. When we use redis hget, we must define the key name and field. When we use the redis hset, we define the key name. The key name and field value is defined in redis hset, and the same key name is used in redis hget.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Redis HGET Key Field

At the time of using redis hget we need to use two parameters first is the key and another parameter is a field. The below syntax shows how we can define the key field in it.

Syntax:

HGET name_of_key name_of_field

The name of the key in the above syntax is simply the key name that we defined using the hset command. We have also defined the field name in redis by using the hset and hmset commands. The hset command is used in the following example to use the redis hget key. In the following example, the key name is hget key, and the key field is hget field. Also, the values of redis hget that are defined as “redis” are as follows.

Code:

HMSET hget_key hget_field "redis"
HGET hget_key hget_field

Output:

HGET Key Field

In the above example, we can see that after using the key field as hget_field, it will be retrieving all the values from the specified field. We can also use multiple field names with a single key at the time of using hget. In the below example, we can see that we are using multiple field names to define the redis hget. In the below example, we are using the key names redis_hget and field names hget_field1, hget_field2, hget_field3, and hget_field4. The below example shows the redis hget key field as follows.

Code:

HMSET redis_hget hget_field1 "val1" hget_field2 "val2" hget_field3 "val3" hget_field4 "val4"
HGET redis_hget hget_field1
HGET redis_hget hget_field3
HGET redis_hget hget_field4
HGET redis_hget hget_field2

Output:

Redis key names

In the above example, we can see that we are defining multiple key field name at the time of defining a single key. We can see that at the time of using hget and specifying the filed name the specified field value will be returned. It will not retrieve all the values which we have set into the redis field.

Redis Hash HGET Command

At the time using redis hget, we need to use the hset and hget commands. While using hget we need to use the first hset command. In the below example, we are not defining the key value so it will give the nil value. It will not provide any output. In the below example, we can see that we are defining the key name as hget_key and the field name as hget_field. But in the hget command, we are using the key name as hget_key1 and the field name as hget_field so it will retrieve the nil value.

Code:

HMSET hget_key hget_field "redis"
HGET hget_key1 hget_field

Output:

hget command

In the below example, we are not defining the key field so it will give the nil value. It will not provide any output. We can see that we are defining the key name as hget_key and the field name as hget_field. But in the hget command, we are using the key name as hget_key and the field name as hget_field1 so it will retrieve the nil value.

Code:

HMSET hget_key hget_field "redis"
HGET hget_key hget_field1

Output:

Nil value

In the below example, we are defining the key name as hget_key and the field name as hget_field. Also in the hget command, we are using the key name as hget_key and the field name as hget_field so it will retrieve the value as redis.

Code:

HMSET hget_key hget_field "redis"
HGET hget_key hget_field

Output:

it will retrieve the value

In the below example, we are using multiple key fields to define the redis hget command. Also as per filed name, we are defining the specified value. We are retrieving the value as per the field as follows.

Code:

HMSET hget_multiple field_multiple1 "hget_val1" field_multiple2 "hget_val2" field_multiple3 "hget_val3" field_multiple4 "hget_val4"
HGET hget_multiple field_multiple1
HGET hget_multiple field_multiple2
HGET hget_multiple field_multiple3
HGET hget_multiple field_multiple4

Output:

retreving the value

We can also use the hset command with hget. The below example shows the hset command with hget as follows.

Code:

HSET hget_key1 hget_field1 "redis"
HGET hget_key1 hget_field1

Output:

hset command with hget

We can use hgetall command to display all the fields as follows. In the below example, we are using hgetall.

Code:

HMSET hget_multiple field_multiple1 "hget_val1" field_multiple2 "hget_val2" field_multiple3 "hget_val3" field_multiple4 "hget_val4"
HGETALL hget_multiple

Output:

display all the fields

Redis HGET Add and Remove Elements

The hset command is used to add the key-value pair to the hash, if hash does not exist then it will create new hash. The hget command is used to retrieve the value from the key which was specified. We are also using hkeys for retrieving the key which was present into the hash. Below example shows add and remove elements by using hget as follows.

Code:

import redis
redisClient = redis.StrictRedis (host='localhost', port=6379, db=0)
redisClient.hset ("NumberVsString", "11", "Eleven")
redisClient.hset ("NumberVsString", "12", "Twelve")
print("Key val twelve")
print(redisClient.hget("NumberVsString", "12"))
print ("Hash key present:");
print(redisClient.hkeys ("NumberVsString"))
print("Redis hash value:");
print(redisClient.hvals ("NumberVsString"))
print("Redis hash key:")
print(redisClient.hgetall ("NumberVsString"))

Output:

Redis HGET - Remove elements

The below example shows redis get add and remove elements as follows. We are using the hgetall method as follows.

Code:

import redis
import redis
r_cli = redis.StrictRedis (host='localhost', port=6379, db=0)
h_name = "Color"
r_cli.hset (h_name, 1, "Black")
r_cli.hset (h_name, 2, "White")
print (r_cli.hgetall(h_name))
r_cli.hdel(h_name, 1)
print(r_cli.hgetall(h_name))

Output:

get add and remove elements

FAQ

Given below are the FAQs mentioned:

Q1. What is the use of redis HGET?

Answer: It is basically used to return the value associated with the stored key and the field.

Q2. Which methods we are using with redis HGET?

Answer: At the time of using it we need to define the method name as hset and hmset. We can use any of the methods for the same.

Q3. Which parameter do we need to pass at the time of using redis hget?

Answer: We need to pass the key name and field name at the time of using it.

Conclusion

If hget key does not exist, or if the key has existed and the hash value does not contain the field which was specified then it will return the nil as an output. Basically, it is used for getting the values that were associated with the field into the key stored in the hash value.

Recommended Articles

This is a guide to Redis HGET. Here we discuss the introduction, key field, redis HGET add and remove elements, and FAQ. You may also have a look at the following articles to learn more –

  1. Redis Get All Keys
  2. Redis GUI
  3. Redis Pubsub
  4. Redis Version
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