Search in sources :

Example 41 with PTransform

use of org.apache.beam.sdk.transforms.PTransform in project component-runtime by Talend.

the class BeamIOWrappingTest method fileOutput.

@Test
public void fileOutput() throws IOException {
    final Object source = newComponent("beamio_text", ComponentManager.ComponentType.PROCESSOR);
    final Processor processor = new BeamProcessorChainImpl((PTransform<PCollection<?>, PDone>) source, null, getPlugin(), "test", "beamio_text");
    processor.start();
    processor.beforeGroup();
    Stream.of("first", "second").forEach(data -> processor.onNext(name -> {
        assertEquals(Branches.DEFAULT_BRANCH, name);
        return data;
    }, name -> value -> fail(name + " >> " + value)));
    final AtomicReference<Object> output = new AtomicReference<>();
    processor.afterGroup(name -> {
        assertEquals(Branches.DEFAULT_BRANCH, name);
        return output::set;
    });
    processor.stop();
    final FileBasedSink.FileResult result = FileBasedSink.FileResult.class.cast(output.get());
    assertNotNull(result);
    final File file = new File(result.getTempFilename().toString());
    assertTrue(file.exists());
    assertEquals(file.getParentFile().getParentFile(), TEMPORARY_FOLDER.getRoot());
    assertEquals("first\nsecond", Files.lines(file.toPath()).collect(joining("\n")));
}
Also used : PartitionMapper(org.talend.sdk.component.api.input.PartitionMapper) PBegin(org.apache.beam.sdk.values.PBegin) PipelineResult(org.apache.beam.sdk.PipelineResult) RequiredArgsConstructor(lombok.RequiredArgsConstructor) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) Sample(org.talend.sdk.component.runtime.beam.data.Sample) Create(org.apache.beam.sdk.transforms.Create) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) GlobalWindow(org.apache.beam.sdk.transforms.windowing.GlobalWindow) Assert.fail(org.junit.Assert.fail) ClassRule(org.junit.ClassRule) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Processor(org.talend.sdk.component.runtime.output.Processor) Collectors.joining(java.util.stream.Collectors.joining) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) List(java.util.List) Branches(org.talend.sdk.component.runtime.output.Branches) Stream(java.util.stream.Stream) ParDo(org.apache.beam.sdk.transforms.ParDo) Optional(java.util.Optional) BeamMapperImpl(org.talend.sdk.component.runtime.beam.impl.BeamMapperImpl) BeamProcessorChainImpl(org.talend.sdk.component.runtime.beam.impl.BeamProcessorChainImpl) Getter(lombok.Getter) CapturingPipeline(org.talend.sdk.component.runtime.beam.impl.CapturingPipeline) Coder(org.apache.beam.sdk.coders.Coder) HashMap(java.util.HashMap) Option(org.talend.sdk.component.api.configuration.Option) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) PTransform(org.apache.beam.sdk.transforms.PTransform) FileBasedSink(org.apache.beam.sdk.io.FileBasedSink) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) DelegatingTransform(org.talend.sdk.component.runtime.beam.transform.DelegatingTransform) Input(org.talend.sdk.component.runtime.input.Input) OutputStream(java.io.OutputStream) DoFn(org.apache.beam.sdk.transforms.DoFn) PDone(org.apache.beam.sdk.values.PDone) Files(java.nio.file.Files) PAssert(org.apache.beam.sdk.testing.PAssert) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) PCollection(org.apache.beam.sdk.values.PCollection) InputStreamReader(java.io.InputStreamReader) File(java.io.File) Collectors.toList(java.util.stream.Collectors.toList) Mapper(org.talend.sdk.component.runtime.input.Mapper) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Instant(org.joda.time.Instant) JarLocation.jarLocation(org.apache.ziplock.JarLocation.jarLocation) BufferedReader(java.io.BufferedReader) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) Assert.assertEquals(org.junit.Assert.assertEquals) TextIO(org.apache.beam.sdk.io.TextIO) TemporaryFolder(org.junit.rules.TemporaryFolder) InputStream(java.io.InputStream) FileBasedSink(org.apache.beam.sdk.io.FileBasedSink) PCollection(org.apache.beam.sdk.values.PCollection) Processor(org.talend.sdk.component.runtime.output.Processor) BeamProcessorChainImpl(org.talend.sdk.component.runtime.beam.impl.BeamProcessorChainImpl) PDone(org.apache.beam.sdk.values.PDone) AtomicReference(java.util.concurrent.atomic.AtomicReference) File(java.io.File) Test(org.junit.Test)

Aggregations

PTransform (org.apache.beam.sdk.transforms.PTransform)41 PCollection (org.apache.beam.sdk.values.PCollection)29 Test (org.junit.Test)18 AppliedPTransform (org.apache.beam.sdk.runners.AppliedPTransform)11 PBegin (org.apache.beam.sdk.values.PBegin)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Map (java.util.Map)10 TupleTag (org.apache.beam.sdk.values.TupleTag)10 DoFn (org.apache.beam.sdk.transforms.DoFn)9 Coder (org.apache.beam.sdk.coders.Coder)8 Create (org.apache.beam.sdk.transforms.Create)8 ParDo (org.apache.beam.sdk.transforms.ParDo)7 PDone (org.apache.beam.sdk.values.PDone)7 PCollectionTuple (org.apache.beam.sdk.values.PCollectionTuple)6 Collection (java.util.Collection)5 HashMap (java.util.HashMap)5 Collectors.toList (java.util.stream.Collectors.toList)5 Schema (org.apache.beam.sdk.schemas.Schema)5