Redis Hash - Add, Remove Elements Using Python And Redis-py

Overview:

  • Redis is a key value store and supports several data structures.
  • Redis keys are always strings and they are binary safe.
  • With Redis, at a higher-level string values can be stored against string keys.
  • Separate hash maps can as well be created with Redis, which are called as Hashes and they store several key-value pairs.

Adding values to a Redis Hash and retrieving them using Python and Redis-Py:

  • The HSET command adds a key-value pair to a hash. If the hash does not exist one will be created. If a key already exists, the value for the key is set to the specified value.
  • The HGET command retrieves the value for a specific key in a hash.
  • The Redis command HGETALL retrieves all the keys and their values present in a hash. The redis-py returns the keys and values as a Python dictionary.
  • The Redis command HKEYS retrieves all the keys present in a hash. The redis-py returns the keys as a Python list.
  • The Redis command HVALS retrieves all the keys present in a hash. The redis-py returns the values as a Python list.
  • The examples here use the commands HGETALL, HKEYS and HVALS, as the data used is very less. However, in a production environment the hashes can be much bigger in size hence these commands will block till all the elements are retrieved. To avoid blocking behavior the commands SCAN and HSCAN can be used which allow only a small number of elements to be retrieved on each call.

Example:

# import the Redis client

import redis

 

# Create a redis client

redisClient = redis.StrictRedis(host='localhost',

                                port=6379,

                                db=0)

                               

# Add key value pairs to the Redis hash

redisClient.hset("NumberVsString", "1", "One")

redisClient.hset("NumberVsString", "2", "Two")

redisClient.hset("NumberVsString", "3", "Three")

 

# Retrieve the value for a specific key

print("Value for the key 3 is")

print(redisClient.hget("NumberVsString", "3"))

 

print("The keys present in the Redis hash:");

print(redisClient.hkeys("NumberVsString"))

 

print("The values present in the Redis hash:");

print(redisClient.hvals("NumberVsString"))

 

print("The keys and values present in the Redis hash are:")

print(redisClient.hgetall("NumberVsString"))

 

Output:

Value for the key 3 is

b'Three'

The keys present in the Redis hash:

[b'1', b'2', b'3']

The values present in the Redis hash:

[b'One', b'Two', b'Three']

The keys and values present in the Redis hash are:

{b'1': b'One', b'2': b'Two', b'3': b'Three'}

 

Removing values from a Redis Hash using Python and Redis-Py:

  • The Redis command HDEL removes a key-value pair identified by a specific key.

 

Example:

# import the Redis client

import redis

 

# Create a redis client

redisClient = redis.StrictRedis(host='localhost',

                                port=6379,

                                db=0)

                               

# Add key value pairs to the Redis hash

hashName = "Dessert"

redisClient.hset(hashName, 1, "Cheesecake")

redisClient.hset(hashName, 2, "Apple Pie")

 

# Print the hash

print(redisClient.hgetall(hashName))

 

# Remove a key

redisClient.hdel(hashName, 1)

 

# Print the hash after removing a key

print(redisClient.hgetall(hashName))

 

 

Output:

{b'1': b'Cheesecake', b'2': b'Apple Pie'}

{b'2': b'Apple Pie'}

 

Get the properties of a Redis Hash:

  • The HEXISTS command checks and return an integer whether a specified key exists on a Redis hash. In case of redis-py it returns a Boolean value – TRUE or FALSE.
  • The HLEN command returns the number of fields defined for a key in a specific Redis hash.

 

Example:

# import the Redis client

import redis

 

# Create a redis client

redisClient = redis.StrictRedis(host='localhost',

                                port=6379,

                                db=0)

                               

# Defines a Redis hash

hashName = "Authors"

redisClient.hmset(hashName,  {1:"The C Programming Language", 2:"The UNIX Programming Environment"})

 

# Check if a key exists in a Redis key

key = 1

print("Does the key {}, exists:".format(key))

print(redisClient.hexists(hashName, key))

 

# Print the key value pairs of the Redis hash

print("Before deletion of a key:")

print(redisClient.hgetall(hashName))

 

# Remove a key

redisClient.hdel(hashName, key)

 

# Print the key after a key-value is removed

print("After deletion of a key:")

print(redisClient.hgetall(hashName))

 

Output:

{b'2': b'The UNIX Programming Environment', b'1': b'The C Programming Language'}

After deletion of a key:

{b'2': b'The UNIX Programming Environment'}


Copyright 2020 © pythontic.com