我的数据库调用为一个键返回一个值的列表,我需要在spring中使用Hazelcast作为缓存来存储这些值。
我可以在IMap中以(key, List)的形式存储,但这样做我无法使用类似Hazelcast的Predicates这样的东西对key的这些项目进行过滤。我也希望它能支持值的列表的分页。
已经听说过一些关于MultiMaps的东西,但无法在springboot中配置相同的东西,也不确定它是否适合上述用例。
如果有任何帮助,我们将非常感激。
例子 {userid1: [{accNum:, accType:}, { }],userid2:[{accNum:, accType:}, { }],...}。
现在,我想尽可能使用谓词对userid1的accType进行过滤。
这有什么帮助吗?
它展示了如何查询JSON对象
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();
}