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 Duration
The defaultDuration
to wait until completion of user code.static final Set
<Class<? extends UserCodeExecutionException>> Set
ofUserCodeExecutionException
s 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 thisPTransform
should be expanded on the givenInputT
.static <RequestT,
ResponseT>
RequestResponseIO<RequestT, ResponseT> Instantiates aRequestResponseIO
with aCaller
and aRequestResponseIO
Coder
with a default package private implementation ofCallShouldBackoff
based 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 aRequestResponseIO
with aRequestResponseIO
Coder
, a default package private implementation ofCallShouldBackoff
based on https://sre.google/sre-book/handling-overload, and an implementation of both theCaller
andSetupTeardown
interfaces.withCache
(Cache.Pair<RequestT, ResponseT> pair) ConfiguresRequestResponseIO
for reading and writingRequestResponseIO
andRequestResponseIO
pairs using a cache.Overrides the package private implementation ofCallShouldBackoff
, based on https://sre.google/sre-book/handling-overload, that determines whether the underlylingDoFn
should holdRequestResponseIO
s.Turns off repeat invocations (default is on) ofSetupTeardown
andCaller
, using theRepeater
, in the setting ofREPEATABLE_ERROR_TYPES
.withTimeout
(Duration value) Overrides theDEFAULT_TIMEOUT
expected 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 defaultDuration
to wait until completion of user code. AUserCodeTimeoutException
is thrown whenCaller.call(RequestT)
,SetupTeardown.setup()
, orSetupTeardown.teardown()
exceed this timeout. -
REPEATABLE_ERROR_TYPES
Set
ofUserCodeExecutionException
s 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 aRequestResponseIO
with aCaller
and aRequestResponseIO
Coder
with a default package private implementation ofCallShouldBackoff
based 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 aRequestResponseIO
with aRequestResponseIO
Coder
, a default package private implementation ofCallShouldBackoff
based on https://sre.google/sre-book/handling-overload, and an implementation of both theCaller
andSetupTeardown
interfaces. ChecksSerializableUtils.ensureSerializable(T)
serializable errors. -
withTimeout
Overrides theDEFAULT_TIMEOUT
expected timeout of all user custom code. If user custom code exceeds this timeout, then aUserCodeTimeoutException
is 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) ofSetupTeardown
andCaller
, 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 underlylingDoFn
should holdRequestResponseIO
s. -
withCache
ConfiguresRequestResponseIO
for reading and writingRequestResponseIO
andRequestResponseIO
pairs using a cache.RequestResponseIO
, by default, does not interact with a cache.When reading, the transform
Flatten
s theRequestResponseIO
PCollection
of successful pairs with that resulting from API calls ofRequestResponseIO
s of unsuccessful pairs.When writing, the transform
Flatten
s theRequestResponseIO
PCollection
of successful pairs with that resulting from API calls ofRequestResponseIO
s. -
withMonitoringConfiguration
-
expand
Description copied from class:PTransform
Override this method to specify how thisPTransform
should be expanded on the givenInputT
.NOTE: This method should not be called directly. Instead apply the
PTransform
should be applied to theInputT
using theapply
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 classPTransform<PCollection<RequestT>,
Result<ResponseT>>
-