简体   繁体   English

使用Executescript和Javascript合并NiFi中的json流文件

[英]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: 我目前正在尝试合并两个Json文件-一个嵌套的文件和一个平坦的文件:

"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. 我使用MergeConent-Processor时有另一种方法,但是结果只是两个Json的串联,这种方式不再是有效的Json。 Anyway I do prefer the Javascript attempt more, I just need your help in figuring out, how to do it in a proper way. 无论如何,我确实更喜欢Javascript尝试,我只需要您的帮助来确定如何以正确的方式进行操作。

i think you can use merge content with parameters: 我认为您可以使用带有参数的合并内容:

  • merge format: binary 合并格式: binary
  • header: [ 标头: [
  • footer: ] 页脚: ]
  • demarcator: , demarcator: ,

by this merge of two json files into one will produce a valid json (array). 通过将两个json文件合并为一个文件,将产生一个有效的json(数组)。

then, if you need to reformat json - you still can use ExecuteScript processor... 然后,如果您需要重新格式化json-您仍然可以使用ExecuteScript处理器...

and you don't need to implement join files logic. 而且您不需要实现联接文件逻辑。


PS: to get two files from input queue use this type of code: PS:要从输入队列中获取两个文件,请使用以下类型的代码:

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
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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