2017-06-21 71 views
0

我想合并一个具有多个对象的JSON文件。以下是我的Oringinal JSON文件。在Apache NIFi中使用ContentMerge追加JSON文件需要帮助

{ 
"applicant": { 
     "full-name": "Tyrion Lannister", 
     "mobile-number" : "8435739739", 
     "email-id" : "[email protected]" 
    }, 
    "product": { 
     "product-category" : "Credit Card", 
     "product-type" : "Super Value Card - Titanium" 
    } 
} 

我将从其他来源得到更多的JSON数据。

{ 
    "flags": { 
     "duplicate-flag" : "No" 
     "contact-flag" : "Yes" 
     } 
} 

我的任务是在旧的JSON recods中添加新的JSON作为新对象,如下所示。

{ 
"applicant": { 
     "full-name": "Tyrion Lannister", 
     "mobile-number" : "8435739739", 
     "email-id" : "[email protected]" 
    }, 
    "product": { 
     "product-category" : "Credit Card", 
     "product-type" : "Super Value Card - Titanium" 
    }, 
    "flags": { 
     "duplicate-flag" : "No" 
     "contact-flag" : "Yes" 
     } 
} 

有人可以帮助指导,如何在NiFi中实现?

回答

4

我建议将您的组件作为flowfile属性累积,然后使用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); 
}