如何将 HSCAN 响应映射到 Map使用 Vert.x Redis

发布于 2025-01-09 05:09:42 字数 428 浏览 5 评论 0原文

我正在使用 io.quarkus.redis.client.RedisClient ,其中使用 Vert.x 用于 Java 中的 Redis 客户端。当我使用 HSCAN 方法时,它会返回不同行中的键和值列表,如下所示:

在此处输入图像描述

键是 0,2,4...,值是 JSON。有没有办法获取Map而不是以优雅/干净的方式混合键和值的列表?

I am using io.quarkus.redis.client.RedisClient which inside use Vert.x for a Redis Client in Java. When I use HSCAN method it return a list of key and values in differents rows like this:

enter image description here

The keys are 0,2,4... and the values are the JSONs. Is there a way to obtain a Map<key,value> instead of a list with a key and values mix in a elegant/clean way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯叶蝶 2025-01-16 05:09:43

你可以用一个简单的 for 来做到这一点

        Map<String, String> result = new HashMap<>();

        for (int i = 0; i < source.length; i+=2) {
            result.put(source[i], source[i + 1]);
        }

在 Kotlin 中你可以使用一个更优雅的解决方案,但我认为这个可行

You can do it with a simple for

        Map<String, String> result = new HashMap<>();

        for (int i = 0; i < source.length; i+=2) {
            result.put(source[i], source[i + 1]);
        }

In Kotlin you can use a more elegant solution but I think this one works

心如狂蝶 2025-01-16 05:09:43

您将需要迭代响应才能获取地图。

这是 Map的示例使用 for 循环:

Map<String, String> map = new HashMap<>();
for (String key : results.getKeys()) {
    map.put(key, results.get(key).toString());
}

这是相同的示例,但使用 java lambdas:

Map<String, String> map = result.getKeys().stream()
    .collect(Collectors.toMap(key -> key, key -> result.get(key).toString()));

对于 json 的情况,您只需将转换函数从 .toString() 更改为适合您需要的东西。

编辑 1:

当 HSCAN 返回定义的数组时:

返回两个元素的批量回复,其中第一个元素是
表示无符号 64 位数字(光标)的字符串,以及
第二个元素是一个包含元素数组的多块元素。

创建地图没有一个简单的解决方案,但这是我推荐的:

Iterator<Response> iterator = response.get(1).stream().iterator();
Map<String, String> map = new HashMap<>();
    while (iterator.hasNext()) {
        map.put(iterator.next().toString(), iterator.next().toString());
    }

You will need to iterate over the Response in order to get the map.

Here is an example for Map<String, String> with for loop:

Map<String, String> map = new HashMap<>();
for (String key : results.getKeys()) {
    map.put(key, results.get(key).toString());
}

Here is the same example but using java lambdas:

Map<String, String> map = result.getKeys().stream()
    .collect(Collectors.toMap(key -> key, key -> result.get(key).toString()));

For your case with json you can just change the transformation function from .toString() to something that suits your need.

Edit 1:

As HSCAN returns array as defined:

return a two elements multi-bulk reply, where the first element is a
string representing an unsigned 64 bit number (the cursor), and the
second element is a multi-bulk with an array of elements.

There is not a simple solution to create a map but this is what I recommend:

Iterator<Response> iterator = response.get(1).stream().iterator();
Map<String, String> map = new HashMap<>();
    while (iterator.hasNext()) {
        map.put(iterator.next().toString(), iterator.next().toString());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文