如何使用HazelcastJsonValue进行聚合?

huangapple go评论60阅读模式
英文:

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 -&gt; &quot;name=&#39;John&#39;&quot;.equals(e.getValue()));        
Long count = map1.aggregate(Aggregators.count(&quot;name=&#39;John&#39;&quot;));

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&lt;String, HazelcastJsonValue&gt; map1 = hazelCast.getMap(&quot;map1&quot;);

    map1.put(&quot;1&quot;, new HazelcastJsonValue(&quot;{&quot;name&quot;:&quot;John&quot;, &quot;age&quot;:31, &quot;city&quot;:&quot;New York&quot;}&quot;));
    map1.put(&quot;2&quot;,  new HazelcastJsonValue(&quot;{&quot;name&quot;:&quot;John&quot;, &quot;age&quot;:31, &quot;city&quot;:&quot;New York&quot;}&quot;));
    map1.put(&quot;3&quot;, new HazelcastJsonValue(&quot;{&quot;name&quot;:&quot;John&quot;, &quot;age&quot;:31, &quot;city&quot;:&quot;New York&quot;}&quot;));
    map1.put(&quot;4&quot;,  new HazelcastJsonValue(&quot;{&quot;name&quot;:&quot;John&quot;, &quot;age&quot;:31, &quot;city&quot;:&quot;New York&quot;}&quot;));
    
    Long count = map1.aggregate(Aggregators.count(), e -&gt; &quot;name=&#39;John&#39;&quot;.equals(e.getValue()));
    Long count = map1.aggregate(Aggregators.count(&quot;name=&#39;John&#39;&quot;));

    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&lt;String, HazelcastJsonValue&gt; map1 = hazelCast.getMap(&quot;map1&quot;);

HazelcastJsonValue jsv = new HazelcastJsonValue(&quot;{\&quot;name\&quot;:\&quot;John\&quot;, \&quot;age\&quot;:31, \&quot;city\&quot;:\&quot;New York\&quot;}&quot;);
map1.put(&quot;A&quot;, jsv);
map1.put(&quot;B&quot;, jsv);

Predicate p = Predicates.equal(&quot;name&quot;, &quot;John&quot;);
long count = map1.aggregate(Aggregators.count(), p);
System.out.println(&quot;Count: &quot; + count);
hazelCast.shutdown();

huangapple
  • 本文由 发表于 2020年8月11日 22:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63360205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定