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.
Redis supports five different datatypes and they are given below. Each datatype can be used in Redis with different set of commands.
- Sorted Set
Hashes in Redis, data are mapped into the fields. It maps between String fields and String values.
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
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
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
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
We don’t have gender field, so console will return NIL value for that field.
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
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
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
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
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
After removed the fields, execute the below command to view the remaining fields in hash.
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
We have learned and executed the Hash datatype in Redis.