在Hazelcast中存储和过滤键值列表 [英] Store and filter on list of values for a key in Hazelcast

查看:76
本文介绍了在Hazelcast中存储和过滤键值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库调用返回一个键的值列表,我需要在弹簧中使用Hazelcast作为缓存来存储这些值.

My database call returns a list of values for a key and I need to use Hazelcast as a cache in spring to store these values.

我能够将其存储为IMap中的(键,列表),但是这样做无法使用诸如Hazelcast的谓词之类的东西在这些项上应用过滤器.我还希望它支持分页显示值列表.

I am able to store it as (key, List) in IMap but by doing so I am unable to apply filters on these items for a key using something like Predicates for Hazelcast. I would also like it to support pagination for the list of values.

已经听说过有关MultiMaps的内容,但无法在springboot中进行配置,也不确定是否符合上述用例.

Already heard something about MultiMaps but unable to configure the same in springboot and also unsure whether it siffices the above use case.

在此方面的任何帮助都将受到高度赞赏.

Any help on this is highly appreciated.

示例: {userid1:[{accNum :, accType:},{}], userid2:[{accNum:,accType:},{}],....}

Example: {userid1: [{accNum: , accType: }, { }], userid2: [{accNum: , accType: }, { }],....}

现在,我想通过使用谓词对userid1的accType进行过滤.

Now I want to filter on accType for userid1 by using predicates if possible.

推荐答案

这对您有帮助吗?

它显示了如何查询JSON对象

It shows how you can query JSON objects

import java.util.Collection;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.HazelcastJsonValue;
import com.hazelcast.core.IMap;
import com.hazelcast.query.Predicate;
import com.hazelcast.query.SqlPredicate;

public class Application {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {
        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();

        IMap<Integer, HazelcastJsonValue> someMap = hazelcastInstance.getMap("name goes here");

        String json1 = "{\"user\": 1, \"acc\": [{\"accNum\": 1, \"accType\": \"A\"}, {\"accNum\": 3, \"accType\": \"C\"}]";
        String json2 = "{\"user\": 2, \"acc\": [{\"accNum\": 2, \"accType\": \"B\"}, ]";

        HazelcastJsonValue value1 = new HazelcastJsonValue(json1);
        HazelcastJsonValue value2 = new HazelcastJsonValue(json2);

        someMap.put(1, value1);
        someMap.put(2, value2);

        String[] queries = {
                "user > 1",
                "acc[any].accType = 'C'",
                "acc[1].accType = 'C'",
                "acc[any].accType != 'C'",
                "acc[0].accType != 'C'",
        };

        for (String query : queries) {
            Predicate<Integer, HazelcastJsonValue> predicate = new SqlPredicate(query);
            System.out.printf("%n%s%n", query);
            Collection<HazelcastJsonValue> results = someMap.values(predicate);
            results.forEach(System.out::println);
            System.out.printf("[%d row%s]%n", results.size(), results.size()==1 ? "": "s");
        }

        hazelcastInstance.shutdown();
    }

这篇关于在Hazelcast中存储和过滤键值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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