Search in sources :

Example 1 with Collator

use of com.hazelcast.mapreduce.Collator in project hazelcast by hazelcast.

the class ClientMultiMapProxy method aggregate.

@Override
public <SuppliedValue, Result> Result aggregate(Supplier<K, V, SuppliedValue> supplier, Aggregation<K, SuppliedValue, Result> aggregation, JobTracker jobTracker) {
    try {
        Preconditions.isNotNull(jobTracker, "jobTracker");
        KeyValueSource<K, V> keyValueSource = KeyValueSource.fromMultiMap(this);
        Job<K, V> job = jobTracker.newJob(keyValueSource);
        Mapper mapper = aggregation.getMapper(supplier);
        CombinerFactory combinerFactory = aggregation.getCombinerFactory();
        ReducerFactory reducerFactory = aggregation.getReducerFactory();
        Collator collator = aggregation.getCollator();
        MappingJob mappingJob = job.mapper(mapper);
        ReducingSubmittableJob reducingJob;
        if (combinerFactory != null) {
            reducingJob = mappingJob.combiner(combinerFactory).reducer(reducerFactory);
        } else {
            reducingJob = mappingJob.reducer(reducerFactory);
        }
        ICompletableFuture<Result> future = reducingJob.submit(collator);
        return future.get();
    } catch (Exception e) {
        throw new HazelcastException(e);
    }
}
Also used : MappingJob(com.hazelcast.mapreduce.MappingJob) HazelcastException(com.hazelcast.core.HazelcastException) ReducerFactory(com.hazelcast.mapreduce.ReducerFactory) HazelcastException(com.hazelcast.core.HazelcastException) Collator(com.hazelcast.mapreduce.Collator) CombinerFactory(com.hazelcast.mapreduce.CombinerFactory) Mapper(com.hazelcast.mapreduce.Mapper) ReducingSubmittableJob(com.hazelcast.mapreduce.ReducingSubmittableJob)

Example 2 with Collator

use of com.hazelcast.mapreduce.Collator in project hazelcast by hazelcast.

the class BigDecimalAvgAggregation method getCollator.

@Override
public Collator<Map.Entry<Key, AvgTuple<Long, BigDecimal>>, BigDecimal> getCollator() {
    return new Collator<Map.Entry<Key, AvgTuple<Long, BigDecimal>>, BigDecimal>() {

        @Override
        public BigDecimal collate(Iterable<Map.Entry<Key, AvgTuple<Long, BigDecimal>>> values) {
            long count = 0;
            BigDecimal amount = BigDecimal.ZERO;
            for (Map.Entry<Key, AvgTuple<Long, BigDecimal>> entry : values) {
                AvgTuple<Long, BigDecimal> tuple = entry.getValue();
                count += tuple.getFirst();
                amount = amount.add(tuple.getSecond());
            }
            return amount.divide(BigDecimal.valueOf(count));
        }
    };
}
Also used : Map(java.util.Map) BigDecimal(java.math.BigDecimal) Collator(com.hazelcast.mapreduce.Collator)

Example 3 with Collator

use of com.hazelcast.mapreduce.Collator in project hazelcast by hazelcast.

the class BigIntegerAvgAggregation method getCollator.

@Override
public Collator<Map.Entry<Key, AvgTuple<Long, BigInteger>>, BigInteger> getCollator() {
    return new Collator<Map.Entry<Key, AvgTuple<Long, BigInteger>>, BigInteger>() {

        @Override
        public BigInteger collate(Iterable<Map.Entry<Key, AvgTuple<Long, BigInteger>>> values) {
            long count = 0;
            BigInteger amount = BigInteger.ZERO;
            for (Map.Entry<Key, AvgTuple<Long, BigInteger>> entry : values) {
                AvgTuple<Long, BigInteger> tuple = entry.getValue();
                count += tuple.getFirst();
                amount = amount.add(tuple.getSecond());
            }
            return amount.divide(BigInteger.valueOf(count));
        }
    };
}
Also used : BigInteger(java.math.BigInteger) Map(java.util.Map) Collator(com.hazelcast.mapreduce.Collator)

Example 4 with Collator

use of com.hazelcast.mapreduce.Collator in project hazelcast by hazelcast.

the class ObjectMultiMapProxy method aggregate.

