Search in sources :

Example 1 with JsonBuilderFactory

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;
}
Also used : StandardValidators(org.apache.nifi.processor.util.StandardValidators) JsonArrayBuilder(javax.json.JsonArrayBuilder) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) JsonBuilderFactory(javax.json.JsonBuilderFactory) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) Scope(org.apache.nifi.components.state.Scope) SchedulingStrategy(org.apache.nifi.scheduling.SchedulingStrategy) Map(java.util.Map) Json(javax.json.Json) DateFormat(java.text.DateFormat) Restricted(org.apache.nifi.annotation.behavior.Restricted) JsonObject(javax.json.JsonObject) Restriction(org.apache.nifi.annotation.behavior.Restriction) TransferDirection(org.apache.nifi.remote.TransferDirection) RequiredPermission(org.apache.nifi.components.RequiredPermission) TimeZone(java.util.TimeZone) JsonArray(javax.json.JsonArray) IOException(java.io.IOException) UUID(java.util.UUID) Transaction(org.apache.nifi.remote.Transaction) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) Stateful(org.apache.nifi.annotation.behavior.Stateful) List(java.util.List) DefaultSchedule(org.apache.nifi.annotation.configuration.DefaultSchedule) Tags(org.apache.nifi.annotation.documentation.Tags) Collections(java.util.Collections) JsonObjectBuilder(javax.json.JsonObjectBuilder) HashMap(java.util.HashMap) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) IOException(java.io.IOException) JsonArray(javax.json.JsonArray) ProcessException(org.apache.nifi.processor.exception.ProcessException) Transaction(org.apache.nifi.remote.Transaction) JsonBuilderFactory(javax.json.JsonBuilderFactory) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) OptionalLong(java.util.OptionalLong) OptionalLong(java.util.OptionalLong) JsonObject(javax.json.JsonObject) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with JsonBuilderFactory

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")));
}
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 3 with JsonBuilderFactory

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());
}
Also used : JsonBuilderFactory(javax.json.JsonBuilderFactory) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) KV(org.apache.beam.sdk.values.KV) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with JsonBuilderFactory

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());
}
Also used : JsonBuilderFactory(javax.json.JsonBuilderFactory) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 5 with JsonBuilderFactory

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());
}
Also used : JsonBuilderFactory(javax.json.JsonBuilderFactory) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Aggregations

JsonBuilderFactory (javax.json.JsonBuilderFactory)10 JsonObject (javax.json.JsonObject)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 JsonArray (javax.json.JsonArray)4 Test (org.junit.Test)4 ComponentManager (org.talend.sdk.component.runtime.manager.ComponentManager)4 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 JsonArrayBuilder (javax.json.JsonArrayBuilder)3 JsonObjectBuilder (javax.json.JsonObjectBuilder)3 ProcessException (org.apache.nifi.processor.exception.ProcessException)3 Transaction (org.apache.nifi.remote.Transaction)3 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 StandardCharsets (java.nio.charset.StandardCharsets)2 UUID (java.util.UUID)2 JsonValue (javax.json.JsonValue)2 ProcessGroupStatus (org.apache.nifi.controller.status.ProcessGroupStatus)2