K - type of keysVI - type of input valuesVA - type of mutable accumulator valuesVO - type of output valuespublic abstract static class Combine.KeyedCombineFn<K,VI,VA,VO>
extends java.lang.Object
implements java.io.Serializable
KeyedCombineFn<K, VI, VA, VO> specifies how to combine
a collection of input values of type VI, associated with
a key of type K, into a single output value of type
VO. It does this via one or more intermediate mutable
accumulator values of type VA.
The overall process to combine a collection of input
VI values associated with an input K key into a
single output VO value is as follows:
VI values are partitioned into one or more
batches.
createAccumulator(K) operation is
invoked to create a fresh mutable accumulator value of type
VA, initialized to represent the combination of zero
values.
VI value in a batch, the
addInput(K, VA, VI) operation is invoked to add the value to that
batch's accumulator VA value. The accumulator may just
record the new value (e.g., if VA == List<VI>, or may do
work to represent the combination more compactly.
mergeAccumulators(K, java.lang.Iterable<VA>) operation is invoked to
combine a collection of accumulator VA values into a
single combined output accumulator VA value, once the
merging accumulators have had all all the input values in their
batches added to them. This operation is invoked repeatedly,
until there is only one accumulator value left.
extractOutput(K, VA) operation is invoked on the final
accumulator VA value to get the output VO value.
K key that the
values being combined are associated with.
For example:
public class ConcatFn
extends KeyedCombineFn<String, Integer, ConcatFn.Accum, String> {
public static class Accum {
String s = "";
}
public Accum createAccumulator(String key) { return new Accum(); }
public void addInput(String key, Accum accum, Integer input) {
accum.s += "+" + input;
}
public Accum mergeAccumulators(String key, Iterable<Accum> accums) {
Accum merged = new Accum();
for (Accum accum : accums) {
merged.s += accum.s;
}
return merged;
}
public String extractOutput(String key, Accum accum) {
return key + accum.s;
}
}
PCollection<KV<String, Integer>> pc = ...;
PCollection<KV<String, String>> pc2 = pc.apply(
Combine.perKey(new ConcatFn()));
Keyed combining functions used by Combine.PerKey,
Combine.GroupedValues, and PTransforms derived
from them should be associative and commutative.
Associativity is required because input values are first broken
up into subgroups before being combined, and their intermediate
results further combined, in an arbitrary tree structure.
Commutativity is required because any order of the input values
is ignored when breaking up input values into groups.
| Constructor and Description |
|---|
Combine.KeyedCombineFn() |
| Modifier and Type | Method and Description |
|---|---|
abstract VA |
addInput(K key,
VA accumulator,
VI value)
Adds the given input value to the given accumulator,
modifying the accumulator.
|
VO |
apply(K key,
java.lang.Iterable<? extends VI> inputs)
Applies this
KeyedCombineFn to a key and a collection
of input values to produce a combined output value. |
abstract VA |
createAccumulator(K key)
Returns a new, mutable accumulator value representing the
accumulation of zero input values.
|
abstract VO |
extractOutput(K key,
VA accumulator)
Returns the output value that is the result of combining all
the input values represented by the given accumulator.
|
Coder<VA> |
getAccumulatorCoder(CoderRegistry registry,
Coder<K> keyCoder,
Coder<VI> inputCoder)
Returns the
Coder to use for accumulator VA
values, or null if it is not able to be inferred. |
Coder<VO> |
getDefaultOutputCoder(CoderRegistry registry,
Coder<K> keyCoder,
Coder<VI> inputCoder)
Returns the
Coder to use by default for output
VO values, or null if it is not able to be inferred. |
abstract VA |
mergeAccumulators(K key,
java.lang.Iterable<VA> accumulators)
Returns an accumulator representing the accumulation of all the
input values accumulated in the merging accumulators.
|
public abstract VA createAccumulator(K key)
key - the key that all the accumulated values using the
accumulator are associated withpublic abstract VA addInput(K key, VA accumulator, VI value)
For efficiency, the input accumulator may be modified and returned.
key - the key that all the accumulated values using the
accumulator are associated withpublic abstract VA mergeAccumulators(K key, java.lang.Iterable<VA> accumulators)
May modify any of the argument accumulators. May return a fresh accumulator, or may return one of the (modified) argument accumulators.
key - the key that all the accumulators are associated
withpublic abstract VO extractOutput(K key, VA accumulator)
key - the key that all the accumulated values using the
accumulator are associated withpublic VO apply(K key, java.lang.Iterable<? extends VI> inputs)
KeyedCombineFn to a key and a collection
of input values to produce a combined output value.
Useful when testing the behavior of a KeyedCombineFn
separately from a Combine transform.
public Coder<VA> getAccumulatorCoder(CoderRegistry registry, Coder<K> keyCoder, Coder<VI> inputCoder)
Coder to use for accumulator VA
values, or null if it is not able to be inferred.
By default, uses the knowledge of the Coder being
used for K keys and input VI values and the
enclosing Pipeline's CoderRegistry to try to
infer the Coder for VA values.
This is the Coder used to send data through a communication-intensive shuffle step, so a compact and efficient representation may have significant performance benefits.
public Coder<VO> getDefaultOutputCoder(CoderRegistry registry, Coder<K> keyCoder, Coder<VI> inputCoder)
Coder to use by default for output
VO values, or null if it is not able to be inferred.
By default, uses the knowledge of the Coder being
used for K keys and input VI values and the
enclosing Pipeline's CoderRegistry to try to
infer the Coder for VO values.