Class WithKeys<K,V>
- Type Parameters:
K- the type of the keys in the outputPCollectionV- the type of the elements in the inputPCollectionand the values in the outputPCollection
- All Implemented Interfaces:
Serializable,HasDisplayData
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.
- See Also:
-
Field Summary
Fields inherited from class org.apache.beam.sdk.transforms.PTransform
annotations, displayData, name, resourceHints -
Method Summary
Modifier and TypeMethodDescriptionPCollection<KV<K, V>> expand(PCollection<V> in) Override this method to specify how thisPTransformshould be expanded on the givenInputT.static <K,V> WithKeys <K, V> Returns aPTransformthat takes aPCollection<V>and returns aPCollection<KV<K, V>>, where each of the values in the inputPCollectionhas been paired with the given key.static <K,V> WithKeys <K, V> of(SerializableFunction<V, K> fn) Returns aPTransformthat takes aPCollection<V>and returns aPCollection<KV<K, V>>, where each of the values in the inputPCollectionhas been paired with a key computed from the value by invoking the givenSerializableFunction.withKeyType(TypeDescriptor<K> keyType) Return aWithKeysthat is like this one with the specified key type descriptor.Methods inherited from class org.apache.beam.sdk.transforms.PTransform
addAnnotation, compose, compose, getAdditionalInputs, getAnnotations, getDefaultOutputCoder, getDefaultOutputCoder, getDefaultOutputCoder, getKindString, getName, getResourceHints, populateDisplayData, setDisplayData, setResourceHints, toString, validate, validate
-
Method Details
-
of
Returns aPTransformthat takes aPCollection<V>and returns aPCollection<KV<K, V>>, where each of the values in the inputPCollectionhas been paired with a key computed from the value by invoking the givenSerializableFunction.If using a lambda in Java 8,
withKeyType(TypeDescriptor)must be called on the resultPTransform. -
of
Returns aPTransformthat takes aPCollection<V>and returns aPCollection<KV<K, V>>, where each of the values in the inputPCollectionhas been paired with the given key. -
withKeyType
Return aWithKeysthat 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 outputPCollection. -
expand
Description copied from class:PTransformOverride this method to specify how thisPTransformshould be expanded on the givenInputT.NOTE: This method should not be called directly. Instead apply the
PTransformshould be applied to theInputTusing theapplymethod.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).
- Specified by:
expandin classPTransform<PCollection<V>,PCollection<KV<K, V>>>
-