use of javax.json.JsonBuilderFactory in project nifi by apache.
the class SiteToSiteBulletinReportingTask method onTrigger.
@Override
public void onTrigger(final ReportingContext context) {
final boolean isClustered = context.isClustered();
final String nodeId = context.getClusterNodeIdentifier();
if (nodeId == null && isClustered) {
getLogger().debug("This instance of NiFi is configured for clustering, but the Cluster Node Identifier is not yet available. " + "Will wait for Node Identifier to be established.");
return;
}
if (lastSentBulletinId < 0) {
Map<String, String> state;
try {
state = context.getStateManager().getState(Scope.LOCAL).toMap();
} catch (IOException e) {
getLogger().error("Failed to get state at start up due to:" + e.getMessage(), e);
return;
}
if (state.containsKey(LAST_EVENT_ID_KEY)) {
lastSentBulletinId = Long.parseLong(state.get(LAST_EVENT_ID_KEY));
}
}
final BulletinQuery bulletinQuery = new BulletinQuery.Builder().after(lastSentBulletinId).build();
final List<Bulletin> bulletins = context.getBulletinRepository().findBulletins(bulletinQuery);
if (bulletins == null || bulletins.isEmpty()) {
getLogger().debug("No events to send because no events are stored in the repository.");
return;
}
final OptionalLong opMaxId = bulletins.stream().mapToLong(t -> t.getId()).max();
final Long currMaxId = opMaxId.isPresent() ? opMaxId.getAsLong() : -1;
if (currMaxId < lastSentBulletinId) {
getLogger().warn("Current bulletin max id is {} which is less than what was stored in state as the last queried event, which was {}. " + "This means the bulletins repository restarted its ids. Restarting querying from the beginning.", new Object[] { currMaxId, lastSentBulletinId });
lastSentBulletinId = -1;
}
if (currMaxId == lastSentBulletinId) {
getLogger().debug("No events to send due to the current max id being equal to the last id that was sent.");
return;
}
final String platform = context.getProperty(PLATFORM).evaluateAttributeExpressions().getValue();
final Map<String, ?> config = Collections.emptyMap();
final JsonBuilderFactory factory = Json.createBuilderFactory(config);
final JsonObjectBuilder builder = factory.createObjectBuilder();
final DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMAT);
df.setTimeZone(TimeZone.getTimeZone("Z"));
final long start = System.nanoTime();
// Create a JSON array of all the events in the current batch
final JsonArrayBuilder arrayBuilder = factory.createArrayBuilder();
for (final Bulletin bulletin : bulletins) {
if (bulletin.getId() > lastSentBulletinId) {
arrayBuilder.add(serialize(factory, builder, bulletin, df, platform, nodeId));
}
}
final JsonArray jsonArray = arrayBuilder.build();
// Send the JSON document for the current batch
try {
final Transaction transaction = getClient().createTransaction(TransferDirection.SEND);
if (transaction == null) {
getLogger().info("All destination nodes are penalized; will attempt to send data later");
return;
}
final Map<String, String> attributes = new HashMap<>();
final String transactionId = UUID.randomUUID().toString();
attributes.put("reporting.task.transaction.id", transactionId);
attributes.put("reporting.task.name", getName());
attributes.put("reporting.task.uuid", getIdentifier());
attributes.put("reporting.task.type", this.getClass().getSimpleName());
attributes.put("mime.type", "application/json");
final byte[] data = jsonArray.toString().getBytes(StandardCharsets.UTF_8);
transaction.send(data, attributes);
transaction.confirm();
transaction.complete();
final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
getLogger().info("Successfully sent {} Bulletins to destination in {} ms; Transaction ID = {}; First Event ID = {}", new Object[] { bulletins.size(), transferMillis, transactionId, bulletins.get(0).getId() });
} catch (final IOException e) {
throw new ProcessException("Failed to send Bulletins to destination due to IOException:" + e.getMessage(), e);
}
// Store the id of the last event so we know where we left off
try {
context.getStateManager().setState(Collections.singletonMap(LAST_EVENT_ID_KEY, String.valueOf(currMaxId)), Scope.LOCAL);
} catch (final IOException ioe) {
getLogger().error("Failed to update state to {} due to {}; this could result in events being re-sent after a restart.", new Object[] { currMaxId, ioe.getMessage() }, ioe);
}
lastSentBulletinId = currMaxId;
}
use of javax.json.JsonBuilderFactory 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 javax.json.JsonBuilderFactory in project component-runtime by Talend.
the class AutoKVWrapperTest method test.
@Test
public void test() {
final ComponentManager instance = ComponentManager.instance();
final JsonBuilderFactory factory = instance.getJsonpBuilderFactory();
PAssert.that(buildBaseJsonPipeline(pipeline, factory).setCoder(JsonpJsonObjectCoder.of(null)).apply(AutoKVWrapper.of(null, JobImpl.LocalSequenceHolder.cleanAndGet(getClass().getName() + ".test"), "", ""))).satisfies(values -> {
final List<KV<String, JsonObject>> items = StreamSupport.stream(values.spliterator(), false).sorted(comparing(k -> k.getValue().getJsonArray("b1").getJsonObject(0).getString("foo"))).collect(toList());
assertEquals(2, items.size());
// ensure we got 2 ids
assertEquals(2, new HashSet<>(items).size());
assertEquals(asList("a", "b"), items.stream().map(k -> k.getValue().getJsonArray("b1").getJsonObject(0).getString("foo")).collect(toList()));
return null;
});
assertEquals(PipelineResult.State.DONE, pipeline.run().waitUntilFinish());
}
use of javax.json.JsonBuilderFactory in project component-runtime by Talend.
the class RecordBranchFilterTest method test.
@Test
public void test() {
final ComponentManager instance = ComponentManager.instance();
final JsonBuilderFactory factory = instance.getJsonpBuilderFactory();
PAssert.that(buildBaseJsonPipeline(pipeline, factory).apply(RecordBranchFilter.of(null, "b1"))).satisfies(values -> {
final List<JsonObject> items = StreamSupport.stream(values.spliterator(), false).collect(toList());
assertEquals(2, items.size());
items.forEach(item -> {
assertTrue(item.containsKey("b1"));
assertNotNull(item.getJsonArray("b1").getJsonObject(0).getString("foo"));
assertFalse(item.containsKey("b2"));
});
return null;
});
assertEquals(PipelineResult.State.DONE, pipeline.run().waitUntilFinish());
}
use of javax.json.JsonBuilderFactory in project component-runtime by Talend.
the class RecordBranchMapperTest method test.
@Test
public void test() {
final ComponentManager instance = ComponentManager.instance();
final JsonBuilderFactory factory = instance.getJsonpBuilderFactory();
PAssert.that(buildBaseJsonPipeline(pipeline, factory).apply(RecordBranchMapper.of(null, "b1", "other"))).satisfies(values -> {
final List<JsonObject> items = StreamSupport.stream(values.spliterator(), false).collect(toList());
assertEquals(2, items.size());
items.forEach(item -> {
assertTrue(item.containsKey("other"));
assertNotNull(item.getJsonArray("other").getJsonObject(0).getString("foo"));
assertFalse(item.containsKey("b1"));
assertTrue(item.containsKey("b2"));
assertNotNull(item.getJsonArray("b2").getJsonObject(0).getString("bar"));
});
return null;
});
assertEquals(PipelineResult.State.DONE, pipeline.run().waitUntilFinish());
}
Aggregations