Class RequestResponseIO<RequestT,ResponseT>
- All Implemented Interfaces:
Serializable,HasDisplayData
PTransform for reading from and writing to Web APIs.
RequestResponseIO is recommended for interacting with external systems that offer RPCs
that execute relatively quickly and do not offer advance features to make RPC execution
efficient.
For systems that offer features for more efficient reading, for example, tracking progress of RPCs, support for splitting RPCs (deduct two or more RPCs which when combined return the same result), consider using the Apache Beam's `Splittable DoFn` interface instead.
Basic Usage
RequestResponseIO minimally requires implementing the Caller interface:
class MyCaller implements Caller<SomeRequest, SomeResponse> {
public SomeResponse call(SomeRequest request) throws UserCodeExecutionException {
// calls the API submitting SomeRequest payload and returning SomeResponse
}
}
Then provide RequestResponseIO's of(org.apache.beam.io.requestresponse.Caller<RequestT, ResponseT>, org.apache.beam.sdk.coders.Coder<ResponseT>) method your Caller
implementation.
Coder<SomeResponse> responseCoder = ...
PCollection<SomeRequest> requests = ...
Result result = requests.apply(RequestResponseIO.of(new MyCaller(), responseCoder));
result.getResponses().apply( ... );
result.getFailures().apply( ... );
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DurationThe defaultDurationto wait until completion of user code.static final Set<Class<? extends UserCodeExecutionException>> SetofUserCodeExecutionExceptions that warrant repeating.Fields inherited from class org.apache.beam.sdk.transforms.PTransform
annotations, displayData, name, resourceHints -
Method Summary
Modifier and TypeMethodDescriptionexpand(PCollection<RequestT> input) Override this method to specify how thisPTransformshould be expanded on the givenInputT.static <RequestT,ResponseT>
RequestResponseIO<RequestT, ResponseT> Instantiates aRequestResponseIOwith aCallerand aRequestResponseIOCoderwith a default package private implementation ofCallShouldBackoffbased on https://sre.google/sre-book/handling-overload.static <RequestT,ResponseT, CallerSetupTeardownT extends Caller<RequestT, ResponseT> & SetupTeardown>
RequestResponseIO<RequestT, ResponseT> ofCallerAndSetupTeardown(CallerSetupTeardownT implementsCallerAndSetupTeardown, Coder<ResponseT> responseTCoder) Instantiates aRequestResponseIOwith aRequestResponseIOCoder, a default package private implementation ofCallShouldBackoffbased on https://sre.google/sre-book/handling-overload, and an implementation of both theCallerandSetupTeardowninterfaces.withCache(Cache.Pair<RequestT, ResponseT> pair) ConfiguresRequestResponseIOfor reading and writingRequestResponseIOandRequestResponseIOpairs using a cache.Overrides the package private implementation ofCallShouldBackoff, based on https://sre.google/sre-book/handling-overload, that determines whether the underlylingDoFnshould holdRequestResponseIOs.Turns off repeat invocations (default is on) ofSetupTeardownandCaller, using theRepeater, in the setting ofREPEATABLE_ERROR_TYPES.withTimeout(Duration value) Overrides theDEFAULT_TIMEOUTexpected timeout of all user custom code.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
-
Field Details
-
DEFAULT_TIMEOUT
The defaultDurationto wait until completion of user code. AUserCodeTimeoutExceptionis thrown whenCaller.call(RequestT),SetupTeardown.setup(), orSetupTeardown.teardown()exceed this timeout. -
REPEATABLE_ERROR_TYPES
SetofUserCodeExecutionExceptions that warrant repeating. Not all errors should be repeat execution such as bad or unauthenticated requests. However, certain errors such as timeouts, remote system or quota exceed errors may not be related to the code but due to the remote system and thus warrant repeating.
-
-
Method Details
-
of
public static <RequestT,ResponseT> RequestResponseIO<RequestT,ResponseT> of(Caller<RequestT, ResponseT> caller, Coder<ResponseT> responseTCoder) Instantiates aRequestResponseIOwith aCallerand aRequestResponseIOCoderwith a default package private implementation ofCallShouldBackoffbased on https://sre.google/sre-book/handling-overload. Checks for theCaller'sSerializableUtils.ensureSerializable(T)serializable errors. -
ofCallerAndSetupTeardown
public static <RequestT,ResponseT, RequestResponseIO<RequestT,CallerSetupTeardownT extends Caller<RequestT, ResponseT> & SetupTeardown> ResponseT> ofCallerAndSetupTeardown(CallerSetupTeardownT implementsCallerAndSetupTeardown, Coder<ResponseT> responseTCoder) Instantiates aRequestResponseIOwith aRequestResponseIOCoder, a default package private implementation ofCallShouldBackoffbased on https://sre.google/sre-book/handling-overload, and an implementation of both theCallerandSetupTeardowninterfaces. ChecksSerializableUtils.ensureSerializable(T)serializable errors. -
withTimeout
Overrides theDEFAULT_TIMEOUTexpected timeout of all user custom code. If user custom code exceeds this timeout, then aUserCodeTimeoutExceptionis thrown. User custom code may throw this exception prior to the configured timeout value on their own. -
withoutRepeater
Turns off repeat invocations (default is on) ofSetupTeardownandCaller, using theRepeater, in the setting ofREPEATABLE_ERROR_TYPES. -
withCallShouldBackoff
public RequestResponseIO<RequestT,ResponseT> withCallShouldBackoff(CallShouldBackoff<ResponseT> value) Overrides the package private implementation ofCallShouldBackoff, based on https://sre.google/sre-book/handling-overload, that determines whether the underlylingDoFnshould holdRequestResponseIOs. -
withCache
ConfiguresRequestResponseIOfor reading and writingRequestResponseIOandRequestResponseIOpairs using a cache.RequestResponseIO, by default, does not interact with a cache.When reading, the transform
Flattens theRequestResponseIOPCollectionof successful pairs with that resulting from API calls ofRequestResponseIOs of unsuccessful pairs.When writing, the transform
Flattens theRequestResponseIOPCollectionof successful pairs with that resulting from API calls ofRequestResponseIOs. -
withMonitoringConfiguration
-
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<RequestT>,Result<ResponseT>>
-