如何在HazelcastJsonValue中使用聚合
我试着使用:
Long count = map1.aggregate(Aggregators.count(), e -> "name='John'".equals(e.getValue()));
Long count = map1.aggregate(Aggregators.count("name='John'"));但我在两种情况下都得到了0,而实际结果应该是4。
下面是一个示例代码:
HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance();
IMap<String, HazelcastJsonValue> map1 = hazelCast.getMap("map1");
map1.put("1", new HazelcastJsonValue("{"name":"John", "age":31, "city":"New York"}"));
map1.put("2", new HazelcastJsonValue("{"name":"John", "age":31, "city":"New York"}"));
map1.put("3", new HazelcastJsonValue("{"name":"John", "age":31, "city":"New York"}"));
map1.put("4", new HazelcastJsonValue("{"name":"John", "age":31, "city":"New York"}"));
Long count = map1.aggregate(Aggregators.count(), e -> "name='John'".equals(e.getValue()));
Long count = map1.aggregate(Aggregators.count("name='John'"));
System.out.println(count);发布于 2020-08-12 00:24:06
在我看来,谓词的格式不是很好--在第一个谓词中,您使用'equals‘将整个JSON文档(多个字段)与文档的一部分进行比较,因此它永远不会匹配。
对我来说起作用的是:
Config c = new Config();
HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance(c);
IMap<String, HazelcastJsonValue> map1 = hazelCast.getMap("map1");
HazelcastJsonValue jsv = new HazelcastJsonValue("{\"name\":\"John\", \"age\":31, \"city\":\"New York\"}");
map1.put("A", jsv);
map1.put("B", jsv);
Predicate p = Predicates.equal("name", "John");
long count = map1.aggregate(Aggregators.count(), p);
System.out.println("Count: " + count);
hazelCast.shutdown();https://stackoverflow.com/questions/63360205
复制相似问题