manu
manu

Reputation: 169

An error when trying to use Hazelcast's IMap aggregation

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);

Upvotes: 1

Views: 188

Answers (1)

st6ph
st6ph

Reputation: 86

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);
}

Upvotes: 2

Related Questions