Redis Hash Commands with Example

Redis Hash Commands With Example

In the previous blog on Redis, we learned the basic commands in Redis like get, set, del, etc. This time we will learn Redis Hash Commands with example.

What are Hashes?

Hashes are maps between the string field and the string values. You can also relate it as an object in programming.

Redis Hash
Redis Hash

In Redis HASHes can store upto 4 billion field-value pairs.

Set Multiple Field Value in Redis Hash

HMSET in Redis is used to set multiple field value pairs.

hmset key field value [field value ...]

$ 127.0.0.1:6379> hmset student:1 name "Divyanshu Shekhar" rollNo   17 class 10
  OK

Set Field in Hash

Redis HSET is used to set a Field in a pre-existing Redis Hash.

HSET Key Field Value

$ 127.0.0.1:6379> hset student:1 subject "CS"
  (integer) 1
$ 127.0.0.1:6379> hgetall student:1
  1) "name"
  2) "Divyanshu Shekhar"
  3) "class"
  4) "10"
  5) "subject"
  6) "CS"

Set Field if it doesn’t exist – Redis HSETNX

Redis HSETNX is used to first check if the field exists in the Hash. If the key doesn’t exist then it adds the field in the Hash else drops it.

HSETNX Key Field Value

$ 127.0.0.1:6379> hsetnx student:1 marks 99
  (integer) 1
$ 127.0.0.1:6379> hgetall student:1
  1) "name"
  2) "Divyanshu Shekhar"
  3) "marks"
  4) "99"

Get a Single Field Value from Hash

Redis HGET command is used to get a single field’s value from a Redis Hash.

HGET key field

$ 127.0.0.1:6379> hget student:1 name
  "Divyanshu Shekhar"
$ 127.0.0.1:6379> hget student:1 class
  "10"

Get all Fields & Values from a Hash

Redis HGETALL command is used to display all the field and values of a Hash.

HGETALL key

$ 127.0.0.1:6379> hgetall student:1
  1) "name"
  2) "Divyanshu Shekhar"
  3) "rollNo"
  4) "17"
  5) "class"
  6) "10"

Get all the Fields in Redis Hash

Just like key * was used to get all the keys, we can get all the Fields using the Redis HKEYS Command.

HKEYS Key

$ 127.0.0.1:6379> hkeys student:1
  1) "name"
  2) "marks"

Get Specific Field Values in Redis Hash

When the Hash gets big it’s difficult to view all the fields and thus we need some kind of command that filters the Fields.

Redis HMGET Command display only the Fields which you want to see.

HMGET Key Field [Field ...]

$ 127.0.0.1:6379> hmget student:1 name marks
  1) "Divyanshu Shekhar"
  2) "109"

Get all the Values in a Redis Hash

Redis HVALS Command is used to get all the Values of a Redis Hash.

$ 127.0.0.1:6379> hvals student:1
  1) "Divyanshu Shekhar"
  2) "99"

Check if Field Exists in Redis Hash

HEXISTS in Redis, lets us know whether a Key exists in a Redis Hash or not.

HEXIST Key Field

$ 127.0.0.1:6379> hexists student:1 name
  (integer) 1
$ 127.0.0.1:6379> hexists student:1 firstname
  (integer) 0

Redis HEXISTS returns 1 if the field exists in the HASH else it returns 0.

Total Number of Fields in Redis Hash

HLEN Command in Redis returns the total number of fields in the Redis Hash.

$ 127.0.0.1:6379> hlen student:1
  (integer) 4

Delete a Redis Hash Field

Redis HDEL Command lets us delete a Field from a HASH.

HDEL Key Field [Field ...]

$ 127.0.0.1:6379> hdel student:1 rollNo
  (integer) 1
$ 127.0.0.1:6379> hgetall student:1
  1) "name"
  2) "Divyanshu Shekhar"
  3) "class"
  4) "10"

Delete Multiple Fields in Redis Hash

Redis HDEL take also delete more than one Field in a Redis Hash.

$ 127.0.0.1:6379> hgetall student:1
  1) "name"
  2) "Divyanshu Shekhar"
  3) "class"
  4) "10"
  5) "subject"
  6) "CS"
$ 127.0.0.1:6379> hdel student:1 subject class
  (integer) 2
$ 127.0.0.1:6379> hgetall student:1
  1) "name"
  2) "Divyanshu Shekhar"

Redis Hash Increment Command

Redis HINCRBY Command performs increment on a field value in the HASH.

$ 127.0.0.1:6379> hincrby student:1 marks 10
  (integer) 109

HDECRBY Command doesn’t exist in Redis. Use the Negative value in HINCRBY to perform subtraction.

Learn more about Redis Hash Commands from Redis official Documentation.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *