Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting Keys From Redis

Tags:

java

redis

jedis

I use this following code to extract all of the Keys start with "NAME:" and it return only over 5,000 records (There is over 60,000 keys in my index). Can anyone explain why it is happening or how can I extract all of the keys from Redis Database.

jedis.select(3);

    Set<String> names=jedis.keys("NAME:*");

    Iterator<String> it = names.iterator();
    while (it.hasNext()) {
        String s = it.next();
        System.out.println(s);
    }
like image 333
BonBoon Avatar asked Oct 30 '12 08:10

BonBoon


People also ask

How do I get Redis keys?

Redis GET all Keys To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

How fetch data from Redis?

During fetching the records from database,first check whether the key present(primary key i.e id) in redis,if "yes" get data directly from redis,otherwise hit the database for data. where $table is 'posts' or 'comments' etc, and $ids are searched keys. or using lua scripts.

How do I see what is stored in Redis?

You can simply connect to your redis server using redis-cli, select your database and type KEYS *, please remember it will give you all the keys present in selected redis database.


1 Answers

When Redis server store many record, using jedis.keys() command can make it over-processing. Thus, result in it stop when the task is un-done. Instead of it, you use jedis.hscan() so as to avoid above problem.

ScanParams scanParam = new ScanParams();
String cursor = "0";
int i = 0;
scanParam.match(prefix + "*");
scanParam.count(50000); // 2000 or 30000 depend on your
do {
ScanResult<Map.Entry<String, String>> hscan = jedis.hscan(key, cursor, scanParam);

// any code here
//
//

// check cursor 
cursor = hscan.getStringCursor();
} while("0".equals(cursor));

It work well in your case.

like image 98
VanThaoNguyen Avatar answered Oct 01 '22 06:10

VanThaoNguyen