Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying in redis

Recently I am learning redis and honestly very impressed and dying to use it. One of the things that keep bothering me is "how do I query redis". To be specific I am trying to resolve following

Say I have a millions of hashes stored as below

usage:1 = {created: 20100521, quantity:9, resource:1033, user:1842, ...}
usage:2 = {created: 20100812, quantity:3, resource:7233, user:1842, ...}
usage:3 = {created: 20100927, quantity:4, resource:1031, user:76, ...}

Please note that there are many keys in hashes I have shown only 4. Now that I want to find records in specific date range, by user, by resource or for a user in given period.

I suspect that there are redis specific patterns to retrieve such data. I am a python programmer. I did look at redisco(ohm port) which supports some quering but I am not sure if it does get all the data and then filters in python.

like image 538
Shekhar Avatar asked Sep 27 '10 07:09

Shekhar


People also ask

Can I query data in Redis?

Spark-Redis library allows you to use the DataFrame APIs to store and access Redis data. In other words, you can insert, update and query data using SQL commands, but the data is internally mapped to Redis data structures.

What is Redis query?

Redis is an open source, advanced key-value store and an apt solution for building highperformance, scalable web applications. Redis has three main peculiarities that sets it apart. Redis holds its database entirely in the memory, using the disk only for persistence.

How do I run a Redis query?

To start Redis client, open the terminal and type the command redis-cli. This will connect to your local server and now you can run any command. In the above example, we connect to Redis server running on the local machine and execute a command PING, that checks whether the server is running or not.

Does Redis use SQL?

Redis does not support any structure query language (SQL), instead each data structure has dedicated set of commands to allow execution of effective atomic operations.


2 Answers

For Redis, it's best to understand what sort of query patterns you want over your data before you decide how you're going to store it.

For example, if you want to do a date range query over a set of data, you can store that data as a sorted set where the keys are the data items you want to query over, and the score is a unix timestamp.

In your example above, I might store your example hash as:

 user_to_resource:i = user:j                   # key -> value forward map
 resources => (resource:i, created_timestamp)  # sorted set
 count_resource:i = quantity                   # key -> value quantity map

That is, I'd have many forward and reverse maps depending on the query patterns I'd like to support.

like image 52
rlotun Avatar answered Sep 28 '22 09:09

rlotun


the queries you mention are highly dependant on time. In this instance you would be wise to use a sorted set. You could use the datetime stamp as the score for each entry.

For example, you could do the following:

hmset usage:1 created 20100521 quantity 9 resource 1033 user 1842
hmset usage:2 created 20100812 quantity 3 resource 7233 user 1842
hmset usage:3 created 20100927 quantity 4 resource 1031 user 76

zadd usage 20200521 1
zadd usage 20100812 2
zadd usage 20100927 3

To retrieve everything:

sort usage get 
# get usage:*->created get usage:*->quantity get usage:*->resource get usage:*->user

or

lrange usage 0 -1

To get the indexes of a range:

zrangebyscore usage 20100800 20100900

For queries based on a hash key value, there is a useful addition to redis which allows the use of scripts written in lua. You could easily write a simple lua script within a python heredoc and use the redis.eval method to pass the script to redis. The script could be a loop which filters based on the value you are looking for.

like image 42
Lloyd Moore Avatar answered Sep 28 '22 10:09

Lloyd Moore