com.nextbreakpoint.flink.dummies.TemperatureAggregator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.nextbreakpoint.flink.dummies Show documentation
Show all versions of com.nextbreakpoint.flink.dummies Show documentation
Collection of dummy jobs for Apache Flink
/*
* This file is part of Flink Dummies
* https://github.com/nextbreakpoint/flink-dummies
*/
package com.nextbreakpoint.flink.dummies;
import org.apache.flink.api.common.functions.AggregateFunction;
import java.math.BigDecimal;
public class TemperatureAggregator implements AggregateFunction {
@Override
public TemperatureAccumulator createAccumulator() {
return TemperatureAccumulator.builder()
.withMaximumTemperature(new BigDecimal(0))
.withTemperature(new BigDecimal(0))
.withCount(0)
.build();
}
@Override
public TemperatureAccumulator add(SampledTemperature element, TemperatureAccumulator accumulator) {
return accumulator.add(BigDecimal.valueOf(element.getValue(), 2));
}
@Override
public TemperatureAccumulator merge(TemperatureAccumulator accumulator, TemperatureAccumulator otherAccumulator) {
return accumulator.add(otherAccumulator);
}
@Override
public Temperature getResult(TemperatureAccumulator accumulator) {
return Temperature.builder()
.withAverage(accumulator.getAverageTemperature().unscaledValue().intValue())
.withMaximum(accumulator.getMaximumTemperature().unscaledValue().intValue())
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy