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.

Advertisements

Redis Datatypes and Commands-Sorted Set

Redis Datatypes and Commands – Sorted Set

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

Sorted set

The Sorted Set datatype is used to maintain the order of the elements in ascending order.

zadd

The ZADD command is used to add the data into the key set. Use the below command to add the data into the key set.

Syntax
zadd <keyname> <score1> <member1> <score2> <member2> ... N

Example
zadd players 20 John 30 Amar 10 Mike
ZADD command

zrange

The ZRANGE command is used to view all the elements from the ordered key set. Use the below command to view all the elements from the key set.

Syntax
zrange <keyname> start stop

Example
zrange players 0 -1
ZRANGE command

The Start and Stop represents the index of the elements. The 0 to -1 represents to view all the elements from the set.

Execute the below command, to view all the elements from the set with “WITHSCORES” option.

Syntax
zrange <keyname> start stop withscores

Example
zrange players 0 -1 withscores
Zrange with scores

zcard

The ZCARD command is used to return the number of elements from the ordered key set. Use the below command to view the number of elements from the set.

Syntax
zcard <keyname>

Example
zcard players
ZCARD command

zcount

The ZCOUNT command is used to return the count of elements from the ordered key set. Use the below command to view the count of elements from the key set.

Syntax
zcount <keyname> -inf +inf

Example
zcount players -inf +inf
ZCOUNT command

zrank

The ZRANK command is used to find out the index of the element in the ordered key set. Use the below command to get the index of the element.

Syntax
zrank <keyname> member

Example
zrank players Amar
ZRANK command

zscore

The ZSCORE command is used to return the score of the member from the ordered key set. Use the below command to return the score of the member.

Syntax
zscore <keyname> member

Example
zscore players Amar
ZSCORE command

zrem

The ZREM command is used to remove the elements from the ordered set. Use the below command to remove the elements from the set.

Syntax
zrem <keyname> member

Example
zrem players Amar
ZREM command

Execute the below command to get the all the elements from the ordered set after remove the member from the key set.

zrange players 0 -1
ZRANGE command

We have learned and executed the Redis Sorted Set datatype commands.

Redis Datatypes and Commands-Set

Redis Datatypes and Commands – Set

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

set

Set is a one of the Redis Datatype and it is a collection of unique strings stored in an unordered list. Set doesn’t allow duplicate data in a key.

sadd

The SADD command is used to store the data into the set by using a key. Use the below command to add the data into the set.

Syntax
sadd <keyname> <member1> <member2> <member3>...N

Example
sadd notes note1 note2 note3
SADD command

smembers

The SMEMBERS command is used to get the data from the set by using a key. Use the below command to get the data from the set.

Syntax
smembers <keyname>

Example
smembers notes
SMEMBERS command

scard

The SCARD command is used to get the number of elements count from the set by using a key. Use the below command to get the number of elements count from the set.

Syntax
scard <keyname>

Example
scard notes
SCARD command

sdiff

The SDIFF command is used to find out the difference between two key sets.

Let us add the new key set and will execute the SDIFF command.

 sadd new_notes note3 note4 note5 
SADD command

Use the below command to find out the difference between two key sets.

Syntax
sdiff <keyname1> <keyname2>

Example
sdiff notes new_notes
SDIFF command

Try out to find the difference between two key set in another way.

sdiff notes new_notes notes
SDIFF command

sunion

The SUNION command is used to return all unique data between two key sets. Use the below command to return all unique data between two key sets.

Syntax
sunion <keyname1> <keyname2>

Example
sunion notes new_notes
SUNION command

sinter

The SINTER command is used to return the common data between two key sets. Use the below command to return the common data between the two keys.

Syntax
sinter <keyname1> <keyname2>

Example
sinter notes new_notes
SINTER command

spop

The SPOP command is used to remove the random data from the set. Use the below command to remove the random data from the key set.

Syntax
spop <keyname>

Example
spop notes
SPOP command

srem

The SREM command is used to remove the one or multiple data from the key set. Use the below command to remove the data from the key set.

Syntax
srem <keyname> <member1> <member2>..N

Example
srem new_notes note4 note5
SREM command

After remove the data from the key set, and execute the below command to see the remaining elements from the key set.

smembers notes
SMEMBERS command

We have learned and executed the Redis Set datatype and it’s commands.