Search in sources :

Example 16 with Processor

use of org.talend.sdk.component.runtime.output.Processor in project component-runtime by Talend.

the class BeamMapperImpl method createFlowDefinition.

private static FlowDefinition createFlowDefinition(final PTransform<PBegin, ?> begin, final String plugin, final String family, final String name) {
    final CapturingPipeline capturingPipeline = new CapturingPipeline(PipelineOptionsFactory.create());
    final CapturingPipeline.SourceExtractor sourceExtractor = new CapturingPipeline.SourceExtractor();
    capturingPipeline.apply(begin);
    capturingPipeline.traverseTopologically(sourceExtractor);
    PTransform<? super PBegin, ?> transform = sourceExtractor.getTransform();
    if (transform == null) {
        capturingPipeline.apply(begin);
        transform = capturingPipeline.getRoot();
    }
    if (Read.Bounded.class.isInstance(transform)) {
        final Processor processor = sourceExtractor.getTransforms().isEmpty() ? null : new BeamProcessorChainImpl(sourceExtractor.getTransforms(), capturingPipeline.getCoderRegistry(), plugin, family, name);
        return new FlowDefinition(Read.Bounded.class.cast(transform).getSource(), processor);
    }
    if (Read.Unbounded.class.isInstance(transform)) {
        final Processor processor = sourceExtractor.getTransforms().isEmpty() ? null : new BeamProcessorChainImpl(sourceExtractor.getTransforms(), capturingPipeline.getCoderRegistry(), plugin, family, name);
        return new FlowDefinition(Read.Unbounded.class.cast(transform).getSource(), processor);
    }
    throw new IllegalArgumentException("This implementation only supports Bounded sources");
}
Also used : Read(org.apache.beam.sdk.io.Read) Processor(org.talend.sdk.component.runtime.output.Processor)

Example 17 with Processor

use of org.talend.sdk.component.runtime.output.Processor in project component-runtime by Talend.

the class BeamProcessorChainImpl method beforeGroup.

@Override
public void beforeGroup() {
    beforeChunkCount = 0;
    for (final Processor p : processors) {
        p.beforeGroup();
        beforeChunkCount++;
    }
}
Also used : Processor(org.talend.sdk.component.runtime.output.Processor)

Example 18 with Processor

use of org.talend.sdk.component.runtime.output.Processor in project component-runtime by Talend.

the class ProcessorTest method processor.

@Test
public void processor() {
    final Processor processor = COMPONENT_FACTORY.createProcessor(SampleProcessor.class, new Object());
    final JoinInputFactory joinInputFactory = new JoinInputFactory().withInput("__default__", asList(new SampleProcessor.Sample(1), Json.createObjectBuilder().add("data", 2).build()));
    final PCollection<JsonObject> inputs = pipeline.apply(Data.of(processor.plugin(), joinInputFactory.asInputRecords()));
    final PCollection<Map<String, JsonObject>> outputs = inputs.apply(TalendFn.asFn(processor)).apply(Data.map(processor.plugin(), JsonObject.class));
    PAssert.that(outputs).satisfies((SerializableFunction<Iterable<Map<String, JsonObject>>, Void>) input -> {
        final List<Map<String, JsonObject>> result = StreamSupport.stream(input.spliterator(), false).collect(toList());
        assertEquals(2, result.size());
        result.forEach(e -> assertTrue(e.containsKey("__default__") && e.containsKey("reject")));
        assertEquals(new HashSet<>(asList(1, 2)), result.stream().map(e -> e.get("__default__").getInt("data")).collect(toSet()));
        return null;
    });
    assertEquals(PipelineResult.State.DONE, pipeline.run().waitUntilFinish());
}
Also used : JsonObject(javax.json.JsonObject) SampleProcessor(org.talend.sdk.component.junit.beam.test.SampleProcessor) PAssert(org.apache.beam.sdk.testing.PAssert) PipelineResult(org.apache.beam.sdk.PipelineResult) SerializableFunction(org.apache.beam.sdk.transforms.SerializableFunction) Test(org.junit.Test) JoinInputFactory(org.talend.sdk.component.junit.JoinInputFactory) PCollection(org.apache.beam.sdk.values.PCollection) Processor(org.talend.sdk.component.runtime.output.Processor) HashSet(java.util.HashSet) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Rule(org.junit.Rule) Arrays.asList(java.util.Arrays.asList) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Map(java.util.Map) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Json(javax.json.Json) StreamSupport(java.util.stream.StreamSupport) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ClassRule(org.junit.ClassRule) SimpleComponentRule(org.talend.sdk.component.junit.SimpleComponentRule) Collectors.toSet(java.util.stream.Collectors.toSet) TalendFn(org.talend.sdk.component.runtime.beam.TalendFn) SampleProcessor(org.talend.sdk.component.junit.beam.test.SampleProcessor) Processor(org.talend.sdk.component.runtime.output.Processor) JsonObject(javax.json.JsonObject) JoinInputFactory(org.talend.sdk.component.junit.JoinInputFactory) JsonObject(javax.json.JsonObject) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with Processor

