Search in sources :

Example 1 with GenerateSequence

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())));
}
Also used : PTransformMatcher(org.apache.beam.sdk.runners.PTransformMatcher) Create(org.apache.beam.sdk.transforms.Create) PTransformOverride(org.apache.beam.sdk.runners.PTransformOverride) GenerateSequence(org.apache.beam.sdk.io.GenerateSequence) Test(org.junit.Test)

Example 2 with GenerateSequence

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;
        }
    });
}
Also used : PTransformMatcher(org.apache.beam.sdk.runners.PTransformMatcher) Node(org.apache.beam.sdk.runners.TransformHierarchy.Node) GenerateSequence(org.apache.beam.sdk.io.GenerateSequence) Create(org.apache.beam.sdk.transforms.Create) PipelineVisitor(org.apache.beam.sdk.Pipeline.PipelineVisitor) PTransformOverride(org.apache.beam.sdk.runners.PTransformOverride) PTransform(org.apache.beam.sdk.transforms.PTransform) AppliedPTransform(org.apache.beam.sdk.runners.AppliedPTransform) Test(org.junit.Test)

Example 3 with GenerateSequence

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));
}
Also used : Node(org.apache.beam.sdk.runners.TransformHierarchy.Node) TupleTag(org.apache.beam.sdk.values.TupleTag) TaggedPValue(org.apache.beam.sdk.values.TaggedPValue) PValue(org.apache.beam.sdk.values.PValue) GenerateSequence(org.apache.beam.sdk.io.GenerateSequence) PCollection(org.apache.beam.sdk.values.PCollection) DoFn(org.apache.beam.sdk.transforms.DoFn) Defaults(org.apache.beam.sdk.Pipeline.PipelineVisitor.Defaults) PCollectionTuple(org.apache.beam.sdk.values.PCollectionTuple) PTransform(org.apache.beam.sdk.transforms.PTransform) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with GenerateSequence

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));
}
Also used : Components(org.apache.beam.sdk.common.runner.v1.RunnerApi.Components) PCollection(org.apache.beam.sdk.values.PCollection) Create(org.apache.beam.sdk.transforms.Create) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) PBegin(org.apache.beam.sdk.values.PBegin) GenerateSequence(org.apache.beam.sdk.io.GenerateSequence) Test(org.junit.Test)

Example 5 with GenerateSequence

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));
}
Also used : PCollection(org.apache.beam.sdk.values.PCollection) Create(org.apache.beam.sdk.transforms.Create) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) PBegin(org.apache.beam.sdk.values.PBegin) GenerateSequence(org.apache.beam.sdk.io.GenerateSequence) Test(org.junit.Test)

Aggregations

GenerateSequence (org.apache.beam.sdk.io.GenerateSequence)7 Test (org.junit.Test)6 PCollection (org.apache.beam.sdk.values.PCollection)5 Create (org.apache.beam.sdk.transforms.Create)4 Node (org.apache.beam.sdk.runners.TransformHierarchy.Node)3 PTransform (org.apache.beam.sdk.transforms.PTransform)3 PBegin (org.apache.beam.sdk.values.PBegin)3 PTransformMatcher (org.apache.beam.sdk.runners.PTransformMatcher)2 PTransformOverride (org.apache.beam.sdk.runners.PTransformOverride)2 DoFn (org.apache.beam.sdk.transforms.DoFn)2 PCollectionTuple (org.apache.beam.sdk.values.PCollectionTuple)2 PValue (org.apache.beam.sdk.values.PValue)2 TaggedPValue (org.apache.beam.sdk.values.TaggedPValue)2 TupleTag (org.apache.beam.sdk.values.TupleTag)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)2 HashSet (java.util.HashSet)1 PipelineVisitor (org.apache.beam.sdk.Pipeline.PipelineVisitor)1 Defaults (org.apache.beam.sdk.Pipeline.PipelineVisitor.Defaults)1 Components (org.apache.beam.sdk.common.runner.v1.RunnerApi.Components)1