Search in sources :

Example 11 with PBegin

use of org.apache.beam.sdk.values.PBegin in project beam by apache.

the class SdkComponentsTest method registerTransformNoChildren.

@Test
public void registerTransformNoChildren() throws IOException {
    Create.Values<Integer> create = Create.of(1, 2, 3);
    PCollection<Integer> pt = pipeline.apply(create);
    String userName = "my_transform/my_nesting";
    AppliedPTransform<?, ?, ?> transform = AppliedPTransform.<PBegin, PCollection<Integer>, Create.Values<Integer>>of(userName, pipeline.begin().expand(), pt.expand(), create, pipeline);
    String componentName = components.registerPTransform(transform, Collections.<AppliedPTransform<?, ?, ?>>emptyList());
    assertThat(componentName, equalTo(userName));
    assertThat(components.getExistingPTransformId(transform), equalTo(componentName));
}
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) Test(org.junit.Test)

Example 12 with PBegin

use of org.apache.beam.sdk.values.PBegin in project beam by apache.

the class SdkComponentsTest method registerTransformNullComponents.

@Test
public void registerTransformNullComponents() throws IOException {
    Create.Values<Integer> create = Create.of(1, 2, 3);
    PCollection<Integer> pt = pipeline.apply(create);
    String userName = "my_transform/my_nesting";
    AppliedPTransform<?, ?, ?> transform = AppliedPTransform.<PBegin, PCollection<Integer>, Create.Values<Integer>>of(userName, pipeline.begin().expand(), pt.expand(), create, pipeline);
    thrown.expect(NullPointerException.class);
    thrown.expectMessage("child nodes may not be null");
    components.registerPTransform(transform, null);
}
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) Test(org.junit.Test)

Example 13 with PBegin

use of org.apache.beam.sdk.values.PBegin in project beam by apache.

the class TransformHierarchyTest method visitVisitsAllPushed.

@Test
public void visitVisitsAllPushed() {
    TransformHierarchy.Node root = hierarchy.getCurrent();
    PBegin begin = PBegin.in(pipeline);
    Create.Values<Long> create = Create.of(1L);
    Read.Bounded<Long> read = Read.from(CountingSource.upTo(1L));
    PCollection<Long> created = PCollection.createPrimitiveOutputInternal(pipeline, WindowingStrategy.globalDefault(), IsBounded.BOUNDED);
    SingleOutput<Long, Long> pardo = ParDo.of(new DoFn<Long, Long>() {

        @ProcessElement
        public void processElement(ProcessContext ctxt) {
            ctxt.output(ctxt.element());
        }
    });
    PCollection<Long> mapped = PCollection.createPrimitiveOutputInternal(pipeline, WindowingStrategy.globalDefault(), IsBounded.BOUNDED);
    TransformHierarchy.Node compositeNode = hierarchy.pushNode("Create", begin, create);
    hierarchy.finishSpecifyingInput();
    assertThat(hierarchy.getCurrent(), equalTo(compositeNode));
    assertThat(compositeNode.getInputs().entrySet(), Matchers.empty());
    assertThat(compositeNode.getTransform(), Matchers.<PTransform<?, ?>>equalTo(create));
    // Not yet set
    assertThat(compositeNode.getOutputs().entrySet(), Matchers.emptyIterable());
    assertThat(compositeNode.getEnclosingNode().isRootNode(), is(true));
    TransformHierarchy.Node primitiveNode = hierarchy.pushNode("Create/Read", begin, read);
    assertThat(hierarchy.getCurrent(), equalTo(primitiveNode));
    hierarchy.finishSpecifyingInput();
    hierarchy.setOutput(created);
    hierarchy.popNode();
    assertThat(primitiveNode.getOutputs().values(), Matchers.<PValue>containsInAnyOrder(created));
    assertThat(primitiveNode.getInputs().entrySet(), Matchers.emptyIterable());
    assertThat(primitiveNode.getTransform(), Matchers.<PTransform<?, ?>>equalTo(read));
    assertThat(primitiveNode.getEnclosingNode(), equalTo(compositeNode));
    hierarchy.setOutput(created);
    // The composite is listed as outputting a PValue created by the contained primitive
    assertThat(compositeNode.getOutputs().values(), Matchers.<PValue>containsInAnyOrder(created));
    // The producer of that PValue is still the primitive in which it is first output
    assertThat(hierarchy.getProducer(created), equalTo(primitiveNode));
    hierarchy.popNode();
    TransformHierarchy.Node otherPrimitive = hierarchy.pushNode("ParDo", created, pardo);
    hierarchy.finishSpecifyingInput();
    hierarchy.setOutput(mapped);
    hierarchy.popNode();
    final Set<TransformHierarchy.Node> visitedCompositeNodes = new HashSet<>();
    final Set<TransformHierarchy.Node> visitedPrimitiveNodes = new HashSet<>();
    final Set<PValue> visitedValuesInVisitor = new HashSet<>();
    Set<PValue> visitedValues = hierarchy.visit(new PipelineVisitor.Defaults() {

        @Override
        public CompositeBehavior enterCompositeTransform(TransformHierarchy.Node node) {
            visitedCompositeNodes.add(node);
            return CompositeBehavior.ENTER_TRANSFORM;
        }

        @Override
        public void visitPrimitiveTransform(TransformHierarchy.Node node) {
            visitedPrimitiveNodes.add(node);
        }

        @Override
        public void visitValue(PValue value, TransformHierarchy.Node producer) {
            visitedValuesInVisitor.add(value);
        }
    });
    assertThat(visitedCompositeNodes, containsInAnyOrder(root, compositeNode));
    assertThat(visitedPrimitiveNodes, containsInAnyOrder(primitiveNode, otherPrimitive));
    assertThat(visitedValuesInVisitor, Matchers.<PValue>containsInAnyOrder(created, mapped));
    assertThat(visitedValuesInVisitor, equalTo(visitedValues));
}
Also used : Node(org.apache.beam.sdk.runners.TransformHierarchy.Node) Node(org.apache.beam.sdk.runners.TransformHierarchy.Node) Defaults(org.apache.beam.sdk.Pipeline.PipelineVisitor.Defaults) PBegin(org.apache.beam.sdk.values.PBegin) TaggedPValue(org.apache.beam.sdk.values.TaggedPValue) PValue(org.apache.beam.sdk.values.PValue) Read(org.apache.beam.sdk.io.Read) Create(org.apache.beam.sdk.transforms.Create) PipelineVisitor(org.apache.beam.sdk.Pipeline.PipelineVisitor) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

PBegin (org.apache.beam.sdk.values.PBegin)13 Test (org.junit.Test)12 PCollection (org.apache.beam.sdk.values.PCollection)7 Create (org.apache.beam.sdk.transforms.Create)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)4 GenerateSequence (org.apache.beam.sdk.io.GenerateSequence)3 Instant (org.joda.time.Instant)2 HashSet (java.util.HashSet)1 Pipeline (org.apache.beam.sdk.Pipeline)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 Read (org.apache.beam.sdk.io.Read)1 Node (org.apache.beam.sdk.runners.TransformHierarchy.Node)1 DisplayData (org.apache.beam.sdk.transforms.display.DisplayData)1 DisplayDataEvaluator (org.apache.beam.sdk.transforms.display.DisplayDataEvaluator)1 KV (org.apache.beam.sdk.values.KV)1 PValue (org.apache.beam.sdk.values.PValue)1 TaggedPValue (org.apache.beam.sdk.values.TaggedPValue)1