Search in sources :

Example 1 with WriteStatistics

use of org.talend.sdk.component.server.front.model.execution.WriteStatistics in project component-runtime by Talend.

the class ExecutionResourceTest method write.

@Test
void write(final TestInfo info) throws IOException {
    final File outputFile = new File(jarLocation(ExecutionResourceTest.class).getParentFile(), getClass().getSimpleName() + "_" + info.getTestMethod().get().getName() + ".output");
    final JsonBuilderFactory objectFactory = Json.createBuilderFactory(emptyMap());
    final WriteStatistics stats = base.path("execution/write/{family}/{component}").resolveTemplate("family", "file").resolveTemplate("component", "output").request(APPLICATION_JSON_TYPE).post(entity(objectFactory.createObjectBuilder().add("file", outputFile.getAbsolutePath()).build() + "\n" + objectFactory.createObjectBuilder().add("line1", "v1").build() + "\n" + objectFactory.createObjectBuilder().add("line2", "v2").build(), "talend/stream"), WriteStatistics.class);
    assertTrue(outputFile.isFile());
    assertEquals(2, stats.getCount());
    assertEquals("{\"line1\":\"v1\"}\n{\"line2\":\"v2\"}", Files.readAllLines(outputFile.toPath()).stream().collect(joining("\n")));
}
Also used : JsonBuilderFactory(javax.json.JsonBuilderFactory) WriteStatistics(org.talend.sdk.component.server.front.model.execution.WriteStatistics) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with WriteStatistics

use of org.talend.sdk.component.server.front.model.execution.WriteStatistics 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)

Aggregations

WriteStatistics (org.talend.sdk.component.server.front.model.execution.WriteStatistics)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 JsonBuilderFactory (javax.json.JsonBuilderFactory)1 JsonObject (javax.json.JsonObject)1 JsonString (javax.json.JsonString)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Test (org.junit.jupiter.api.Test)1 Processor (org.talend.sdk.component.runtime.output.Processor)1 ErrorPayload (org.talend.sdk.component.server.front.model.error.ErrorPayload)1