VI - type of input valuesVA - type of mutable accumulator valuesVO - type of output valuespublic abstract static class Combine.AccumulatingCombineFn<VI,VA extends Combine.AccumulatingCombineFn.Accumulator<VI,VA,VO>,VO> extends Combine.CombineFn<VI,VA,VO>
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<VI,VA,VO>
The type of mutable accumulator values used by this
AccumulatingCombineFn. |
| Constructor and Description |
|---|
Combine.AccumulatingCombineFn() |
| Modifier and Type | Method and Description |
|---|---|
VA |
addInput(VA accumulator,
VI input)
Adds the given input value to the given accumulator, returning the
new accumulator value.
|
VO |
extractOutput(VA accumulator)
Returns the output value that is the result of combining all
the input values represented by the given accumulator.
|
VA |
mergeAccumulators(java.lang.Iterable<VA> accumulators)
Returns an accumulator representing the accumulation of all the
input values accumulated in the merging accumulators.
|
apply, asKeyedFn, createAccumulator, getAccumulatorCoder, getDefaultOutputCoderpublic final VA addInput(VA accumulator, VI input)
Combine.CombineFnFor efficiency, the input accumulator may be modified and returned.
addInput in class Combine.CombineFn<VI,VA extends Combine.AccumulatingCombineFn.Accumulator<VI,VA,VO>,VO>public final VA mergeAccumulators(java.lang.Iterable<VA> 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<VI,VA extends Combine.AccumulatingCombineFn.Accumulator<VI,VA,VO>,VO>public final VO extractOutput(VA accumulator)
Combine.CombineFnextractOutput in class Combine.CombineFn<VI,VA extends Combine.AccumulatingCombineFn.Accumulator<VI,VA,VO>,VO>