Redis Datatypes and Commands-Hash

Redis Datatypes and Commands – Hash

Redis stores the data as key value pair. Keys can be any binary value and it is represented as ASCII strings for easily understandable and readability purpose. Redis supports five different datatypes and anyone of the datatype can be represented as value.

datatypes

Redis supports five different datatypes and they are given below. Each datatype can be used in Redis with different set of commands.

  • String
  • List
  • Set
  • Sorted Set
  • Hash

Hash

Hashes in Redis, data are mapped into the fields. It maps between String fields and String values.

hset

The HSET is a command used to store the data maps between the fields and values. Use the below command to store the data by using HSET command.

Syntax
hset <keyname> <field> <value>

Example
hset players name John
HSET command

hget

The HGET is a command used to get the data that maps into the field by using an key name. Use the below command to fetch the values that maps into the field.

Syntax
hget <keyname> <field>

Example
hget players name
HGET command

hmset

The HMSET command is used to store multiple field and value data. Use the below command to store multiple hash data into the Redis.

Syntax
hmset <keyname> <field1> <value1> <field2> <value2>...N

Example
hmset players name John score 100 grade A
HMSET command

hmget

The HMGET command is used to fetch multiple values associated with the fields. Use the below command to fetch the values from the fields.

Syntax
hmget <keyname> field1 field2.. fieldN

Example
hmget players name score grade gender
HMGET Command

We don’t have gender field, so console will return NIL value for that field.

hgetall

The HGETALL command is used to get all the fields and values by using a key name. Use the below command to fetch all fields and values.

Syntax
hgetall <keyname>

Example
hgetall players
HGETALL command

hexists

The HEXISTS command is used to find out the field is exists or not by using the key name. Use the below command to find the field exists by using key.

Syntax
hexists <keyname> <field>

Example
hexists players name
HEXISTS command

hkeys

The HKEYS command is used to fetch all the fields in a key. Use the below command to get all the fields in a key.

Syntax
hkeys <keyname>

Example
hkeys players
HKEYS command

hvals

The HVALS command is used to get all the values in a key. Use the below command to fetch all the values.

Syntax
hvals <keyname>

Example
hvals players
HVALS command

hdel

The HDEL command is used to delete one or more fields in a hash by using a key name. Use the below command to remove the fields.

Syntax
hdel <keyname> <field1> <field2>...N

Example
hdel players name grade
HDEL command

After removed the fields, execute the below command to view the remaining fields in hash.

hkeys players
HKEY command

hlen

The HLEN is a command used to return the number of fields in hash. Use the below command to fetch the number of fields in hash.

Syntax
hlen <keyname>

Example
hlen players
HLEN command

We have learned and executed the Hash datatype in Redis.

How useful was this post?

Click on a star to rate it!

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

Advertisements

Post your comments