Friday, 25 November 2016

redis data types and basic commands for each in redis

Sometime when working with redis, we need to know the data type of a particular key in redis.
This is useful to know what kind of commands should be executed against that key.
A key can have any one of the below data types.
  1. String
  2. Hash
  3. List
  4. Set
  5. Sorted Set
  6. HLL(they are stored as string types internally)
The "TYPE" command in redis can be used to find out type of a particular key.

Lets say we have a redis instance and we find out the keys by executing the "keys *" command. For simplicity, i have separate types of keys in redis.


For each of these keys, the 'type' command will give the type of the key.


For performing basic view operations on each of the types of redis keys, below are the types and commands for each.

String
          GET  command in redis can be used to get the data in a string key.

 src/redis-cli GET mystringkey

    


Hash

         HGETALL command can be used to view the data in a hash. It returns a list of values, which consists of the key followed by the value of that key.  Like in the below, in the key "myhashkey", field1 has value value1 while field2 has value value2. Hashes are normally stored to store an object which can have many fields, like a user can have various fields like name, age, gender, etc, so that the key can be user:<userid>, fields can be name, age, gender etc and values can be 'Jurgen', 50, 'm'

src/redis-cli HGETALL myhashkey



List
        Lists in redis are infact linked list, and data can be pushed or removed from either ends.    LRANGE command in redis can be used to view the data in a list, while LLEN can be used to get the length of the list.

src/redis-cli LLEN mylistkey
src/redis-cli LRANGE mylistkey -100 100

     


Set
          Set in redis consists of unique elements. The values in set can be viewed using the SMEMBERS command. 

src/redis-cli SMEMBERS mysetkey




Zset (Sorted Set)
          Sorted Set in redis contains the string data along with a score. Finding out any entry for a particular score takes log N time where N is the number of entries stored in a key. ZRANGEBYSCORE command can be used to view the data in a sorted set. Sorted sets are normally used for storing data involving some leaderboard where each user has a particular score. Another use case where sorted sets can be useful is in reporting where the data can be stored as value and date in YYMMDD format can be stored as a score. To get the data between two dates, ZRANGEBYSCORE query can be used.

src/redis-cli ZRANGEBYSCORE mysortedsetkey -INF +INF WITHSCORES
src/redis-cli ZRANGEBYSCORE mysortedsetkey 90 100 WITHSCORES



HLL
         HyperLogLog(HLL) in redis is used to calculate the estimate the number of unique values in a key without storing the actual values!!! Sounds too good to be true, go through this link to understand more. HLLs are stored as strings, so you can do the normal GET operation on it, but to approximately calculate the number of unique keys in a key, the command which is used is PFCOUNT

src/redis-cli PFCOUNT myhllkey


So what happens when we try to try to get a key using a separate command that what is expected for that type, say when we try to GET a hash key instead of HGETALL?
We get the error, "WRONGTYPE Operation against a key holding the wrong kind of value" which is expected since we should be using HGETALL command to get the data from a hash instead of GET.

:)

No comments:

Post a Comment