Search in sources :

Example 1 with OutputFactory

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

the class BeamProcessorChainImpl method onNext.

@Override
public void onNext(final InputFactory input, final OutputFactory output) {
    Collection<InputFactory> finalInput = singletonList(input);
    if (!preProcessors.isEmpty()) {
        for (final Processor p : preProcessors) {
            final StoringOuputFactory tmpOutput = new StoringOuputFactory();
            finalInput.forEach(in -> p.onNext(in, tmpOutput));
            if (tmpOutput.getValues() == null) {
                // chain is stopped
                return;
            }
            finalInput = tmpOutput.getValues().stream().map(val -> (InputFactory) name -> {
                if (!Branches.DEFAULT_BRANCH.equals(name)) {
                    throw new IllegalArgumentException("Only default branch is supported at the moment");
                }
                return val;
            }).collect(toList());
        }
    }
    finalInput.forEach(in -> lastProcessor.onNext(in, output));
}
Also used : CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) PBegin(org.apache.beam.sdk.values.PBegin) ObjectInputStream(java.io.ObjectInputStream) Coder(org.apache.beam.sdk.coders.Coder) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) OutputFactory(org.talend.sdk.component.runtime.output.OutputFactory) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) PTransform(org.apache.beam.sdk.transforms.PTransform) ByteArrayInputStream(java.io.ByteArrayInputStream) TupleTag(org.apache.beam.sdk.values.TupleTag) InputFactory(org.talend.sdk.component.runtime.output.InputFactory) Map(java.util.Map) PCollectionTuple(org.apache.beam.sdk.values.PCollectionTuple) InvalidObjectException(java.io.InvalidObjectException) OutputStream(java.io.OutputStream) DoFn(org.apache.beam.sdk.transforms.DoFn) ContainerFinder(org.talend.sdk.component.runtime.serialization.ContainerFinder) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Delegated(org.talend.sdk.component.runtime.base.Delegated) IOException(java.io.IOException) PCollection(org.apache.beam.sdk.values.PCollection) Processor(org.talend.sdk.component.runtime.output.Processor) Serializer(org.talend.sdk.component.runtime.base.Serializer) EnhancedObjectInputStream(org.talend.sdk.component.runtime.serialization.EnhancedObjectInputStream) Serializable(java.io.Serializable) ObjectStreamException(java.io.ObjectStreamException) POutput(org.apache.beam.sdk.values.POutput) Lifecycle(org.talend.sdk.component.runtime.base.Lifecycle) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Branches(org.talend.sdk.component.runtime.output.Branches) Stream(java.util.stream.Stream) AllArgsConstructor(lombok.AllArgsConstructor) Collections(java.util.Collections) WindowingStrategy(org.apache.beam.sdk.values.WindowingStrategy) InputStream(java.io.InputStream) InputFactory(org.talend.sdk.component.runtime.output.InputFactory) Processor(org.talend.sdk.component.runtime.output.Processor)

Example 2 with OutputFactory

use of org.talend.sdk.component.runtime.output.OutputFactory 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)

Example 3 with OutputFactory

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

the class DIBatchSimulationTest method doDi.

private void doDi(final ComponentManager manager, final Collection<Object> sourceData, final Collection<Object> processorData, final Optional<Processor> proc, final Optional<Mapper> mapper) {
    final Map<String, Object> globalMap = new HashMap<>();
    try {
        final Processor processor = proc.orElseThrow(() -> new IllegalStateException("scanning failed"));
        JobStateAware.init(processor, globalMap);
        final Jsonb jsonbProcessor = Jsonb.class.cast(manager.findPlugin(processor.plugin()).get().get(ComponentManager.AllServices.class).getServices().get(Jsonb.class));
        final AutoChunkProcessor processorProcessor = new AutoChunkProcessor(100, processor);
        processorProcessor.start();
        globalMap.put("processorProcessor", processorProcessor);
        final InputsHandler inputsHandlerProcessor = new InputsHandler(jsonbProcessor);
        inputsHandlerProcessor.addConnection("FLOW", row1Struct.class);
        final OutputsHandler outputHandlerProcessor = new OutputsHandler(jsonbProcessor);
        final InputFactory inputsProcessor = inputsHandlerProcessor.asInputFactory();
        final OutputFactory outputsProcessor = outputHandlerProcessor.asOutputFactory();
        final Mapper tempMapperMapper = mapper.orElseThrow(() -> new IllegalStateException("scanning failed"));
        JobStateAware.init(tempMapperMapper, globalMap);
        doRun(manager, sourceData, processorData, globalMap, processorProcessor, inputsHandlerProcessor, outputHandlerProcessor, inputsProcessor, outputsProcessor, tempMapperMapper);
    } finally {
        doClose(globalMap);
    }
}
Also used : InputFactory(org.talend.sdk.component.runtime.output.InputFactory) AutoChunkProcessor(org.talend.sdk.component.runtime.di.AutoChunkProcessor) Processor(org.talend.sdk.component.runtime.output.Processor) HashMap(java.util.HashMap) OutputsHandler(org.talend.sdk.component.runtime.di.OutputsHandler) ToString(lombok.ToString) PartitionMapper(org.talend.sdk.component.api.input.PartitionMapper) ChainedMapper(org.talend.sdk.component.runtime.manager.chain.ChainedMapper) Mapper(org.talend.sdk.component.runtime.input.Mapper) Jsonb(javax.json.bind.Jsonb) AutoChunkProcessor(org.talend.sdk.component.runtime.di.AutoChunkProcessor) JsonObject(javax.json.JsonObject) OutputFactory(org.talend.sdk.component.runtime.output.OutputFactory) InputsHandler(org.talend.sdk.component.runtime.di.InputsHandler)

Aggregations

InputFactory (org.talend.sdk.component.runtime.output.InputFactory)3 OutputFactory (org.talend.sdk.component.runtime.output.OutputFactory)3 JsonObject (javax.json.JsonObject)2 DoFn (org.apache.beam.sdk.transforms.DoFn)2 Processor (org.talend.sdk.component.runtime.output.Processor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvalidObjectException (java.io.InvalidObjectException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectStreamException (java.io.ObjectStreamException)1 OutputStream (java.io.OutputStream)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 List (java.util.List)1