Search in sources :

Example 6 with Processor

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

the class AdvancedProcessorImplTest method serialization.

@Test
void serialization() throws IOException, ClassNotFoundException {
    final Processor processor = new ProcessorImpl("Root", "Test", "Plugin", new SampleOutput());
    final Processor copy = Serializer.roundTrip(processor);
    assertNotSame(copy, processor);
    assertEquals("Root", copy.rootName());
    assertEquals("Test", copy.name());
    assertEquals("Plugin", copy.plugin());
}
Also used : Processor(org.talend.sdk.component.runtime.output.Processor) ProcessorImpl(org.talend.sdk.component.runtime.output.ProcessorImpl) Test(org.junit.jupiter.api.Test)

Example 7 with Processor

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

the class MycompProcessorTest method map.

@Test
@Ignore("You need to complete this test")
public void map() throws IOException {
    // Processor configuration
    // Setup your component configuration for the test here
    final MycompProcessorConfiguration configuration = new MycompProcessorConfiguration();
    // We create the component processor instance using the configuration filled above
    final Processor processor = COMPONENT_FACTORY.createProcessor(MycompProcessor.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());
    // Run the flow and get the outputs
    final SimpleComponentRule.Outputs outputs = COMPONENT_FACTORY.collect(processor, joinInputFactory);
    // TODO - Test Asserts
    // test of the output branches count of the component
    assertEquals(1, outputs.size());
    // Here you have all your processor output branches
    // You can fill in the expected data for every branch to test them
    final List<JsonObject> value___default__ = outputs.get(JsonObject.class, "__default__");
    assertEquals(asList(), value___default__);
}
Also used : Processor(org.talend.sdk.component.runtime.output.Processor) JoinInputFactory(org.talend.sdk.component.junit.JoinInputFactory) JsonObject(javax.json.JsonObject) SimpleComponentRule(org.talend.sdk.component.junit.SimpleComponentRule) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with Processor

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

the class TaCoKitGuessSchema method fromOutputEmitterPojo.

public void fromOutputEmitterPojo(final Processor processor, final String outBranchName) {
    Object o = processor;
    while (Delegated.class.isInstance(o)) {
        o = Delegated.class.cast(o).getDelegate();
    }
    final ClassLoader classLoader = o.getClass().getClassLoader();
    final Thread thread = Thread.currentThread();
    final ClassLoader old = thread.getContextClassLoader();
    thread.setContextClassLoader(classLoader);
    try {
        final Optional<java.lang.reflect.Type> type = Stream.of(o.getClass().getMethods()).filter(m -> m.isAnnotationPresent(ElementListener.class)).flatMap(m -> IntStream.range(0, m.getParameterCount()).filter(i -> m.getParameters()[i].isAnnotationPresent(Output.class) && outBranchName.equals(m.getParameters()[i].getAnnotation(Output.class).value())).mapToObj(i -> m.getGenericParameterTypes()[i]).filter(t -> ParameterizedType.class.isInstance(t) && ParameterizedType.class.cast(t).getRawType() == OutputEmitter.class && ParameterizedType.class.cast(t).getActualTypeArguments().length == 1).map(p -> ParameterizedType.class.cast(p).getActualTypeArguments()[0])).findFirst();
        if (type.isPresent() && Class.class.isInstance(type.get())) {
            final Class<?> clazz = Class.class.cast(type.get());
            if (clazz != JsonObject.class) {
                guessSchemaThroughResultClass(clazz);
            }
        }
    } finally {
        thread.setContextClassLoader(old);
    }
}
Also used : IntStream(java.util.stream.IntStream) HashMap(java.util.HashMap) Modifier.isStatic(java.lang.reflect.Modifier.isStatic) Type(org.talend.sdk.component.api.service.schema.Type) Schema(org.talend.sdk.component.api.service.schema.Schema) HashSet(java.util.HashSet) JsonValue(javax.json.JsonValue) Collectors.toMap(java.util.stream.Collectors.toMap) ElementListener(org.talend.sdk.component.api.processor.ElementListener) Map(java.util.Map) Input(org.talend.sdk.component.runtime.input.Input) JobStateAware(org.talend.sdk.component.runtime.di.JobStateAware) Output(org.talend.sdk.component.api.processor.Output) ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) PrintStream(java.io.PrintStream) JsonObject(javax.json.JsonObject) ContainerComponentRegistry(org.talend.sdk.component.runtime.manager.ContainerComponentRegistry) Collection(java.util.Collection) Set(java.util.Set) Delegated(org.talend.sdk.component.runtime.base.Delegated) Field(java.lang.reflect.Field) ChainedMapper(org.talend.sdk.component.runtime.manager.chain.ChainedMapper) Processor(org.talend.sdk.component.runtime.output.Processor) OutputEmitter(org.talend.sdk.component.api.processor.OutputEmitter) Mapper(org.talend.sdk.component.runtime.input.Mapper) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) ParameterizedType(java.lang.reflect.ParameterizedType) Optional(java.util.Optional) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) ServiceMeta(org.talend.sdk.component.runtime.manager.ServiceMeta) ParameterizedType(java.lang.reflect.ParameterizedType) Type(org.talend.sdk.component.api.service.schema.Type) ParameterizedType(java.lang.reflect.ParameterizedType) Output(org.talend.sdk.component.api.processor.Output) JsonObject(javax.json.JsonObject) ElementListener(org.talend.sdk.component.api.processor.ElementListener)

Example 9 with Processor

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

the class ExecutionResource method write.

/**
 * Sends records using a processor instance. Note that the processor should have only an input.
 * Behavior for other processors is undefined.
 * The input format is a JSON based format where each like is a json record - same as for the symmetric endpoint.
 *
 * @param family the component family.
 * @param component the component name.
 * @param chunkSize the bundle size (chunk size).
 */
@POST
@Deprecated
@Consumes("talend/stream")
@Produces(MediaType.APPLICATION_JSON)
@Path("write/{family}/{component}")
public void write(@Suspended final AsyncResponse response, @Context final Providers providers, @PathParam("family") final String family, @PathParam("component") final String component, @QueryParam("group-size") @DefaultValue("50") final long chunkSize, final InputStream stream) {
    response.setTimeoutHandler(asyncResponse -> log.warn("Timeout on dataset retrieval"));
    response.setTimeout(appConfiguration.datasetRetrieverTimeout(), SECONDS);
    executorService.submit(() -> {
        Processor processor = null;
        final WriteStatistics statistics = new WriteStatistics(0);
        try (final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
            String line = reader.readLine();
            if (line == null || line.trim().isEmpty()) {
                response.resume(new WebApplicationException(Response.status(BAD_REQUEST).entity(new ErrorPayload(ACTION_ERROR, "No configuration sent")).build()));
                return;
            }
            final JsonObject configuration = inlineStreamingMapper.fromJson(line, JsonObject.class);
            final Map<String, String> config = convertConfig(configuration);
            final Optional<Processor> processorOptional = manager.findProcessor(family, component, getConfigComponentVersion(config), config);
            if (!processorOptional.isPresent()) {
                response.resume(new WebApplicationException(Response.status(BAD_REQUEST).entity(new ErrorPayload(COMPONENT_MISSING, "Didn't find the output component")).build()));
                return;
            }
            processor = processorOptional.get();
            processor.start();
            int groupCount = 0;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (!line.isEmpty()) {
                    final JsonObject object = inlineStreamingMapper.fromJson(line, JsonObject.class);
                    if (groupCount == 0) {
                        processor.beforeGroup();
                    }
                    groupCount++;
                    processor.onNext(name -> {
                        if (!Branches.DEFAULT_BRANCH.equals(name)) {
                            throw new IllegalArgumentException("Can't access branch '" + name + "' from component " + family + "#" + name);
                        }
                        return inlineStreamingMapper.fromJson(inlineStreamingMapper.toJson(object), JsonObject.class);
                    }, mockOutputFactory);
                    statistics.setCount(statistics.getCount() + 1);
                    if (groupCount == chunkSize) {
                        processor.afterGroup(mockOutputFactory);
                    }
                }
            }
        } catch (final Exception e) {
            response.resume(new WebApplicationException(Response.status(BAD_REQUEST).entity(new ErrorPayload(ACTION_ERROR, "Didn't find the input component")).build()));
        } finally {
            ofNullable(processor).ifPresent(Processor::stop);
        }
        response.resume(statistics);
    });
}
Also used : Processor(org.talend.sdk.component.runtime.output.Processor) InputStreamReader(java.io.InputStreamReader) WebApplicationException(javax.ws.rs.WebApplicationException) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) WebApplicationException(javax.ws.rs.WebApplicationException) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) WriteStatistics(org.talend.sdk.component.server.front.model.execution.WriteStatistics) BufferedReader(java.io.BufferedReader) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 10 with Processor

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

the class SimpleComponentRuleTest method processorCollector.

@Test
public void processorCollector() {
    final Processor processor = COMPONENT_FACTORY.createProcessor(Transform.class, null);
    final SimpleComponentRule.Outputs outputs = COMPONENT_FACTORY.collect(processor, new JoinInputFactory().withInput("__default__", asList(new Transform.Record("a"), new Transform.Record("bb"))).withInput("second", asList(new Transform.Record("1"), new Transform.Record("2"))));
    assertEquals(2, outputs.size());
    assertEquals(asList(2, 3), outputs.get(Integer.class, "size"));
    assertEquals(asList("a1", "bb2"), outputs.get(String.class, "value"));
}
Also used : Processor(org.talend.sdk.component.runtime.output.Processor) Transform(org.talend.sdk.component.junit.component.Transform) 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