Search in sources :

Example 6 with Input

use of org.talend.sdk.component.runtime.input.Input in project component-runtime by Talend.

the class BeamComponentExtensionTest method assertMapper.

private void assertMapper(final Mapper mapper) {
    assertNotNull(mapper);
    mapper.start();
    final Input input = mapper.create();
    input.start();
    assertEquals(new Sample("a"), input.next());
    assertEquals(new Sample("b"), input.next());
    assertNull(input.next());
    input.stop();
    mapper.stop();
}
Also used : Input(org.talend.sdk.component.runtime.input.Input) Sample(org.talend.sdk.component.runtime.beam.data.Sample)

Example 7 with Input

use of org.talend.sdk.component.runtime.input.Input in project component-runtime by Talend.

the class ExecutionResource method read.

/**
 * Read inputs from an instance of mapper. The number of returned records if enforced to be limited to 1000.
 * The format is a JSON based format where each like is a json record.
 *
 * @param family the component family.
 * @param component the component name.
 * @param size the maximum number of records to read.
 * @param configuration the component configuration as key/values.
 */
@POST
@Deprecated
@Produces("talend/stream")
@Path("read/{family}/{component}")
public void read(@Suspended final AsyncResponse response, @Context final Providers providers, @PathParam("family") final String family, @PathParam("component") final String component, @QueryParam("size") @DefaultValue("50") final long size, final Map<String, String> configuration) {
    final long maxSize = Math.min(size, MAX_RECORDS);
    response.setTimeoutHandler(asyncResponse -> log.warn("Timeout on dataset retrieval"));
    response.setTimeout(appConfiguration.datasetRetrieverTimeout(), SECONDS);
    executorService.submit(() -> {
        final Optional<Mapper> mapperOptional = manager.findMapper(family, component, getConfigComponentVersion(configuration), configuration);
        if (!mapperOptional.isPresent()) {
            response.resume(new WebApplicationException(Response.status(BAD_REQUEST).entity(new ErrorPayload(COMPONENT_MISSING, "Didn't find the input component")).build()));
            return;
        }
        final Mapper mapper = mapperOptional.get();
        mapper.start();
        try {
            final Input input = mapper.create();
            try {
                input.start();
                response.resume((StreamingOutput) output -> {
                    Object data;
                    int current = 0;
                    while (current++ < maxSize && (data = input.next()) != null) {
                        if (CharSequence.class.isInstance(data) || Number.class.isInstance(data) || Boolean.class.isInstance(data)) {
                            final PrimitiveWrapper wrapper = new PrimitiveWrapper();
                            wrapper.setValue(data);
                            data = wrapper;
                        }
                        inlineStreamingMapper.toJson(data, output);
                        output.write(EOL);
                    }
                });
            } finally {
                input.stop();
            }
        } finally {
            mapper.stop();
        }
    });
}
Also used : PrimitiveWrapper(org.talend.sdk.component.server.front.model.execution.PrimitiveWrapper) Produces(javax.ws.rs.Produces) Path(javax.ws.rs.Path) BAD_FORMAT(org.talend.sdk.component.server.front.model.ErrorDictionary.BAD_FORMAT) PreDestroy(javax.annotation.PreDestroy) MediaType(javax.ws.rs.core.MediaType) JsonNumber(javax.json.JsonNumber) QueryParam(javax.ws.rs.QueryParam) Collectors.toMap(java.util.stream.Collectors.toMap) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) BAD_REQUEST(javax.ws.rs.core.Response.Status.BAD_REQUEST) JsonObject(javax.json.JsonObject) JsonbBuilder(javax.json.bind.JsonbBuilder) Context(javax.ws.rs.core.Context) Providers(javax.ws.rs.ext.Providers) AsyncResponse(javax.ws.rs.container.AsyncResponse) StreamingOutput(javax.ws.rs.core.StreamingOutput) Processor(org.talend.sdk.component.runtime.output.Processor) Suspended(javax.ws.rs.container.Suspended) StandardCharsets(java.nio.charset.StandardCharsets) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) OutputEmitter(org.talend.sdk.component.api.processor.OutputEmitter) Branches(org.talend.sdk.component.runtime.output.Branches) Slf4j(lombok.extern.slf4j.Slf4j) Response(javax.ws.rs.core.Response) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ACTION_ERROR(org.talend.sdk.component.server.front.model.ErrorDictionary.ACTION_ERROR) WriteStatistics(org.talend.sdk.component.server.front.model.execution.WriteStatistics) PathParam(javax.ws.rs.PathParam) OutputFactory(org.talend.sdk.component.runtime.output.OutputFactory) Inject(javax.inject.Inject) ComponentServerConfiguration(org.talend.sdk.component.server.configuration.ComponentServerConfiguration) Input(org.talend.sdk.component.runtime.input.Input) ExecutorService(java.util.concurrent.ExecutorService) POST(javax.ws.rs.POST) Optional.ofNullable(java.util.Optional.ofNullable) COMPONENT_MISSING(org.talend.sdk.component.server.front.model.ErrorDictionary.COMPONENT_MISSING) InputStreamReader(java.io.InputStreamReader) JsonString(javax.json.JsonString) Mapper(org.talend.sdk.component.runtime.input.Mapper) Jsonb(javax.json.bind.Jsonb) BufferedReader(java.io.BufferedReader) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) SECONDS(java.util.concurrent.TimeUnit.SECONDS) InputStream(java.io.InputStream) Mapper(org.talend.sdk.component.runtime.input.Mapper) PrimitiveWrapper(org.talend.sdk.component.server.front.model.execution.PrimitiveWrapper) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Input(org.talend.sdk.component.runtime.input.Input) WebApplicationException(javax.ws.rs.WebApplicationException) JsonNumber(javax.json.JsonNumber) JsonObject(javax.json.JsonObject) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 8 with Input

