use of org.apache.beam.sdk.io.GenerateSequence in project beam by apache.
the class PipelineTest method testReplaceAllIncomplete.
/**
* Tests that {@link Pipeline#replaceAll(List)} throws when one of the PTransformOverride still
* matches.
*/
@Test
public void testReplaceAllIncomplete() {
pipeline.enableAbandonedNodeEnforcement(false);
pipeline.apply(GenerateSequence.from(0));
// The order is such that the output of the second will match the first, which is not permitted
thrown.expect(IllegalStateException.class);
pipeline.replaceAll(ImmutableList.of(PTransformOverride.of(new PTransformMatcher() {
@Override
public boolean matches(AppliedPTransform<?, ?, ?> application) {
return application.getTransform() instanceof Create.Values;
}
}, new CreateValuesToEmptyFlattenOverride()), PTransformOverride.of(new PTransformMatcher() {
@Override
public boolean matches(AppliedPTransform<?, ?, ?> application) {
return application.getTransform() instanceof GenerateSequence;
}
}, new GenerateSequenceToCreateOverride())));
}
use of org.apache.beam.sdk.io.GenerateSequence in project beam by apache.
the class PipelineTest method testReplaceAll.
@Test
public void testReplaceAll() {
pipeline.enableAbandonedNodeEnforcement(false);
pipeline.apply("unbounded", GenerateSequence.from(0));
pipeline.apply("bounded", GenerateSequence.from(0).to(100));
pipeline.replaceAll(ImmutableList.of(PTransformOverride.of(new PTransformMatcher() {
@Override
public boolean matches(AppliedPTransform<?, ?, ?> application) {
return application.getTransform() instanceof GenerateSequence;
}
}, new GenerateSequenceToCreateOverride()), PTransformOverride.of(new PTransformMatcher() {
@Override
public boolean matches(AppliedPTransform<?, ?, ?> application) {
return application.getTransform() instanceof Create.Values;
}
}, new CreateValuesToEmptyFlattenOverride())));
pipeline.traverseTopologically(new PipelineVisitor.Defaults() {
@Override
public CompositeBehavior enterCompositeTransform(Node node) {
if (!node.isRootNode()) {
assertThat(node.getTransform().getClass(), not(anyOf(Matchers.<Class<? extends PTransform>>equalTo(GenerateSequence.class), Matchers.<Class<? extends PTransform>>equalTo(Create.Values.class))));
}
return CompositeBehavior.ENTER_TRANSFORM;
}
});
}
use of org.apache.beam.sdk.io.GenerateSequence in project beam by apache.
the class TransformHierarchyTest method visitAfterReplace.
/**
* Tests that visiting the {@link TransformHierarchy} after replacing nodes does not visit any
* of the original nodes or inaccessible values but does visit all of the replacement nodes,
* new inaccessible replacement values, and the original output values.
*/
@Test
public void visitAfterReplace() {
Node root = hierarchy.getCurrent();
final SingleOutput<Long, Long> originalParDo = ParDo.of(new DoFn<Long, Long>() {
@ProcessElement
public void processElement(ProcessContext ctxt) {
ctxt.output(ctxt.element() + 1L);
}
});
GenerateSequence genUpstream = GenerateSequence.from(0);
PCollection<Long> upstream = pipeline.apply(genUpstream);
PCollection<Long> output = upstream.apply("Original", originalParDo);
Node upstreamNode = hierarchy.pushNode("Upstream", pipeline.begin(), genUpstream);
hierarchy.finishSpecifyingInput();
hierarchy.setOutput(upstream);
hierarchy.popNode();
Node original = hierarchy.pushNode("Original", upstream, originalParDo);
hierarchy.finishSpecifyingInput();
hierarchy.setOutput(output);
hierarchy.popNode();
final TupleTag<Long> longs = new TupleTag<>();
final MultiOutput<Long, Long> replacementParDo = ParDo.of(new DoFn<Long, Long>() {
@ProcessElement
public void processElement(ProcessContext ctxt) {
ctxt.output(ctxt.element() + 1L);
}
}).withOutputTags(longs, TupleTagList.empty());
PTransform<PCollection<Long>, PCollection<Long>> replacementComposite = new PTransform<PCollection<Long>, PCollection<Long>>() {
@Override
public PCollection<Long> expand(PCollection<Long> input) {
return input.apply("Contained", replacementParDo).get(longs);
}
};
PCollectionTuple replacementOutput = upstream.apply("Contained", replacementParDo);
Node compositeNode = hierarchy.replaceNode(original, upstream, replacementComposite);
Node replacementParNode = hierarchy.pushNode("Original/Contained", upstream, replacementParDo);
hierarchy.finishSpecifyingInput();
hierarchy.setOutput(replacementOutput);
hierarchy.popNode();
hierarchy.setOutput(replacementOutput.get(longs));
Entry<TupleTag<?>, PValue> replacementLongs = Iterables.getOnlyElement(replacementOutput.expand().entrySet());
hierarchy.replaceOutputs(Collections.<PValue, ReplacementOutput>singletonMap(replacementOutput.get(longs), ReplacementOutput.of(TaggedPValue.ofExpandedValue(output), TaggedPValue.of(replacementLongs.getKey(), replacementLongs.getValue()))));
hierarchy.popNode();
final Set<Node> visitedCompositeNodes = new HashSet<>();
final Set<Node> visitedPrimitiveNodes = new HashSet<>();
Set<PValue> visitedValues = hierarchy.visit(new Defaults() {
@Override
public CompositeBehavior enterCompositeTransform(Node node) {
visitedCompositeNodes.add(node);
return CompositeBehavior.ENTER_TRANSFORM;
}
@Override
public void visitPrimitiveTransform(Node node) {
visitedPrimitiveNodes.add(node);
}
});
/*
Final Graph:
Upstream -> Upstream.out -> Composite -> (ReplacementParDo -> OriginalParDo.out)
*/
assertThat(visitedCompositeNodes, containsInAnyOrder(root, compositeNode));
assertThat(visitedPrimitiveNodes, containsInAnyOrder(upstreamNode, replacementParNode));
assertThat(visitedValues, Matchers.<PValue>containsInAnyOrder(upstream, output));
}
use of org.apache.beam.sdk.io.GenerateSequence in project beam by apache.
the class SdkComponentsTest method registerTransformAfterChildren.
@Test
public void registerTransformAfterChildren() throws IOException {
Create.Values<Long> create = Create.of(1L, 2L, 3L);
GenerateSequence createChild = GenerateSequence.from(0);
PCollection<Long> pt = pipeline.apply(create);
String userName = "my_transform";
String childUserName = "my_transform/my_nesting";
AppliedPTransform<?, ?, ?> transform = AppliedPTransform.<PBegin, PCollection<Long>, Create.Values<Long>>of(userName, pipeline.begin().expand(), pt.expand(), create, pipeline);
AppliedPTransform<?, ?, ?> childTransform = AppliedPTransform.<PBegin, PCollection<Long>, GenerateSequence>of(childUserName, pipeline.begin().expand(), pt.expand(), createChild, pipeline);
String childId = components.registerPTransform(childTransform, Collections.<AppliedPTransform<?, ?, ?>>emptyList());
String parentId = components.registerPTransform(transform, Collections.<AppliedPTransform<?, ?, ?>>singletonList(childTransform));
Components components = this.components.toComponents();
assertThat(components.getTransformsOrThrow(parentId).getSubtransforms(0), equalTo(childId));
assertThat(components.getTransformsOrThrow(childId).getSubtransformsCount(), equalTo(0));
}
use of org.apache.beam.sdk.io.GenerateSequence in project beam by apache.
the class SdkComponentsTest method registerTransformWithUnregisteredChildren.
/**
* Tests that trying to register a transform which has unregistered children throws.
*/
@Test
public void registerTransformWithUnregisteredChildren() throws IOException {
Create.Values<Long> create = Create.of(1L, 2L, 3L);
GenerateSequence createChild = GenerateSequence.from(0);
PCollection<Long> pt = pipeline.apply(create);
String userName = "my_transform";
String childUserName = "my_transform/my_nesting";
AppliedPTransform<?, ?, ?> transform = AppliedPTransform.<PBegin, PCollection<Long>, Create.Values<Long>>of(userName, pipeline.begin().expand(), pt.expand(), create, pipeline);
AppliedPTransform<?, ?, ?> childTransform = AppliedPTransform.<PBegin, PCollection<Long>, GenerateSequence>of(childUserName, pipeline.begin().expand(), pt.expand(), createChild, pipeline);
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(childTransform.toString());
components.registerPTransform(transform, Collections.<AppliedPTransform<?, ?, ?>>singletonList(childTransform));
}
Aggregations