简体   繁体   中英

Merge json flowfiles in NiFi using Executescript with Javascript

I am currently trying to merge two Json files - one that is nested and one that is flat:

"ampdata": [
                {
                    "nr": "303",
                    "code": "JGJGh4958GH",
                    "Anr": "AVAILABLE",
                    "ability": [ "" ],
                    "type": "wheeled",
                    "conns": [
                        {
                            "nr": "447",
                            "status": "",
                            "version": "3",
                            "format": "sckt",

                            "amp": "32",
                            "vol": "400",
                            "vpower": 22

                        }
                    ]
                }

    [ {
  "nr" : 91643421,
  "Anr" : "Real",
  "Title" : null,
  "Comp" : null,
  "Name" : "Smith",
  "CompanyName" : "WhiteC"
}]

My current Approach is:

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

  flowFile = session.write(flowFile,
    new StreamCallback(function(inputStream, outputStream) {
        var text = IOUtils.buffer(inputStream)
        var obj = JSON.parse(text)
        var neu = [];
        var neuesObjekt = {};
        for (var i = 0; i < obj.ampdata.length; i++) {
            var entry = obj.ampdata[i];
            if(obj.ampdata[i].nr != obj2.nr) {

                           obj2.nr = obj.ampdate[i].nr
                        }

        }



        outputStream.write(JSON.stringify(newObj, null, '\t').getBytes(StandardCharsets.UTF_8)) 
    }))
  flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json')
  session.transfer(flowFile, REL_SUCCESS)

How do I parse two flowfiles that are incoming at the same time? I do like to work with both at the same time as I have to compare them at several positions. I can not figure out how I can avoid overwriting the first flowfile.

I had another Approach with using the MergeConent-Processor, but the result was just the concatenation of the both Jsons in a way that was not a valid Json anymore. Anyway I do prefer the Javascript attempt more, I just need your help in figuring out, how to do it in a proper way.

i think you can use merge content with parameters:

  • merge format: binary
  • header: [
  • footer: ]
  • demarcator: ,

by this merge of two json files into one will produce a valid json (array).

then, if you need to reformat json - you still can use ExecuteScript processor...

and you don't need to implement join files logic.


PS: to get two files from input queue use this type of code:

var flowFiles = session.get(2);
if(!flowFiles)return;
if(flowFiles.size()!=2){
    session.transfer(flowFiles); //return files back to input queue
    return;
}
//we have exactly two files. let's process them...
var flowFile1 = flowFiles[0];
var flowFile2 = flowFiles[1];
//read each, parse, apply logic, write result
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM