Spring Redis Sort Keys

I have the following keys in Redis (Spring Data Redis),

localhost>Keys * "1+ { \"_id":"1", \"Name\" : \"C5796\" , \"Site\" : \"DRG1\"}" "2+ { \"_id":"2", \"Name\" : \"CX1XE\" , \"Site\" : \"DG1\"}" "3+ { \"_id":"3", \"Name\" : \"C553\" , \"Site\" : \"DG1\"}" 

If I want to sort by id / name / site , how can I do this in Spring Redis ?

 List<Object> keys = redistemplate.sort(SortQueryBuilder.sort("Customer").build()); 

and

 SortQuery<String> sort = SortQueryBuilder.sort(key).noSort().get(field).build(); List<?> keys = redistemplate.boundHashOps(key).getOperations().sort(sort); 

does not work.

+5
source share
2 answers

The code is in last place of the message, if you are familiar with the principle of multi-segment sort keys in redis, skip the following content and read the code directly.

Redis Sort is designed to sort the fields inside List / Set / Zset, but this method can be used to sort the database with several keys by the specified metric we want. We can use β€œsorting” to sort several hset keys by the specified field, but there is a restriction on the hset key pattern.
For example, if the hset key template is "hash {i}" (i is an integer), under this condition we can sort it.

 127.0.0.1:6379> keys hash* 1) "hash3" 2) "hash2" 3) "hash1" 

Take a look at the contents of hash1:

 127.0.0.1:6379> hgetall hash1 1) "id" 2) "24" 3) "name" 4) "kobe" 

Each hash key contains two fields: "id", "name". If we want to sort these hset keys by its identifier. What should we do?

First add the given key named "myset". "myset" is a set of keys containing the elements {"1", "2", "3"}.

 127.0.0.1:6379> smembers myset 1) "1" 2) "2" 3) "3" 

Then run the following command:

 127.0.0.1:6379> SORT myset BY hash*->id GET hash*->id GET hash*->name 1) "3" 2) "wade" 3) "24" 4) "kobe" 5) "30" 6) "curry" 

Eureka, sort the hash {1-3} by its identifier.
Here is the code for using Spring Redis to do the job:

 public static String getRandomStr() { return String.valueOf(new Random().nextInt(100)); } public static void redisTemplateSort(RedisTemplate redisTemplate) { String sortKey = "sortKey"; StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setHashValueSerializer(stringRedisSerializer); redisTemplate.delete(sortKey); if (!redisTemplate.hasKey(sortKey)) { for (int i = 0; i < 10; i++) { redisTemplate.boundSetOps(sortKey).add(String.valueOf(i)); String hashKey = "hash" + i, strId = String.valueOf(i), strName = getRandomStr(), strSite = getRandomStr(); redisTemplate.boundHashOps(hashKey).put("_id", strId); redisTemplate.boundHashOps(hashKey).put("Name", strName); redisTemplate.boundHashOps(hashKey).put("Site", strSite); System.out.printf("%s : {\"_id\": %s, \"Name\": %s, \"Site\", %s}\n", hashKey, strId, strName, strSite); } } SortQuery<String> sortQuery = SortQueryBuilder.sort(sortKey).by("hash*->Name") .get("hash*->_id").get("hash*->Name").get("hash*->Site").build(); List<String> sortRslt = redisTemplate.sort(sortQuery); for (int i = 0; i < sortRslt.size(); ) { System.out.printf("{\"_id\": %s, \"Name\": %s, \"Site\", %s}\n", sortRslt.get(i+2), sortRslt.get(i+1), sortRslt.get(i)); i += 3; } } 

The result of executing redisTemplateSort(redisTemplate) (how to sort by name in code):

 hash0 : {"_id": 0, "Name": 59, "Site", 60} hash1 : {"_id": 1, "Name": 37, "Site", 57} hash2 : {"_id": 2, "Name": 6, "Site", 40} hash3 : {"_id": 3, "Name": 91, "Site", 58} hash4 : {"_id": 4, "Name": 39, "Site", 32} hash5 : {"_id": 5, "Name": 27, "Site", 82} hash6 : {"_id": 6, "Name": 43, "Site", 10} hash7 : {"_id": 7, "Name": 17, "Site", 55} hash8 : {"_id": 8, "Name": 14, "Site", 91} hash9 : {"_id": 9, "Name": 39, "Site", 91} {"_id": 40, "Name": 6, "Site", 2} {"_id": 91, "Name": 14, "Site", 8} {"_id": 55, "Name": 17, "Site", 7} {"_id": 82, "Name": 27, "Site", 5} {"_id": 57, "Name": 37, "Site", 1} {"_id": 32, "Name": 39, "Site", 4} {"_id": 91, "Name": 39, "Site", 9} {"_id": 10, "Name": 43, "Site", 6} {"_id": 60, "Name": 59, "Site", 0} {"_id": 58, "Name": 91, "Site", 3} 
+2
source

I do not know about redis spring data. Let me give you a sample to achieve this in a naive Radish. Say you have a hash that has an id, name, and site. and I have a list representing the keys of this hash.

My structure will look like this:

 lpush("Values",1); hset("hash_1","id","1"),hset("hash_1","Name","C5796"),hset("hash_1","Site","DRG1") for second hash lpush("Values",2); ... 

Similarly for all the values ​​you want to set in the hash. Now for sorting you like it

 SORT "Values" BY hash_*->id get hash_*->id get hash_*->name get hash_*->site 

this will return you an ascending sorted hashmap result based on id. similarly you can do for names / site. For more information about sorting in redis: http://redis.io/commands/sort

+2
source

Source: https://habr.com/ru/post/1243882/


All Articles