Search in sources :

Example 31 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class PTransformMatchersTest method parDoWithFnTypeWithNoMatch.

@Test
public void parDoWithFnTypeWithNoMatch() {
    DoFn<Object, Object> fn = new DoFn<Object, Object>() {

        @ProcessElement
        public void process(ProcessContext ctxt) {
        }
    };
    AppliedPTransform<?, ?, ?> parDoSingle = getAppliedTransform(ParDo.of(fn));
    AppliedPTransform<?, ?, ?> parDoMulti = getAppliedTransform(ParDo.of(fn).withOutputTags(new TupleTag<>(), TupleTagList.empty()));
    PTransformMatcher matcher = PTransformMatchers.parDoWithFnType(doFnWithState.getClass());
    assertThat(matcher.matches(parDoSingle), is(false));
    assertThat(matcher.matches(parDoMulti), is(false));
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) PTransformMatcher(org.apache.beam.sdk.runners.PTransformMatcher) TupleTag(org.apache.beam.sdk.values.TupleTag) Test(org.junit.Test)

Example 32 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class PTransformMatchersTest method classEqualToMatchesSameClass.

@Test
public void classEqualToMatchesSameClass() {
    PTransformMatcher matcher = PTransformMatchers.classEqualTo(ParDo.SingleOutput.class);
    AppliedPTransform<?, ?, ?> application = getAppliedTransform(ParDo.of(new DoFn<KV<String, Integer>, Integer>() {

        @ProcessElement
        public void doStuff(ProcessContext ctxt) {
        }
    }));
    assertThat(matcher.matches(application), is(true));
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) PTransformMatcher(org.apache.beam.sdk.runners.PTransformMatcher) ParDo(org.apache.beam.sdk.transforms.ParDo) Test(org.junit.Test)

Example 33 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DirectRunnerTest method testMutatingOutputThenOutputDoFnError.

/**
 * Tests that a {@link DoFn} that mutates an output with a good equals() fails in the {@link
 * DirectRunner}.
 */
@Test
public void testMutatingOutputThenOutputDoFnError() throws Exception {
    Pipeline pipeline = getPipeline();
    pipeline.apply(Create.of(42)).apply(ParDo.of(new DoFn<Integer, List<Integer>>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            List<Integer> outputList = Arrays.asList(1, 2, 3, 4);
            c.output(outputList);
            outputList.set(0, 37);
            c.output(outputList);
        }
    }));
    thrown.expect(IllegalMutationException.class);
    thrown.expectMessage("output");
    thrown.expectMessage("must not be mutated");
    pipeline.run();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DoFn(org.apache.beam.sdk.transforms.DoFn) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Pipeline(org.apache.beam.sdk.Pipeline) Test(org.junit.Test)

Example 34 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DirectRunnerTest method testFromOptionsIfIgnoredFieldsGettingDropped.

/**
 * Tests that {@link DirectRunner#fromOptions(PipelineOptions)} drops {@link PipelineOptions}
 * marked with {@link JsonIgnore} fields.
 */
@Test
public void testFromOptionsIfIgnoredFieldsGettingDropped() {
    TestSerializationOfOptions options = PipelineOptionsFactory.fromArgs("--foo=testValue", "--ignoredField=overridden", "--runner=DirectRunner").as(TestSerializationOfOptions.class);
    assertEquals("testValue", options.getFoo());
    assertEquals("overridden", options.getIgnoredField());
    Pipeline p = Pipeline.create(options);
    PCollection<Integer> pc = p.apply(Create.of("1")).apply(ParDo.of(new DoFn<String, Integer>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            TestSerializationOfOptions options = c.getPipelineOptions().as(TestSerializationOfOptions.class);
            assertEquals("testValue", options.getFoo());
            assertEquals("not overridden", options.getIgnoredField());
            c.output(Integer.parseInt(c.element()));
        }
    }));
    PAssert.that(pc).containsInAnyOrder(1);
    p.run();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DoFn(org.apache.beam.sdk.transforms.DoFn) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Pipeline(org.apache.beam.sdk.Pipeline) Test(org.junit.Test)

Example 35 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DirectRunnerTest method testMutatingInputDoFnError.

/**
 * Tests that a {@link DoFn} that mutates its input with a good equals() fails in the {@link
 * DirectRunner}.
 */
@Test
public void testMutatingInputDoFnError() throws Exception {
    Pipeline pipeline = getPipeline();
    pipeline.apply(Create.of(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6)).withCoder(ListCoder.of(VarIntCoder.of()))).apply(ParDo.of(new DoFn<List<Integer>, Integer>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            List<Integer> inputList = c.element();
            inputList.set(0, 37);
            c.output(12);
        }
    }));
    thrown.expect(IllegalMutationException.class);
    thrown.expectMessage("Input");
    thrown.expectMessage("must not be mutated");
    pipeline.run();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DoFn(org.apache.beam.sdk.transforms.DoFn) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Pipeline(org.apache.beam.sdk.Pipeline) Test(org.junit.Test)

Aggregations

DoFn (org.apache.beam.sdk.transforms.DoFn)154 Test (org.junit.Test)98 Pipeline (org.apache.beam.sdk.Pipeline)60 KV (org.apache.beam.sdk.values.KV)45 TupleTag (org.apache.beam.sdk.values.TupleTag)28 StateSpec (org.apache.beam.sdk.state.StateSpec)26 Instant (org.joda.time.Instant)26 ArrayList (java.util.ArrayList)23 TestPipeline (org.apache.beam.sdk.testing.TestPipeline)23 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)22 PCollection (org.apache.beam.sdk.values.PCollection)21 TimerSpec (org.apache.beam.sdk.state.TimerSpec)19 WindowedValue (org.apache.beam.sdk.util.WindowedValue)18 PCollectionView (org.apache.beam.sdk.values.PCollectionView)18 HashMap (java.util.HashMap)17 Coder (org.apache.beam.sdk.coders.Coder)17 List (java.util.List)16 Map (java.util.Map)14 ValueState (org.apache.beam.sdk.state.ValueState)14 RunnerApi (org.apache.beam.model.pipeline.v1.RunnerApi)13