Class WithFailures

java.lang.Object
org.apache.beam.sdk.transforms.WithFailures

public class WithFailures extends Object
A collection of utilities for writing transforms that can handle exceptions raised during processing of elements.

Consuming transforms such as MapElements.MapWithFailures follow the general pattern of taking in a user-defined exception handler of type ProcessFunction<ExceptionElement<InputT>, FailureOutputT> where the input WithFailures.ExceptionElement contains an exception along with the input element that was being processed when the exception was raised. This handler is responsible for producing some output element that captures relevant details of the failure and can be encoded as part of a failure output PCollection. Transforms can then package together their output and failure collections in a WithFailures.Result that avoids users needing to interact with TupleTags and indexing into a PCollectionTuple.

Exception handlers can narrow their scope by rethrowing the passed WithFailures.ExceptionElement.exception() and catching only specific subclasses of Exception. Unhandled exceptions will generally bubble up to a top-level Pipeline.PipelineExecutionException that halts progress.

Users can take advantage of WithFailures.Result.failuresTo(List) for fluent chaining of transforms that handle exceptions:


 PCollection<Integer> input = ...
 List<PCollection<Map<String, String>> failureCollections = new ArrayList<>();
 input.apply(MapElements.via(...).exceptionsVia(...))
      .failuresTo(failureCollections)
      .apply(MapElements.via(...).exceptionsVia(...))
      .failuresTo(failureCollections);
 PCollection<Map<String, String>> failures = PCollectionList.of(failureCollections)
      .apply("FlattenFailureCollections", Flatten.pCollections());
 
  • Constructor Details

    • WithFailures

      public WithFailures()