Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HSCAN command in Redis?

Tags:

redis

hiredis

I want to use Redis's HSCAN command in my assignment but I have no idea how it works. Redis's official page (http://redis.io/commands/hscan) for this command gives me blank page.

I am getting continuous input data and saving them instantaneously in multiple hashes in Redis and I would like to iterate through all of them at later point of time.

I'm using following command to save my data

HMSET myhash ordertype "neworder" order_ts "1234" act_type "order_ack" ack_ts "1240"
HMSET myhash2 ordertype "neworder" order_ts "2234" act_type "order_ack" ack_ts "2240"

Can anyone give me some examples of how to use HSCAN?

In my case I would like to get following output
1) myhash
2) myhash2
3) myhash3
.
.
.
.

like image 517
user2418755 Avatar asked Nov 11 '13 15:11

user2418755


People also ask

What is Hscan Redis?

Redis Hash: HSCAN HSCAN array of elements contains two elements, a field, and a value, for every returned element of the Hash. Syntax: HSCAN key cursor [MATCH pattern] [COUNT count] Available since. 2.8. 0.

How do I count keys in Redis?

The first command you can use to get the total number of keys in a Redis database is the DBSIZE command. This simple command should return the total number of keys in a selected database as an integer value. The above example command shows that there are 203 keys in the database at index 10.

How do I scan using Redis?

The Redis SCAN command is used in order to incrementally iterate over a collection of elements. SCAN is a cursor based iterator. This means that at every call of the command, the server returns an updated cursor that the user needs to use as the cursor argument in the next call.

Does Redis scan block?

So yes, SCAN is blocking, but it's usually not a big deal, and is only blocking when Redis is actually processing the command.


1 Answers

Commands

Start a full hash scan with:

HSCAN myhash 0

Start a hash scan with fields matching a pattern with:

HSCAN myhash 0 MATCH order_*

Start a hash scan with fields matching a pattern and forcing the scan command to do more scanning with:

HSCAN myhash 0 MATCH order_* COUNT 1000

Note

Don't forget that MATCH can return little to no element for each iteration, as explained in the documentation:

It is important to note that the MATCH filter is applied after elements are retrieved from the collection, just before returning data to the client. This means that if the pattern matches very little elements inside the collection, SCAN will likely return no elements in most iterations.

And that's why you can use COUNT to force more scanning for each iteration.

[Update] As Didier Spezia specified, you'll need Redis 2.8+ to use the *SCAN commands.

like image 65
FGRibreau Avatar answered Sep 18 '22 14:09

FGRibreau