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.

Redis Datatypes and Commands-String

Redis Datatypes and Commands – String

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

string

String is a datatype used in Redis to store the Key value pair. The value can be represented as String. String is a sequence of characters and binary safe in Redis.

set and get

The Set command is used to create a new key value pair data in Redis. Execute the below command on the Redis client to create a new value pair data.

Syntax
set <keyname> <value>

Example
set employee John

The Get command is used to fetch the value by using key name. Execute the below command on the Redis client to fetch the value by using key name.

Syntax
get <keyname>

Example
get employee

If you want to set the new value for existing key, then you can use the same Set command. The Set command doesn’t append the new value with existing one. It overrides the existing value with new one.

Example
set employee Mike
set and get commands

If you want to set the values for the key like employee First Name and Last Name, then use the below command on the Redis client. The value must be provided within double quotes.

Syntax
set <keyname> "<value>"

Example
set employee "John Mike"
The value provided within double quotes

naming a key

If you want to store the more than one employee details with unique ID, then use the key name like below.

Syntax
set <keyname>:<uniqueID> value

Example
set employee:1 John
set employee:2 Mike
set employee:3 Lawrence
Redis keys with unique Id

Execute the below command on the Redis client to fetch all available keys.

keys *
to view all keys

Setting up new key value pair data once again to see the new key on the list of available keys.

set student "James"
get student
setting up new key value pair

Now, execute the below command once again to view the list of available keys.

keys *
To view list of available keys

Use the below command, to fetch the list of available keys starts with sequence of characters.

Syntax
keys <pattern>

Example
keys emp*
keys starts with

time to live

Use the below command to set the expire time for the key. The expire time must be provided by using seconds or milliseconds.

Syntax
set <keyname> <value> ex <no:of:seconds>

Example
set employee:4 Kite ex 30

Use the below command to view the remaining time of the key after set the expire time. The command name is “Time To Live”. The negative value on the TTL represented key has expired. The NIL value will return, while fetching the value by using key after expired.

Syntax
ttl <keyname>

Example
ttl employee:4
TTL command

key not exists – nx

Use the below command to set key value pair data, if the key is not exists on the Redis database.

Syntax
set <keyname> <value> NX

Example
set employee:4 Fidel NX
Key Not exists

key exists – xx

Use the below command to set the key value pair data, if the key is already exists on the Redis database. If the Key is already not exists, then the Redis client will return “NIL”.

Syntax
set <keyname> <value> XX

Example
set employee:4 Nathan XX
set employee:5 Nathan XX
Key already exists

exists

Use the below command to check the key is already exists or not on the Redis database.

Syntax
exists <keyname>

Example
exists employee:4
exists employee:5

append

Execute the below command to append the value for existing key value pair data.

Syntax
append <keyname> <newvalue>

Example
set employee:5 Vikram
get employee:5
append employee:5 " Karthik"
get employee:5
Append command

string length

To find the length of the value by using key, execute the below command. The return value will be Integer.

Syntax
strlen <keyname>

Example
strlen employee:5
String length

incr and decr

Use the below command to Increment and Decrements the Integer value by using key. By default Increment increase the value by 1 and Decrements decreases the value by 1.

Syntax - Increment
incr <keyname>
incrby <keyname> <incrementvalue>

Syntax - Decrement
decr <keyname>
decrby <keyname> <decrementvalue>

Example
set testkey 1 
incr testkey  
incrby testkey 20 
decr testkey 
decrby testkey 10 
get testkey 
Increment and Decrements

mset and mget

Use the below commands, to set and get the multiple key value pair by using single command.

Syntax
mset <keyname1> <value1> <keyname2> <value2>
mget <keyname1> 
mget <keyname1> <keyname2>

Example
mset firstkey firstvalue secondkey secondvalue
mget firstkey
mget firstkey secondkey
mset and mget commands

We have learned the basic Redis String commands.