Class AsJsons<InputT>

java.lang.Object
org.apache.beam.sdk.transforms.PTransform<PCollection<InputT>,PCollection<String>>
org.apache.beam.sdk.extensions.jackson.AsJsons<InputT>
All Implemented Interfaces:
Serializable, HasDisplayData

public class AsJsons<InputT> extends PTransform<PCollection<InputT>,PCollection<String>>
PTransform for serializing objects to JSON Strings. Transforms a PCollection<InputT> into a PCollection of JSON Strings representing objects in the original PCollection using Jackson.
See Also:
  • Method Details

    • of

      public static <InputT> AsJsons<InputT> of(Class<? extends InputT> inputClass)
      Creates a AsJsons PTransform that will transform a PCollection<InputT> into a PCollection of JSON Strings representing those objects using a Jackson ObjectMapper.
    • withMapper

      public AsJsons<InputT> withMapper(ObjectMapper mapper)
      Use custom Jackson ObjectMapper instead of the default one.
    • exceptionsInto

      public <NewFailureT> AsJsons<InputT>.AsJsonsWithFailures<NewFailureT> exceptionsInto(TypeDescriptor<NewFailureT> failureTypeDescriptor)
      Returns a new AsJsons.AsJsonsWithFailures transform that catches exceptions raised while writing JSON elements, with the given type descriptor used for the failure collection but the exception handler yet to be specified using AsJsons.AsJsonsWithFailures.exceptionsVia(ProcessFunction).

      See WithFailures documentation for usage patterns of the returned WithFailures.Result.

    • exceptionsVia

      public <FailureT> AsJsons<InputT>.AsJsonsWithFailures<FailureT> exceptionsVia(InferableFunction<WithFailures.ExceptionElement<InputT>,FailureT> exceptionHandler)
      Returns a new AsJsons.AsJsonsWithFailures transform that catches exceptions raised while writing JSON elements, passing the raised exception instance and the input element being processed through the given exceptionHandler and emitting the result to a failure collection.

      See WithFailures documentation for usage patterns of the returned WithFailures.Result.

      Example usage:

      
       WithFailures.Result<PCollection<String>, KV<MyPojo, Map<String, String>>> result =
           pojos.apply(
               AsJsons.of(MyPojo.class)
                   .exceptionsVia(new WithFailures.ExceptionAsMapHandler<MyPojo>() {}));
      
       PCollection<String> output = result.output(); // valid json elements
       PCollection<KV<MyPojo, Map<String, String>>> failures = result.failures();
       
    • exceptionsVia

      Returns a new AsJsons.AsJsonsWithFailures transform that catches exceptions raised while writing JSON elements, passing the raised exception instance and the input element being processed through the default exception handler AsJsons.DefaultExceptionAsMapHandler and emitting the result to a failure collection.

      See AsJsons.DefaultExceptionAsMapHandler for more details about default handler behavior.

      See WithFailures documentation for usage patterns of the returned WithFailures.Result.

      Example usage:

      
       WithFailures.Result<PCollection<String>, KV<MyPojo, Map<String, String>>> result =
           pojos.apply(
               AsJsons.of(MyPojo.class)
                   .exceptionsVia());
      
       PCollection<String> output = result.output(); // valid json elements
       PCollection<KV<MyPojo, Map<String, String>>> failures = result.failures();
       
    • expand

      public PCollection<String> expand(PCollection<InputT> input)
      Description copied from class: PTransform
      Override this method to specify how this 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).

      Specified by:
      expand in class PTransform<PCollection<InputT>,PCollection<String>>