InputT - type of input valuesAccumT - type of mutable accumulator valuesOutputT - type of output valuespublic abstract static class Combine.AccumulatingCombineFn<InputT,AccumT extends Combine.AccumulatingCombineFn.Accumulator<InputT,AccumT,OutputT>,OutputT> extends Combine.CombineFn<InputT,AccumT,OutputT>
CombineFn that uses a subclass of
Combine.AccumulatingCombineFn.Accumulator as its accumulator
type. By defining the operations of the Accumulator
helper class, the operations of the enclosing CombineFn
are automatically provided. This can reduce the code required to
implement a CombineFn.
For example, the example from Combine.CombineFn above can be
expressed using AccumulatingCombineFn more concisely as
follows:
public class AverageFn
extends AccumulatingCombineFn<Integer, AverageFn.Accum, Double> {
public Accum createAccumulator() {
return new Accum();
}
public class Accum
extends AccumulatingCombineFn<Integer, AverageFn.Accum, Double>
.Accumulator {
private int sum = 0;
private int count = 0;
public void addInput(Integer input) {
sum += input;
count++;
}
public void mergeAccumulator(Accum other) {
sum += other.sum;
count += other.count;
}
public Double extractOutput() {
return ((double) sum) / count;
}
}
}
PCollection<Integer> pc = ...;
PCollection<Double> average = pc.apply(Combine.globally(new AverageFn()));
| Modifier and Type | Class and Description |
|---|---|
static interface |
Combine.AccumulatingCombineFn.Accumulator<InputT,AccumT,OutputT>
The type of mutable accumulator values used by this
AccumulatingCombineFn. |
| Constructor and Description |
|---|
AccumulatingCombineFn() |
| Modifier and Type | Method and Description |
|---|---|
AccumT |
addInput(AccumT accumulator,
InputT input)
Adds the given input value to the given accumulator, returning the
new accumulator value.
|
OutputT |
extractOutput(AccumT accumulator)
Returns the output value that is the result of combining all
the input values represented by the given accumulator.
|
AccumT |
mergeAccumulators(Iterable<AccumT> accumulators)
Returns an accumulator representing the accumulation of all the
input values accumulated in the merging accumulators.
|
apply, asKeyedFn, createAccumulator, getAccumulatorCoder, getDefaultOutputCoder, getOutputTypepublic final AccumT addInput(AccumT accumulator, InputT input)
Combine.CombineFnFor efficiency, the input accumulator may be modified and returned.
addInput in class Combine.CombineFn<InputT,AccumT extends Combine.AccumulatingCombineFn.Accumulator<InputT,AccumT,OutputT>,OutputT>public final AccumT mergeAccumulators(Iterable<AccumT> accumulators)
Combine.CombineFnMay modify any of the argument accumulators. May return a fresh accumulator, or may return one of the (modified) argument accumulators.
mergeAccumulators in class Combine.CombineFn<InputT,AccumT extends Combine.AccumulatingCombineFn.Accumulator<InputT,AccumT,OutputT>,OutputT>public final OutputT extractOutput(AccumT accumulator)
Combine.CombineFnextractOutput in class Combine.CombineFn<InputT,AccumT extends Combine.AccumulatingCombineFn.Accumulator<InputT,AccumT,OutputT>,OutputT>