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")));
}
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);
});
}
Aggregations