Hazelcast的IMap聚合使用时出现错误。

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

An error when trying to use Hazelcast's IMap aggregation

问题

我正在尝试聚合IMap的值,但我收到以下错误:

com.hazelcast.map.impl.query.QueryPartitionOperation
SEVERE: [192.168.99.1]:5701 [dev] [3.12.3] java.lang.IllegalArgumentException: 在类'java.lang.String'上没有适合的访问器'john'

这是一个重现器:

HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance();
IMap<String, String> map1 = hazelCast.getMap("map1");

map1.put("1", "john");
map1.put("2", "charlie");
map1.put("3", "john");
map1.put("4", "john");

Long count = map1.aggregate(Aggregators.count("john"));

System.out.println(count);
英文:

I'm trying to aggregate IMap's values but I'm getting the below error:

com.hazelcast.map.impl.query.QueryPartitionOperation
SEVERE: [192.168.99.1]:5701 [dev] [3.12.3] java.lang.IllegalArgumentException: There is no suitable accessor for &#39;john&#39; on class &#39;java.lang.String&#39;

Here is a reproducer:


		HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance();
		IMap&lt;String, String&gt; map1 = hazelCast.getMap(&quot;map1&quot;);

		map1.put(&quot;1&quot;, &quot;john&quot;);
		map1.put(&quot;2&quot;, &quot;charlie&quot;);
		map1.put(&quot;3&quot;, &quot;john&quot;);
		map1.put(&quot;4&quot;, &quot;john&quot;);
		
	
		Long count = map1.aggregate(Aggregators.count(&quot;john&quot;));

        System.out.println(count);

答案1

得分: 2

你应该使用IMap的以下聚合函数:

/**
 * 对使用Predicate筛选的映射条目应用聚合逻辑并返回结果
 * <p>
 * Fast-Aggregations是Map-Reduce聚合器的继任者。
 * 在大多数用例中,它们与Map-Reduce聚合器等效,但不是在Map-Reduce引擎上运行,
 * 而是在查询基础设施上运行。由于它们对每个分区并行运行并针对速度和低内存消耗进行了高度优化,
 * 所以它们的性能要好几十倍甚至几百倍。
 *
 * @param aggregator 聚合条目的聚合器
 * @param predicate  用于筛选条目的谓词
 * @param &lt;R&gt;        结果的类型
 * @return 给定类型的结果
 * @since 3.8
 */
&lt;R&gt; R aggregate(Aggregator&lt;Map.Entry&lt;K, V&gt;, R&gt; aggregator, Predicate&lt;K, V&gt; predicate);

示例代码如下:

@Test
public void myTest() {
    HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance();
    IMap&lt;String, String&gt; map1 = hazelCast.getMap("map1");

    map1.put("1", "john");
    map1.put("2", "charlie");	
    map1.put("3", "john");
    map1.put("4", "john");

    Long count = map1.aggregate(Aggregators.count(), e -&gt; "john".equals(e.getValue()));

    System.out.println(count);
}
英文:

You should use the following aggregate function of IMap :

/**
 * Applies the aggregation logic on map entries filtered with the Predicated and returns the result
 * &lt;p&gt;
 * Fast-Aggregations are the successor of the Map-Reduce Aggregators.
 * They are equivalent to the Map-Reduce Aggregators in most of the use-cases, but instead of running on the Map-Reduce
 * engine they run on the Query infrastructure. Their performance is tens to hundreds times better due to the fact
 * that they run in parallel for each partition and are highly optimized for speed and low memory consumption.
 *
 * @param aggregator aggregator to aggregate the entries with
 * @param predicate  predicate to filter the entries with
 * @param &lt;R&gt;        type of the result
 * @return the result of the given type
 * @since 3.8
 */
&lt;R&gt; R aggregate(Aggregator&lt;Map.Entry&lt;K, V&gt;, R&gt; aggregator, Predicate&lt;K, V&gt; predicate);

like this :

@Test
public void myTest() {
    HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance();
    IMap&lt;String, String&gt; map1 = hazelCast.getMap(&quot;map1&quot;);

    map1.put(&quot;1&quot;, &quot;john&quot;);
    map1.put(&quot;2&quot;, &quot;charlie&quot;);	
    map1.put(&quot;3&quot;, &quot;john&quot;);
    map1.put(&quot;4&quot;, &quot;john&quot;);
    

    Long count = map1.aggregate(Aggregators.count(), e -&gt; &quot;john&quot;.equals(e.getValue()));

    System.out.println(count);
}

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

发表评论

匿名网友

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

确定