Apache NiFi:JoltJSONTransform更新JSON列表

问题描述 投票:0回答:1

首先,感谢您的帮助。

我正在使用Apache NiFi,将一个FlowFile中的JSONS列表转换为几个具有一个JSON的FlowFiles。

然后,我正在使用JoltJSONTransform更新属性(文件名)上存在的json中缺少的密钥

我的问题是花了很多时间,因为我正在处理大文件。我的下一个任务是尝试将JSON列表中存在的每个元素中的关键字修改,然后再将其拆分为多个流文件。

我的数据类似于:

[
{ "number": "1",
  "pokemon":"Bulbasaur",
  "type":"plant"
},
{ "number": "4",
  "pokemon":"Charmander",
  "type":"fire"
},
{ "number": "7",
  "pokemon":"Squirtle",
  "type":"water"
}
]

并且我正在尝试添加key:value“ filename”:“ pokemon.csv”。列表中每个字典的键相同...

这是我的最佳尝试,我认为...best try

有人知道我该怎么做吗?

首先,我不知道在Nifi中使用脚本:(

json apache-nifi
1个回答
0
投票
首先,在分割大文件时,您可以使用一系列SplitText处理器对原始数据进行分块,例如每10.000行。然后,第二个SplitText处理器将这些块拆分为最终大小。这将减少堆的内存占用。

[第二,您可以将具有记录感知功能的处理器(如SplitRecord)及其关联的Writer / Reader(写入器/读取器)一起使用。这些都是为了避免多重拆分问题而开发的。

解决方案的确取决于您Json的大小。这是一个开始:


使用SplitRecord将Json列表分成多个块(例如1000)

  • 总流量:
  • enter image description here

    GenerateFlowFile:

    enter image description here

    SplitRecord:

    enter image description here

    创建一个JsonTreeReader和JsonRecordSetWriter,保留默认设置。配置分割大小。我将其设置为两个,因为我的列表包含三个对象,并且我想查看两个块。

    JoltTransformJSON

    enter image description here

    震动规格:

    [ { "operation": "modify-overwrite-beta", "spec": { "*": { "filename": "filename.csv" } } }, { "operation": "shift", "spec": { "*": { "*": "[&1].&" } } } ]

    MergeRecord

    enter image description here

    创建一个JsonTreeReader和JsonRecordSetWriter,保留默认设置。我将Minimum Number of Records设置为3,因为我希望合并所有记录。

    结果

    enter image description here

  • © www.soinside.com 2019 - 2024. All rights reserved.