use of org.talend.sdk.component.runtime.output.Processor in project component-runtime by Talend.

the class TInOutputBeamTest method processor.

@Test
@Ignore("You need to complete this test with your own data and assertions")
public void processor() {
    // Output configuration
    // Setup your component configuration for the test here
    final TInOutputConfiguration configuration = new TInOutputConfiguration();
    // We create the component processor instance using the configuration filled above
    final Processor processor = COMPONENT_FACTORY.createProcessor(TInOutput.class, configuration);
    // The join input factory construct inputs test data for every input branch you have defined for this component
    // Make sure to fil in some test data for the branches you want to test
    // You can also remove the branches that you don't need from the factory below
    final JoinInputFactory joinInputFactory = new JoinInputFactory().withInput("__default__", asList());
    // Convert it to a beam "source"
    final PCollection<JsonObject> inputs = pipeline.apply(Data.of(processor.plugin(), joinInputFactory.asInputRecords()));
    // add our processor right after to see each data as configured previously
    inputs.apply(TalendFn.asFn(processor)).apply(Data.map(processor.plugin(), JsonObject.class));
    // run the pipeline and ensure the execution was successful
    assertEquals(PipelineResult.State.DONE, pipeline.run().waitUntilFinish());
}
Also used : Processor(org.talend.sdk.component.runtime.output.Processor) JoinInputFactory(org.talend.sdk.component.junit.JoinInputFactory) JsonObject(javax.json.JsonObject) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 20 with Processor

use of org.talend.sdk.component.runtime.output.Processor in project component-runtime by Talend.

the class TInProcessorBeamTest method processor.

@Test
@Ignore("You need to complete this test with your own data and assertions")
public void processor() {
    // Processor configuration
    // Setup your component configuration for the test here
    final TInProcessorConfiguration configuration = new TInProcessorConfiguration();
    // We create the component processor instance using the configuration filled above
    final Processor processor = COMPONENT_FACTORY.createProcessor(TInProcessor.class, configuration);
    // The join input factory construct inputs test data for every input branch you have defined for this component
    // Make sure to fil in some test data for the branches you want to test
    // You can also remove the branches that you don't need from the factory below
    final JoinInputFactory joinInputFactory = new JoinInputFactory().withInput("__default__", asList());
    // Convert it to a beam "source"
    final PCollection<JsonObject> inputs = pipeline.apply(Data.of(processor.plugin(), joinInputFactory.asInputRecords()));
    // add our processor right after to see each data as configured previously
    final PCollection<Map<String, JsonObject>> outputs = inputs.apply(TalendFn.asFn(processor)).apply(Data.map(processor.plugin(), JsonObject.class));
    PAssert.that(outputs).satisfies((SerializableFunction<Iterable<Map<String, JsonObject>>, Void>) input -> {
        final List<Map<String, JsonObject>> result = StreamSupport.stream(input.spliterator(), false).collect(toList());
        return null;
    });
    // run the pipeline and ensure the execution was successful
    assertEquals(PipelineResult.State.DONE, pipeline.run().waitUntilFinish());
}
Also used : JsonObject(javax.json.JsonObject) PAssert(org.apache.beam.sdk.testing.PAssert) PipelineResult(org.apache.beam.sdk.PipelineResult) SerializableFunction(org.apache.beam.sdk.transforms.SerializableFunction) Test(org.junit.Test) JoinInputFactory(org.talend.sdk.component.junit.JoinInputFactory) PCollection(org.apache.beam.sdk.values.PCollection) Processor(org.talend.sdk.component.runtime.output.Processor) Data(org.talend.sdk.component.junit.beam.Data) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Rule(org.junit.Rule) Ignore(org.junit.Ignore) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) StreamSupport(java.util.stream.StreamSupport) ClassRule(org.junit.ClassRule) SimpleComponentRule(org.talend.sdk.component.junit.SimpleComponentRule) Assert.assertEquals(org.junit.Assert.assertEquals) TalendFn(org.talend.sdk.component.runtime.beam.TalendFn) Processor(org.talend.sdk.component.runtime.output.Processor) JoinInputFactory(org.talend.sdk.component.junit.JoinInputFactory) JsonObject(javax.json.JsonObject) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Processor (org.talend.sdk.component.runtime.output.Processor)20 List (java.util.List)8 Map (java.util.Map)8 Collectors.toList (java.util.stream.Collectors.toList)8 JsonObject (javax.json.JsonObject)8 Collection (java.util.Collection)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)6 Stream (java.util.stream.Stream)6 PipelineResult (org.apache.beam.sdk.PipelineResult)6 PCollection (org.apache.beam.sdk.values.PCollection)6 IOException (java.io.IOException)5 Arrays.asList (java.util.Arrays.asList)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 PAssert (org.apache.beam.sdk.testing.PAssert)5 TestPipeline (org.apache.beam.sdk.testing.TestPipeline)5 ClassRule (org.junit.ClassRule)5 Rule (org.junit.Rule)5 Mapper (org.talend.sdk.component.runtime.input.Mapper)5 BufferedReader (java.io.BufferedReader)4