英文:
How to use aggregations with HazelcastJsonValue?
问题
How to use aggregations with HazelcastJsonValue
?
我如何使用HazelcastJsonValue
进行聚合?
I'm trying to use:
我尝试使用:
Long count = map1.aggregate(Aggregators.count(), e -> "name='John'".equals(e.getValue()));
Long count = map1.aggregate(Aggregators.count("name='John'"));
But I'm getting 0 in both cases while the actual result should be 4.
但在这两种情况下,我都得到了0,而实际结果应该是4。
Here's a sample code:
这是一个示例代码:
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);
英文:
How to use aggregations with HazelcastJsonValue
?
I'm trying to use:
Long count = map1.aggregate(Aggregators.count(), e -> "name='John'".equals(e.getValue()));
Long count = map1.aggregate(Aggregators.count("name='John'"));
But I'm getting 0 in both cases while the actual result should be 4.
Here's a sample code:
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);
答案1
得分: 0
The predicate doesn't look well-formed to me -- in the first one, you're comparing the entire JSON document (multiple fields) using 'equals' against a portion of the doc, so it will never match.
What worked for me is this:
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();
(Note: This is a code snippet and should not be translated.)
英文:
The predicate doesn't look well-formed to me -- in the first one, you're comparing the entire JSON document (multiple fields) using 'equals' against a portion of the doc, so it will never match.
What worked for me is this:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论