在 Apache NIFi 中使用 ContentMerge 附加 JSON 文件时需要帮助

Sat*_*raj 1 json apache-nifi

我正在尝试合并一个包含多个对象的 JSON 文件。下面是我的原始 JSON 文件。

 {
 "applicant": {
        "full-name": "Tyrion Lannister",
        "mobile-number" : "8435739739",
        "email-id" : "tyrionlannister_casterlyrock@gmail.com"
    },
    "product": {
        "product-category" : "Credit Card",
        "product-type" : "Super Value Card - Titanium"
    }
}
Run Code Online (Sandbox Code Playgroud)

我将从其他来源获取更多 JSON 数据,如下所示。

{
    "flags": {
        "duplicate-flag" : "No"
        "contact-flag" : "Yes"
         }
}
Run Code Online (Sandbox Code Playgroud)

我的任务是将新的 JSON 作为新对象附加到旧的 JSON 记录中,如下所示。

 {
 "applicant": {
        "full-name": "Tyrion Lannister",
        "mobile-number" : "8435739739",
        "email-id" : "tyrionlannister_casterlyrock@gmail.com"
    },
    "product": {
        "product-category" : "Credit Card",
        "product-type" : "Super Value Card - Titanium"
    },
    "flags": {
        "duplicate-flag" : "No"
        "contact-flag" : "Yes"
         }
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙指导一下,如何在 NiFi 中实现吗?

Jam*_*mes 5

我建议将组件累积为流文件属性,然后使用 JavaScript/ECMAScript 与ExecuteScript处理器形成合并对象。有时 JavaScript 是无可替代的。像下面这样的东西可能会起作用:

flowFile = session.get();
if (flowFile != null) {
    var OutputStreamCallback = Java.type("org.apache.nifi.processor.io.OutputStreamCallback");
    var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");

    // Get attributes
    var applicant = JSON.parse(flowFile.getAttribute("applicant"));
    var product = JSON.parse(flowFile.getAttribute("product"));
    var flags = JSON.parse(flowFile.getAttribute("flags"));

    // Combine
    var merged = {
        "applicant": applicant,
        "product": product,
        "flags": flags
    };

    // Write output content
    flowFile = session.write(flowFile, new OutputStreamCallback(function(outputStream) {
        outputStream.write(JSON.stringify(merged, null, "\t").getBytes(StandardCharsets.UTF_8));
    }));

    session.transfer(flowFile, REL_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)