Store and filter on list of values for a key in Hazelcast

我与影子孤独终老i 提交于 2020-06-01 05:04:02

问题


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.

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.

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.

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

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


回答1:


Does this help any ?

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();
    }


来源:https://stackoverflow.com/questions/61181666/store-and-filter-on-list-of-values-for-a-key-in-hazelcast

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!