Class MapValues<K,V1,V2>
- Type Parameters:
V1- the type of the values in the inputPCollectionV2- the type of the elements in the outputPCollection
- All Implemented Interfaces:
Serializable,HasDisplayData
MapValues maps a SerializableFunction<V1,V2> over values of a
PCollection<KV<K,V1>> and returns a PCollection<KV<K, V2>>.
Example of use:
PCollection<KV<String, Integer>> input = ...;
PCollection<KV<String, Double> output =
input.apply(MapValues.into(TypeDescriptors.doubles()).via(Integer::doubleValue));
See also MapKeys.
- See Also:
-
Field Summary
Fields inherited from class org.apache.beam.sdk.transforms.PTransform
annotations, displayData, name, resourceHints -
Method Summary
Modifier and TypeMethodDescriptionexceptionsInto(TypeDescriptor<FailureT> failureTypeDescriptor) Returns a newSimpleMapWithFailurestransform that catches exceptions raised while mapping elements, with the given type descriptor used for the failure collection but the exception handler yet to be specified usingSimpleMapWithFailures.exceptionsVia(ProcessFunction).exceptionsVia(InferableFunction<WithFailures.ExceptionElement<KV<K, V1>>, FailureT> exceptionHandler) Returns a newSimpleMapWithFailurestransform that catches exceptions raised while mapping elements, passing the raised exception instance and the input element being processed through the givenexceptionHandlerand emitting the result to a failure collection.PCollection<KV<K, V2>> expand(PCollection<KV<K, V1>> input) Override this method to specify how thisPTransformshould be expanded on the givenInputT.static <V2> MapValues<?, ?, V2> into(TypeDescriptor<V2> outputType) Returns a newMapValuestransform with the given type descriptor for the output type, but the mapping function yet to be specified usingvia(SerializableFunction).via(SerializableFunction<NewValueT, V2> fn) 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
-
via
public <NewKeyT,NewValueT> MapValues<NewKeyT,NewValueT, viaV2> (SerializableFunction<NewValueT, V2> fn) - Type Parameters:
NewKeyT- the type of the keys in the input and outputPCollectionsNewValueT- the type of the values in the inputPCollection
-
into
Returns a newMapValuestransform with the given type descriptor for the output type, but the mapping function yet to be specified usingvia(SerializableFunction). -
exceptionsInto
@RequiresNonNull("fn") public <FailureT> org.apache.beam.sdk.transforms.SimpleMapWithFailures<KV<K,V1>, exceptionsIntoKV<K, V2>, FailureT> (TypeDescriptor<FailureT> failureTypeDescriptor) Returns a newSimpleMapWithFailurestransform that catches exceptions raised while mapping elements, with the given type descriptor used for the failure collection but the exception handler yet to be specified usingSimpleMapWithFailures.exceptionsVia(ProcessFunction).See
WithFailuresdocumentation for usage patterns of the returnedWithFailures.Result.Example usage:
Result<PCollection<KV<String, Integer>>, String> result = input.apply( MapValues.into(TypeDescriptors.integers()) .<String, String>via(word -> 1 / word.length) // Could throw ArithmeticException .exceptionsInto(TypeDescriptors.strings()) .exceptionsVia(ee -> ee.exception().getMessage())); PCollection<KV<String, Integer>> output = result.output(); PCollection<String> failures = result.failures(); -
exceptionsVia
@RequiresNonNull("fn") public <FailureT> org.apache.beam.sdk.transforms.SimpleMapWithFailures<KV<K,V1>, exceptionsViaKV<K, V2>, FailureT> (InferableFunction<WithFailures.ExceptionElement<KV<K, V1>>, FailureT> exceptionHandler) Returns a newSimpleMapWithFailurestransform that catches exceptions raised while mapping elements, passing the raised exception instance and the input element being processed through the givenexceptionHandlerand emitting the result to a failure collection.This method takes advantage of the type information provided by
InferableFunction, meaning that a call toexceptionsInto(TypeDescriptor)may not be necessary.See
WithFailuresdocumentation for usage patterns of the returnedWithFailures.Result.Example usage:
Result<PCollection<KV<String, Integer>>, String> result = input.apply( MapValues.into(TypeDescriptors.integers()) .<String, String>via(word -> 1 / word.length) // Could throw ArithmeticException .exceptionsVia( new InferableFunction<ExceptionElement<KV<String, String>>, String>() { @Override public String apply(ExceptionElement<KV<String, String>> input) { return input.exception().getMessage(); } })); PCollection<KV<String, Integer>> output = result.output(); PCollection<String> failures = result.failures(); -
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<KV<K,V1>>, PCollection<KV<K, V2>>>
-