use of javax.json.JsonBuilderFactory in project component-runtime by Talend.
the class RecordBranchUnwrapperTest 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());
}
use of javax.json.JsonBuilderFactory in project nifi by apache.
the class SiteToSiteProvenanceReportingTask 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;
}
final ProcessGroupStatus procGroupStatus = context.getEventAccess().getControllerStatus();
final String rootGroupName = procGroupStatus == null ? null : procGroupStatus.getName();
final String nifiUrl = context.getProperty(INSTANCE_URL).evaluateAttributeExpressions().getValue();
URL url;
try {
url = new URL(nifiUrl);
} catch (final MalformedURLException e1) {
// already validated
throw new AssertionError();
}
final String hostname = url.getHost();
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"));
consumer.consumeEvents(context, (mapHolder, events) -> {
final long start = System.nanoTime();
// Create a JSON array of all the events in the current batch
final JsonArrayBuilder arrayBuilder = factory.createArrayBuilder();
for (final ProvenanceEventRecord event : events) {
final String componentName = mapHolder.getComponentName(event.getComponentId());
final String processGroupId = mapHolder.getProcessGroupId(event.getComponentId(), event.getComponentType());
final String processGroupName = mapHolder.getComponentName(processGroupId);
arrayBuilder.add(serialize(factory, builder, event, df, componentName, processGroupId, processGroupName, hostname, url, rootGroupName, 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) {
// Throw an exception to avoid provenance event id will not proceed so that those can be consumed again.
throw new ProcessException("All destination nodes are penalized; will attempt to send data later");
}
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 {} Provenance Events to destination in {} ms; Transaction ID = {}; First Event ID = {}", new Object[] { events.size(), transferMillis, transactionId, events.get(0).getEventId() });
} catch (final IOException e) {
throw new ProcessException("Failed to send Provenance Events to destination due to IOException:" + e.getMessage(), e);
}
});
}
use of javax.json.JsonBuilderFactory in project nifi by apache.
the class SiteToSiteStatusReportingTask 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;
}
componentTypeFilter = Pattern.compile(context.getProperty(COMPONENT_TYPE_FILTER_REGEX).evaluateAttributeExpressions().getValue());
componentNameFilter = Pattern.compile(context.getProperty(COMPONENT_NAME_FILTER_REGEX).evaluateAttributeExpressions().getValue());
final ProcessGroupStatus procGroupStatus = context.getEventAccess().getControllerStatus();
final String rootGroupName = procGroupStatus == null ? null : procGroupStatus.getName();
final String nifiUrl = context.getProperty(INSTANCE_URL).evaluateAttributeExpressions().getValue();
URL url;
try {
url = new URL(nifiUrl);
} catch (final MalformedURLException e1) {
// already validated
throw new AssertionError();
}
final String hostname = url.getHost();
final String platform = context.getProperty(PLATFORM).evaluateAttributeExpressions().getValue();
final Map<String, ?> config = Collections.emptyMap();
final JsonBuilderFactory factory = Json.createBuilderFactory(config);
final DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMAT);
df.setTimeZone(TimeZone.getTimeZone("Z"));
final JsonArrayBuilder arrayBuilder = factory.createArrayBuilder();
serializeProcessGroupStatus(arrayBuilder, factory, procGroupStatus, df, hostname, rootGroupName, platform, null, new Date());
final JsonArray jsonArray = arrayBuilder.build();
final int batchSize = context.getProperty(BATCH_SIZE).asInteger();
int fromIndex = 0;
int toIndex = Math.min(batchSize, jsonArray.size());
List<JsonValue> jsonBatch = jsonArray.subList(fromIndex, toIndex);
while (!jsonBatch.isEmpty()) {
// Send the JSON document for the current batch
try {
long start = System.nanoTime();
final Transaction transaction = getClient().createTransaction(TransferDirection.SEND);
if (transaction == null) {
getLogger().debug("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");
JsonArrayBuilder jsonBatchArrayBuilder = factory.createArrayBuilder();
for (JsonValue jsonValue : jsonBatch) {
jsonBatchArrayBuilder.add(jsonValue);
}
final JsonArray jsonBatchArray = jsonBatchArrayBuilder.build();
final byte[] data = jsonBatchArray.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 {} Status Records to destination in {} ms; Transaction ID = {}", new Object[] { jsonArray.size(), transferMillis, transactionId });
fromIndex = toIndex;
toIndex = Math.min(fromIndex + batchSize, jsonArray.size());
jsonBatch = jsonArray.subList(fromIndex, toIndex);
} catch (final IOException e) {
throw new ProcessException("Failed to send Status Records to destination due to IOException:" + e.getMessage(), e);
}
}
}
use of javax.json.JsonBuilderFactory in project nifi by apache.
the class TestMetricsBuilder method testBuildMetricsObject.
@Test
public void testBuildMetricsObject() {
final Map<String, ?> config = Collections.emptyMap();
final JsonBuilderFactory factory = Json.createBuilderFactory(config);
final String instanceId = "1234-5678-1234-5678";
final String applicationId = "NIFI";
final String hostname = "localhost";
final long timestamp = System.currentTimeMillis();
final Map<String, String> metrics = new HashMap<>();
metrics.put("a", "1");
metrics.put("b", "2");
final MetricsBuilder metricsBuilder = new MetricsBuilder(factory);
final JsonObject metricsObject = metricsBuilder.applicationId(applicationId).instanceId(instanceId).hostname(hostname).timestamp(timestamp).addAllMetrics(metrics).build();
final JsonArray metricsArray = metricsObject.getJsonArray("metrics");
Assert.assertNotNull(metricsArray);
Assert.assertEquals(2, metricsArray.size());
JsonObject firstMetric = metricsArray.getJsonObject(0);
if (!"a".equals(firstMetric.getString(MetricFields.METRIC_NAME))) {
firstMetric = metricsArray.getJsonObject(1);
}
Assert.assertEquals("a", firstMetric.getString(MetricFields.METRIC_NAME));
Assert.assertEquals(applicationId, firstMetric.getString(MetricFields.APP_ID));
Assert.assertEquals(instanceId, firstMetric.getString(MetricFields.INSTANCE_ID));
Assert.assertEquals(hostname, firstMetric.getString(MetricFields.HOSTNAME));
Assert.assertEquals(String.valueOf(timestamp), firstMetric.getString(MetricFields.TIMESTAMP));
final JsonObject firstMetricValues = firstMetric.getJsonObject("metrics");
Assert.assertEquals("1", firstMetricValues.getString("" + timestamp));
}
Aggregations