Package 

Class MapProcessContextFn

  • All Implemented Interfaces:
    java.io.Serializable , org.apache.beam.sdk.transforms.display.HasDisplayData

    
    public final class MapProcessContextFn<InputT extends Object, OutputT extends Object>
    extends BaseElementFn<InputT, OutputT>
                        

    This class allows to handle a generic and custom org.apache.beam.sdk.transforms.DoFn for map operation with error handling.

    <br></br>

    This class is based on an input class and output type descriptor and take a org.apache.beam.sdk.transforms.SerializableFunction to execute the mapping treatment lazily. These types allow to give type information and handle default coders. This function is from org.apache.beam.sdk.transforms.DoFn.ProcessContext object to the output type. In some case, developers need to access to ProcessContext, to get technical data (timestamp...) or handle side inputs. This function is mandatory and executed in the ProcessElement stage of Beam lifecycle.

    <br></br>

    This class can take actions SerializableAction, used in the DoFn Beam lifecycle.

    • withSetupAction : executed in the setup method

    • withStartBundleAction : executed in the start bundle method

    • withFinishBundleAction : executed in the finish bundle method

    • withTeardownAction : executed in the teardown method

    These functions are not required and if they are given, they are executed lazily in the dedicated method.

    <br></br>

    Example usage:

    // With serializable function but without lifecycle actions.
    MapProcessContextFn.from(String.class)
       .into(TypeDescriptors.integers())
       .via((ProcessContext ctx) -> 1 / ctx.element().length)  // Could throw ArithmeticException
    
    // With serializable function and some lifecycle actions.
    MapProcessContextFn.from(String.class)
       .into(TypeDescriptors.integers())
       .via((String word) -> 1 / word.length)
       .withSetupAction(() -> System.out.println("Starting of mapping...")
       .withStartBundleAction(() -> System.out.println("Starting bundle of mapping...")
       .withFinishBundleAction(() -> System.out.println("Ending bundle of mapping...")
       .withTeardownAction(() -> System.out.println("Ending of mapping...")