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

前端 未结 1 894
耶瑟儿~
耶瑟儿~ 2021-01-27 07:42

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

相关标签:
1条回答
  • 2021-01-27 08:16

    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();
        }
    
    0 讨论(0)
提交回复
热议问题