Search in sources :

Example 1 with ViewsMappingTransform

use of org.talend.sdk.component.runtime.beam.transform.ViewsMappingTransform in project component-runtime by Talend.

the class Main method main.

public static void main(final String[] args) throws IOException {
    final Config options = PipelineOptionsFactory.fromArgs(args).as(Config.class);
    final Pipeline pipeline = Pipeline.create(options);
    try (final FileWriter writer = new FileWriter(options.getInputFile())) {
        writer.write("normal;6\nmarilyn;36");
    }
    final ComponentManager manager = ComponentManager.instance();
    pipeline.apply(TalendIO.read(manager.findMapper("sample", "reader", 1, new HashMap<String, String>() {

        {
            // will be migrated to "file" with the migration handler
            put("old_file", options.getInputFile());
        }
    }).orElseThrow(() -> new IllegalArgumentException("No reader sample#reader, existing: " + manager.availablePlugins())))).apply(new ViewsMappingTransform(emptyMap(), "sample")).apply(TalendFn.asFn(manager.findProcessor("sample", "mapper", 1, emptyMap()).orElseThrow(() -> new IllegalStateException("didn't find the processor")))).apply(ParDo.of(new ToStringFn())).apply(TextIO.write().to(ValueProvider.StaticValueProvider.of(options.getOutputFile())));
    final PipelineResult.State state = pipeline.run().waitUntilFinish();
    System.out.println(state);
}
Also used : HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) PipelineResult(org.apache.beam.sdk.PipelineResult) ViewsMappingTransform(org.talend.sdk.component.runtime.beam.transform.ViewsMappingTransform) Pipeline(org.apache.beam.sdk.Pipeline)

Example 2 with ViewsMappingTransform

use of org.talend.sdk.component.runtime.beam.transform.ViewsMappingTransform in project component-runtime by Talend.

the class TalendIOTest method processorMulti.

@Test
public void processorMulti() {
    final PCollection<SampleLength> out = pipeline.apply(Create.of(new Sample("a"), new Sample("bb")).withCoder(JsonbCoder.of(Sample.class, PLUGIN))).apply(UUID.randomUUID().toString(), ParDo.of(new DoFn<Sample, JsonObject>() {

        @ProcessElement
        public void toData(final ProcessContext sample) {
            sample.output(JSONB.fromJson(JSONB.toJson(sample.element()), JsonObject.class));
        }
    })).setCoder(JsonpJsonObjectCoder.of(PLUGIN)).apply(new ViewsMappingTransform(emptyMap(), PLUGIN)).apply(TalendFn.asFn(new BaseTestProcessor() {

        @Override
        public void onNext(final InputFactory input, final OutputFactory factory) {
            factory.create(Branches.DEFAULT_BRANCH).emit(new SampleLength(JSONB.fromJson(input.read(Branches.DEFAULT_BRANCH).toString(), Sample.class).data.length()));
        }
    })).apply(ParDo.of(new DoFn<JsonObject, SampleLength>() {

        @ProcessElement
        public void onElement(final ProcessContext ctx) {
            ctx.output(JSONB.fromJson(ctx.element().getJsonArray("__default__").getJsonObject(0).toString(), SampleLength.class));
        }
    }));
    PAssert.that(out.apply(UUID.randomUUID().toString(), ParDo.of(new DoFn<SampleLength, Integer>() {

        @ProcessElement
        public void toInt(final ProcessContext pc) {
            pc.output(pc.element().len);
        }
    }))).containsInAnyOrder(1, 2);
    assertEquals(PipelineResult.State.DONE, pipeline.run().getState());
}
Also used : InputFactory(org.talend.sdk.component.runtime.output.InputFactory) DoFn(org.apache.beam.sdk.transforms.DoFn) JsonObject(javax.json.JsonObject) ViewsMappingTransform(org.talend.sdk.component.runtime.beam.transform.ViewsMappingTransform) OutputFactory(org.talend.sdk.component.runtime.output.OutputFactory) Test(org.junit.Test)

Aggregations

ViewsMappingTransform (org.talend.sdk.component.runtime.beam.transform.ViewsMappingTransform)2 FileWriter (java.io.FileWriter)1 HashMap (java.util.HashMap)1 JsonObject (javax.json.JsonObject)1 Pipeline (org.apache.beam.sdk.Pipeline)1 PipelineResult (org.apache.beam.sdk.PipelineResult)1 DoFn (org.apache.beam.sdk.transforms.DoFn)1 Test (org.junit.Test)1 ComponentManager (org.talend.sdk.component.runtime.manager.ComponentManager)1 InputFactory (org.talend.sdk.component.runtime.output.InputFactory)1 OutputFactory (org.talend.sdk.component.runtime.output.OutputFactory)1