问题
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