InputT
- the type of the DoFn
's (main) input elementsOutputT
- the type of the DoFn
's (main) output elementspublic class DoFnTester<InputT,OutputT>
extends java.lang.Object
implements java.lang.AutoCloseable
DoFn
.
For example:
DoFn<InputT, OutputT> fn = ...;
DoFnTester<InputT, OutputT> fnTester = DoFnTester.of(fn);
// Set arguments shared across all bundles:
fnTester.setSideInputs(...); // If fn takes side inputs.
fnTester.setOutputTags(...); // If fn writes to more than one output.
// Process a bundle containing a single input element:
Input testInput = ...;
List<OutputT> testOutputs = fnTester.processBundle(testInput);
Assert.assertThat(testOutputs, Matchers.hasItems(...));
// Process a bigger bundle:
Assert.assertThat(fnTester.processBundle(i1, i2, ...), Matchers.hasItems(...));
Modifier and Type | Class and Description |
---|---|
static class |
DoFnTester.CloningBehavior
When a
DoFnTester should clone the DoFn under test and how it should manage
the lifecycle of the DoFn . |
Modifier and Type | Method and Description |
---|---|
void |
clearOutputElements()
Clears the record of the elements output so far to the main output.
|
<T> void |
clearOutputElements(TupleTag<T> tag)
Clears the record of the elements output so far to the output with the given tag.
|
void |
close() |
DoFn.ProcessContext |
createProcessContext(ValueInSingleWindow<InputT> element) |
void |
finishBundle()
Calls the
DoFn.FinishBundle method of the DoFn under test. |
DoFnTester.CloningBehavior |
getCloningBehavior()
Indicates whether this
DoFnTester will clone the DoFn under test. |
TupleTag<OutputT> |
getMainOutputTag() |
<T> java.util.List<ValueInSingleWindow<T>> |
getMutableOutput(TupleTag<T> tag) |
PipelineOptions |
getPipelineOptions() |
static <InputT,OutputT> |
of(DoFn<InputT,OutputT> fn)
Returns a
DoFnTester supporting unit-testing of the given
DoFn . |
java.util.List<OutputT> |
peekOutputElements()
Returns the elements output so far to the main output.
|
<T> java.util.List<T> |
peekOutputElements(TupleTag<T> tag)
Returns the elements output so far to the output with the
given tag.
|
java.util.List<TimestampedValue<OutputT>> |
peekOutputElementsInWindow(BoundedWindow window)
Returns the elements output so far to the main output in the provided window with associated
timestamps.
|
java.util.List<TimestampedValue<OutputT>> |
peekOutputElementsInWindow(TupleTag<OutputT> tag,
BoundedWindow window)
Returns the elements output so far to the specified output in the provided window with
associated timestamps.
|
java.util.List<TimestampedValue<OutputT>> |
peekOutputElementsWithTimestamp()
Returns the elements output so far to the main output with associated timestamps.
|
java.util.List<OutputT> |
processBundle(InputT... inputElements)
A convenience method for testing
DoFns with bundles of elements. |
java.util.List<OutputT> |
processBundle(java.lang.Iterable<? extends InputT> inputElements)
A convenience operation that first calls
startBundle() ,
then calls processElement(InputT) on each of the input elements, then
calls finishBundle() , then returns the result of
takeOutputElements() . |
void |
processElement(InputT element)
Calls the
DoFn.ProcessElement method on the DoFn under test, in a
context where DoFn.ProcessContext.element() returns the
given element and the element is in the global window. |
void |
processTimestampedElement(TimestampedValue<InputT> element)
Calls
DoFn.ProcessElement on the DoFn under test, in a
context where DoFn.ProcessContext.element() returns the
given element and timestamp and the element is in the global window. |
void |
processWindowedElement(InputT element,
Instant timestamp,
BoundedWindow window)
Calls
DoFn.ProcessElement on the DoFn under test, in a
context where DoFn.ProcessContext.element() returns the
given element and timestamp and the element is in the given window. |
void |
setCloningBehavior(DoFnTester.CloningBehavior newValue)
Instruct this
DoFnTester whether or not to clone the DoFn under test. |
<T> void |
setSideInput(PCollectionView<T> sideInput,
BoundedWindow window,
T value)
Registers the values of a side input
PCollectionView to pass to the DoFn
under test. |
void |
setSideInputs(java.util.Map<PCollectionView<?>,java.util.Map<BoundedWindow,?>> sideInputs)
Registers the tuple of values of the side input
PCollectionView s to
pass to the DoFn under test. |
void |
startBundle()
Calls the
DoFn.StartBundle method on the DoFn under test. |
java.util.List<OutputT> |
takeOutputElements()
Returns the elements output so far to the main output.
|
<T> java.util.List<T> |
takeOutputElements(TupleTag<T> tag)
Returns the elements output so far to the output with the given tag.
|
java.util.List<TimestampedValue<OutputT>> |
takeOutputElementsWithTimestamp()
Returns the elements output so far to the main output with associated timestamps.
|
public static <InputT,OutputT> DoFnTester<InputT,OutputT> of(DoFn<InputT,OutputT> fn)
DoFnTester
supporting unit-testing of the given
DoFn
. By default, uses DoFnTester.CloningBehavior.CLONE_ONCE
.
The only supported extra parameter of the DoFn.ProcessElement
method is
BoundedWindow
.
public void setSideInputs(java.util.Map<PCollectionView<?>,java.util.Map<BoundedWindow,?>> sideInputs)
PCollectionView
s to
pass to the DoFn
under test.
Resets the state of this DoFnTester
.
If this isn't called, DoFnTester
assumes the
DoFn
takes no side inputs.
public <T> void setSideInput(PCollectionView<T> sideInput, BoundedWindow window, T value)
PCollectionView
to pass to the DoFn
under test.
The provided value is the final value of the side input in the specified window, not the value of the input PCollection in that window.
If this isn't called, DoFnTester
will return the default value for any side input
that is used.
public PipelineOptions getPipelineOptions()
public void setCloningBehavior(DoFnTester.CloningBehavior newValue)
DoFnTester
whether or not to clone the DoFn
under test.public DoFnTester.CloningBehavior getCloningBehavior()
DoFnTester
will clone the DoFn
under test.public java.util.List<OutputT> processBundle(java.lang.Iterable<? extends InputT> inputElements) throws java.lang.Exception
startBundle()
,
then calls processElement(InputT)
on each of the input elements, then
calls finishBundle()
, then returns the result of
takeOutputElements()
.java.lang.Exception
@SafeVarargs public final java.util.List<OutputT> processBundle(InputT... inputElements) throws java.lang.Exception
DoFns
with bundles of elements.
Logic proceeds as follows:
startBundle()
.processElement(InputT)
on each of the arguments.finishBundle()
.takeOutputElements()
.java.lang.Exception
public void startBundle() throws java.lang.Exception
DoFn.StartBundle
method on the DoFn
under test.
If needed, first creates a fresh instance of the DoFn
under test and calls
DoFn.Setup
.
java.lang.Exception
public void processElement(InputT element) throws java.lang.Exception
DoFn.ProcessElement
method on the DoFn
under test, in a
context where DoFn.ProcessContext.element()
returns the
given element and the element is in the global window.
Will call startBundle()
automatically, if it hasn't
already been called.
java.lang.IllegalStateException
- if the DoFn
under test has already
been finishedjava.lang.Exception
public void processTimestampedElement(TimestampedValue<InputT> element) throws java.lang.Exception
DoFn.ProcessElement
on the DoFn
under test, in a
context where DoFn.ProcessContext.element()
returns the
given element and timestamp and the element is in the global window.
Will call startBundle()
automatically, if it hasn't
already been called.
java.lang.Exception
public void processWindowedElement(InputT element, Instant timestamp, BoundedWindow window) throws java.lang.Exception
DoFn.ProcessElement
on the DoFn
under test, in a
context where DoFn.ProcessContext.element()
returns the
given element and timestamp and the element is in the given window.
Will call startBundle()
automatically, if it hasn't
already been called.
java.lang.Exception
public void finishBundle() throws java.lang.Exception
DoFn.FinishBundle
method of the DoFn
under test.
If setCloningBehavior(org.apache.beam.sdk.transforms.DoFnTester.CloningBehavior)
was called with DoFnTester.CloningBehavior.CLONE_PER_BUNDLE
,
then also calls DoFn.Teardown
on the DoFn
, and it will be cloned and
DoFn.Setup
again when processing the next bundle.
java.lang.IllegalStateException
- if DoFn.FinishBundle
has already been called
for this bundle.java.lang.Exception
public java.util.List<OutputT> peekOutputElements()
takeOutputElements()
,
clearOutputElements()
@Experimental public java.util.List<TimestampedValue<OutputT>> peekOutputElementsWithTimestamp()
public java.util.List<TimestampedValue<OutputT>> peekOutputElementsInWindow(BoundedWindow window)
public java.util.List<TimestampedValue<OutputT>> peekOutputElementsInWindow(TupleTag<OutputT> tag, BoundedWindow window)
public void clearOutputElements()
peekOutputElements()
public java.util.List<OutputT> takeOutputElements()
peekOutputElements()
@Experimental public java.util.List<TimestampedValue<OutputT>> takeOutputElementsWithTimestamp()
public <T> java.util.List<T> peekOutputElements(TupleTag<T> tag)
takeOutputElements()
,
clearOutputElements()
public <T> void clearOutputElements(TupleTag<T> tag)
peekOutputElements()
public <T> java.util.List<T> takeOutputElements(TupleTag<T> tag)
peekOutputElements()
public <T> java.util.List<ValueInSingleWindow<T>> getMutableOutput(TupleTag<T> tag)
public DoFn.ProcessContext createProcessContext(ValueInSingleWindow<InputT> element)
public void close() throws java.lang.Exception
close
in interface java.lang.AutoCloseable
java.lang.Exception