@Override
public <SuppliedValue, Result> Result aggregate(Supplier<K, V, SuppliedValue> supplier, Aggregation<K, SuppliedValue, Result> aggregation, JobTracker jobTracker) {
    try {
        isNotNull(jobTracker, "jobTracker");
        KeyValueSource<K, V> keyValueSource = KeyValueSource.fromMultiMap(this);
        Job<K, V> job = jobTracker.newJob(keyValueSource);
        Mapper mapper = aggregation.getMapper(supplier);
        CombinerFactory combinerFactory = aggregation.getCombinerFactory();
        ReducerFactory reducerFactory = aggregation.getReducerFactory();
        Collator collator = aggregation.getCollator();
        MappingJob mappingJob = job.mapper(mapper);
        ReducingSubmittableJob reducingJob;
        if (combinerFactory != null) {
            reducingJob = mappingJob.combiner(combinerFactory).reducer(reducerFactory);
        } else {
            reducingJob = mappingJob.reducer(reducerFactory);
        }
        ICompletableFuture<Result> future = reducingJob.submit(collator);
        return future.get();
    } catch (Exception e) {
        throw new HazelcastException(e);
    }
}
Also used : MappingJob(com.hazelcast.mapreduce.MappingJob) HazelcastException(com.hazelcast.core.HazelcastException) ReducerFactory(com.hazelcast.mapreduce.ReducerFactory) HazelcastException(com.hazelcast.core.HazelcastException) Collator(com.hazelcast.mapreduce.Collator) CombinerFactory(com.hazelcast.mapreduce.CombinerFactory) Mapper(com.hazelcast.mapreduce.Mapper) ReducingSubmittableJob(com.hazelcast.mapreduce.ReducingSubmittableJob)

Example 5 with Collator

use of com.hazelcast.mapreduce.Collator in project hazelcast by hazelcast.

the class MapProxyImpl method aggregate.

@Override
public <SuppliedValue, Result> Result aggregate(Supplier<K, V, SuppliedValue> supplier, Aggregation<K, SuppliedValue, Result> aggregation, JobTracker jobTracker) {
    checkTrue(NATIVE != mapConfig.getInMemoryFormat(), "NATIVE storage format is not supported for MapReduce");
    try {
        isNotNull(jobTracker, "jobTracker");
        KeyValueSource<K, V> keyValueSource = KeyValueSource.fromMap(this);
        Job<K, V> job = jobTracker.newJob(keyValueSource);
        Mapper mapper = aggregation.getMapper(supplier);
        CombinerFactory combinerFactory = aggregation.getCombinerFactory();
        ReducerFactory reducerFactory = aggregation.getReducerFactory();
        Collator collator = aggregation.getCollator();
        MappingJob mappingJob = job.mapper(mapper);
        ReducingSubmittableJob reducingJob;
        if (combinerFactory == null) {
            reducingJob = mappingJob.reducer(reducerFactory);
        } else {
            reducingJob = mappingJob.combiner(combinerFactory).reducer(reducerFactory);
        }
        ICompletableFuture<Result> future = reducingJob.submit(collator);
        return future.get();
    } catch (Exception e) {
        // TODO: not what we want, because it can lead to wrapping of HazelcastException
        throw new HazelcastException(e);
    }
}
Also used : MappingJob(com.hazelcast.mapreduce.MappingJob) HazelcastException(com.hazelcast.core.HazelcastException) ReducerFactory(com.hazelcast.mapreduce.ReducerFactory) HazelcastException(com.hazelcast.core.HazelcastException) Collator(com.hazelcast.mapreduce.Collator) AggregationResult(com.hazelcast.map.impl.query.AggregationResult) QueryResult(com.hazelcast.map.impl.query.QueryResult) CombinerFactory(com.hazelcast.mapreduce.CombinerFactory) Mapper(com.hazelcast.mapreduce.Mapper) ReducingSubmittableJob(com.hazelcast.mapreduce.ReducingSubmittableJob)

Aggregations

Collator (com.hazelcast.mapreduce.Collator)10 Map (java.util.Map)6 HazelcastException (com.hazelcast.core.HazelcastException)4 CombinerFactory (com.hazelcast.mapreduce.CombinerFactory)4 Mapper (com.hazelcast.mapreduce.Mapper)4 MappingJob (com.hazelcast.mapreduce.MappingJob)4 ReducerFactory (com.hazelcast.mapreduce.ReducerFactory)4 ReducingSubmittableJob (com.hazelcast.mapreduce.ReducingSubmittableJob)4 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 AggregationResult (com.hazelcast.map.impl.query.AggregationResult)1 QueryResult (com.hazelcast.map.impl.query.QueryResult)1