K
- the type of the keys in the output PCollection
V
- the type of the elements in the input PCollection
and the values in the output
PCollection
public class WithKeys<K,V> extends PTransform<PCollection<V>,PCollection<KV<K,V>>>
WithKeys<K, V>
takes a PCollection<V>
, and either a constant key of type K
or a function from V
to K
, and returns a PCollection<KV<K, V>>
, where
each of the values in the input PCollection
has been paired with either the constant key
or a key computed from the value.
Example of use:
PCollection<String> words = ...;
PCollection<KV<Integer, String>> lengthsToWords =
words.apply(WithKeys.of(new SerializableFunction<String, Integer>() {
public Integer apply(String s) { return s.length(); } }));
Each output element has the same timestamp and is in the same windows as its corresponding
input element, and the output PCollection
has the same WindowFn
associated with it as the input.
name
Modifier and Type | Method and Description |
---|---|
PCollection<KV<K,V>> |
expand(PCollection<V> in)
Override this method to specify how this
PTransform should be expanded on the given
InputT . |
static <K,V> WithKeys<K,V> |
of(K key)
Returns a
PTransform that takes a PCollection<V> and returns a PCollection<KV<K, V>> , where each of the values in the input PCollection has been
paired with the given key. |
static <K,V> WithKeys<K,V> |
of(SerializableFunction<V,K> fn)
Returns a
PTransform that takes a PCollection<V> and returns a PCollection<KV<K, V>> , where each of the values in the input PCollection has been
paired with a key computed from the value by invoking the given SerializableFunction . |
WithKeys<K,V> |
withKeyType(TypeDescriptor<K> keyType)
Return a
WithKeys that is like this one with the specified key type descriptor. |
compose, getAdditionalInputs, getDefaultOutputCoder, getDefaultOutputCoder, getDefaultOutputCoder, getKindString, getName, populateDisplayData, toString, validate
public static <K,V> WithKeys<K,V> of(SerializableFunction<V,K> fn)
PTransform
that takes a PCollection<V>
and returns a PCollection<KV<K, V>>
, where each of the values in the input PCollection
has been
paired with a key computed from the value by invoking the given SerializableFunction
.
If using a lambda in Java 8, withKeyType(TypeDescriptor)
must be called on the
result PTransform
.
public static <K,V> WithKeys<K,V> of(@Nullable K key)
PTransform
that takes a PCollection<V>
and returns a PCollection<KV<K, V>>
, where each of the values in the input PCollection
has been
paired with the given key.public WithKeys<K,V> withKeyType(TypeDescriptor<K> keyType)
WithKeys
that is like this one with the specified key type descriptor.
For use with lambdas in Java 8, either this method must be called with an appropriate type
descriptor or PCollection.setCoder(Coder)
must be called on the output PCollection
.
public PCollection<KV<K,V>> expand(PCollection<V> in)
PTransform
PTransform
should be expanded on the given
InputT
.
NOTE: This method should not be called directly. Instead apply the PTransform
should
be applied to the InputT
using the apply
method.
Composite transforms, which are defined in terms of other transforms, should return the output of one of the composed transforms. Non-composite transforms, which do not apply any transforms internally, should return a new unbound output and register evaluators (via backend-specific registration methods).
expand
in class PTransform<PCollection<V>,PCollection<KV<K,V>>>