如何将聚合与 HazelcastJsonValue 一起使用?
How to use aggregations with HazelcastJsonValue?
如何使用 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);
谓词在我看来 well-formed 不一样——在第一个中,您要使用 '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();
如何使用 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);
谓词在我看来 well-formed 不一样——在第一个中,您要使用 '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();