Spring Redis排序键 [英] Spring Redis sort keys

查看:521
本文介绍了Spring Redis排序键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Redis(Spring数据Redis)中具有以下键,

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\"}"

如果要根据 id/name/site/进行排序,如何在Spring Redis中进行?

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

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

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

不起作用.

推荐答案

代码位于文章的最后,如果您熟悉redis中多hset键排序的原理,请跳过以下内容并直接阅读代码.

Redis Sort 旨在对List/Set/Zset中的字段进行排序,但是可以使用此方法根据我们想要的指定指标对多键进行排序.我们可以使用"sort"按指定字段对多个hset键进行排序,但是关于hset键的模式存在限制.
例如,如果hset键的模式为"hash {i}"(i为整数),则在这种情况下我们可以对其进行排序.

Redis Sort is aimed to sort fields inside List/Set/Zset, but this method can be used to sort multi keys base on specified metric we want. We can use "sort" to sort multi hset keys by specified field, but there is limitation about the pattern of hset keys.
For example, if the pattern of hset keys 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"

看看hash1的内容:

Take a look at the content of hash1:

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

每个哈希键都包含两个字段:"id","name".如果我们想按其ID对这些hset键进行排序.我们该怎么办?

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

首先,添加一个名为"myset"的设置键. "myset"是一个设置键,其中包含成员{"1","2","3"}.

First, add a set key named "myset". "myset" is a set key which contains members {"1", "2", "3"}.

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

然后运行以下命令:

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,按其ID对哈希{1-3}进行排序.
以下是使用Spring Redis来完成工作的代码:

Eureka, sort hash{1-3} by its id.
Here is the code of 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;
    }
}

运行redisTemplateSort(redisTemplate)的结果(按代码中的名称排序):

Result of running redisTemplateSort(redisTemplate)(as sort by name in the 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}

这篇关于Spring Redis排序键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