英文:
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 'john' on class 'java.lang.String'
Here is a reproducer:
		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);
答案1
得分: 2
你应该使用IMap的以下聚合函数:
/**
 * 对使用Predicate筛选的映射条目应用聚合逻辑并返回结果
 * <p>
 * Fast-Aggregations是Map-Reduce聚合器的继任者。
 * 在大多数用例中,它们与Map-Reduce聚合器等效,但不是在Map-Reduce引擎上运行,
 * 而是在查询基础设施上运行。由于它们对每个分区并行运行并针对速度和低内存消耗进行了高度优化,
 * 所以它们的性能要好几十倍甚至几百倍。
 *
 * @param aggregator 聚合条目的聚合器
 * @param predicate  用于筛选条目的谓词
 * @param <R>        结果的类型
 * @return 给定类型的结果
 * @since 3.8
 */
<R> R aggregate(Aggregator<Map.Entry<K, V>, R> aggregator, Predicate<K, V> predicate);
示例代码如下:
@Test
public void myTest() {
    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(), e -> "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
 * <p>
 * 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 <R>        type of the result
 * @return the result of the given type
 * @since 3.8
 */
<R> R aggregate(Aggregator<Map.Entry<K, V>, R> aggregator, Predicate<K, V> predicate);
like this :
@Test
public void myTest() {
    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(), e -> "john".equals(e.getValue()));
    System.out.println(count);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论