use of org.talend.sdk.component.runtime.input.Input in project component-runtime by Talend.

the class TaCoKitGuessSchema method guessInputComponentSchemaThroughResult.

private boolean guessInputComponentSchemaThroughResult() throws Exception {
    final Mapper mapper = componentManager.findMapper(family, componentName, 1, configuration).orElseThrow(() -> new IllegalArgumentException("Can't find " + family + "#" + componentName));
    if (JobStateAware.class.isInstance(mapper)) {
        JobStateAware.class.cast(mapper).setState(new JobStateAware.State());
    }
    Input input = null;
    try {
        mapper.start();
        final ChainedMapper chainedMapper = new ChainedMapper(mapper, mapper.split(mapper.assess()).iterator());
        chainedMapper.start();
        input = chainedMapper.create();
        input.start();
        Object rowObject = input.next();
        if (rowObject == null) {
            return false;
        }
        if (rowObject instanceof java.util.Map) {
            return guessInputSchemaThroughResults(input, (java.util.Map) rowObject);
        } else if (rowObject instanceof java.util.Collection) {
            throw new Exception("Can't guess schema from a Collection");
        } else {
            return guessSchemaThroughResultClass(rowObject.getClass());
        }
    } finally {
        if (input != null) {
            try {
                input.stop();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        try {
            mapper.stop();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
Also used : ChainedMapper(org.talend.sdk.component.runtime.manager.chain.ChainedMapper) Mapper(org.talend.sdk.component.runtime.input.Mapper) Input(org.talend.sdk.component.runtime.input.Input) JobStateAware(org.talend.sdk.component.runtime.di.JobStateAware) Collection(java.util.Collection) ChainedMapper(org.talend.sdk.component.runtime.manager.chain.ChainedMapper) JsonObject(javax.json.JsonObject) HashMap(java.util.HashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map)

Example 9 with Input

use of org.talend.sdk.component.runtime.input.Input in project component-runtime by Talend.

the class DIBatchSimulationTest method doClose.

private void doClose(final Map<String, Object> globalMap) {
    final Mapper mapperMapper = Mapper.class.cast(globalMap.remove("mapperMapper"));
    final Input inputMapper = Input.class.cast(globalMap.remove("inputMapper"));
    try {
        if (inputMapper != null) {
            inputMapper.stop();
        }
    } catch (final RuntimeException re) {
        fail(re.getMessage());
    } finally {
        try {
            if (mapperMapper != null) {
                mapperMapper.stop();
            }
        } catch (final RuntimeException re) {
            fail(re.getMessage());
        }
    }
    final AutoChunkProcessor processorProcessor = AutoChunkProcessor.class.cast(globalMap.remove("processorProcessor"));
    try {
        if (processorProcessor != null) {
            processorProcessor.stop();
        }
    } catch (final RuntimeException re) {
        fail(re.getMessage());
    }
}
Also used : 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) Input(org.talend.sdk.component.runtime.input.Input) AutoChunkProcessor(org.talend.sdk.component.runtime.di.AutoChunkProcessor)

Example 10 with Input

use of org.talend.sdk.component.runtime.input.Input in project component-runtime by Talend.

the class QueueMapperTest method execution.

@Test
void execution() {
    final QueueMapper mapper = new QueueMapper("test", "test", "test", null);
    try {
        mapper.setState(new JobStateAware.State());
    } catch (final NullPointerException npe) {
    // not sexy but for the test it is ok
    }
    final Input input = mapper.create();
    final LoopState lookup = LoopState.lookup(mapper.getStateId());
    lookup.push(Json.createObjectBuilder().add("id", 1).build());
    lookup.end();
    assertEquals(1, JsonObject.class.cast(input.next()).getInt("id"));
    assertNull(input.next());
}
Also used : LoopState(org.talend.sdk.component.runtime.di.beam.LoopState) Input(org.talend.sdk.component.runtime.input.Input) JobStateAware(org.talend.sdk.component.runtime.di.JobStateAware) Test(org.junit.jupiter.api.Test)

Aggregations

Input (org.talend.sdk.component.runtime.input.Input)11 Mapper (org.talend.sdk.component.runtime.input.Mapper)9 JsonObject (javax.json.JsonObject)4 Map (java.util.Map)3 Jsonb (javax.json.bind.Jsonb)3 Test (org.junit.Test)3 PartitionMapper (org.talend.sdk.component.api.input.PartitionMapper)3 ChainedMapper (org.talend.sdk.component.runtime.manager.chain.ChainedMapper)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Optional (java.util.Optional)2 ExecutorService (java.util.concurrent.ExecutorService)2 SECONDS (java.util.concurrent.TimeUnit.SECONDS)2 Collectors.toMap (java.util.stream.Collectors.toMap)2 Slf4j (lombok.extern.slf4j.Slf4j)2 PBegin (org.apache.beam.sdk.values.PBegin)2 Source (org.talend.sdk.component.junit.component.Source)2 Sample (org.talend.sdk.component.runtime.beam.data.Sample)2 BeamMapperImpl (org.talend.sdk.component.runtime.beam.impl.BeamMapperImpl)2 JobStateAware (org.talend.sdk.component.runtime.di.JobStateAware)2