使用Apache Nifi将Json数组转换为CSV

时间:2019-03-23 02:57:41

标签: arrays json apache-nifi jsonpath

我正在寻找将数组转换为CSV格式的JSON。每一行中数组内部元素的数量是动态的。我尝试使用此流程((将流程文件xml附加在帖子上)。

GetFile-> ConvertRecord-> UpdateAttribute-> PutFile

还有其他选择吗?

JSON格式:

{  "LogData": {
"Location": "APAC",
"product": "w1"  },  "Outcome": [
{
  "limit": "0",
  "pri": "3",
  "result": "pass"
},
{
  "limit": "1",
  "pri": "2",
  "result": "pass"
},
{
  "limit": "5",
  "priority": "1",
  "result": "fail"
}  ],  "attr": {
"vers": "1",
"datetime": "2018-01-10 00:36:00"  }}

csv中的预期输出:

location,   product,    limit,  pri,    result, vers,   datetime
APAC        w1          0       3       pass    1       2018-01-10 00:36:00
APAC        w1          1       2       pass    1       2018-01-10 00:36:00
APAC        w1          5       1       fail    1       2018-01-10 00:36:00

附件流的输出: LogData,结果,属性 “ MapRecord [{product = w1,Location = APAC}]”“,” [MapRecord [{limit = 0,result = pass,pri = 3}],MapRecord [{limit = 1,result = pass,pri = 2}] ,MapRecord [{limit = 5,result = fail}]]“”,“ MapRecord [{datetime = 2018-01-10 00:36:00,vers = 1}]”

enter image description here

ConvertRecord-我正在使用JSONTreereader和CSVRecordSSetwriter配置,如下所示: enter image description here

JSONTreereader Controler服务配置: enter image description here CSVRecordSetwriter控制器服务配置: enter image description here AvroschemaRegistry Controller服务配置: enter image description here

Avro模式:     {“ name”:“ myschema”,“ type”:“ record”,“ namespace”:“ myschema”,“ fields”:[{“ name”:“ LogData”,“ type”:{“ name”:“ LogData “,”类型“:”记录“,”字段“:[{”名称“:”位置“,”类型“:”字符串“},{”名称“:”产品“,”类型“:”字符串“} ]}},{“ name”:“结果”,“ type”:{“ type”:“ array”,“ items”:{“ name”:“ Outcome_record”,“ type”:“ record”,“ fields” :[{“ name”:“ limit”,“ type”:“ string”},{“ name”:“ pri”,“ type”:[“ string”,“ null”]},{“ name”:“ result“,” type“:” string“}]}}},{” name“:” attr“,” type“:{” name“:” attr“,” type“:” record“,” fields“: [{“ name”:“ vers”,“ type”:“ string”},{“ name”:“ datetime”,“ type”:“ string”}]}}]}

2 个答案:

答案 0 :(得分:1)

似乎您需要先执行JoltTransform,然后才能转换为CSV。

答案 1 :(得分:1)

在ConvertRecord之前在JoltTransformJSON中尝试此规范:

  {
    "operation": "shift",
    "spec": {
      "Outcome": {
        "*": {
          "@(3,LogData.Location)": "[#2].location",
          "@(3,LogData.product)": "[#2].product",
          "@(3,attr.vers)": "[#2].vers",
          "@(3,attr.datetime)": "[#2].datetime",
          "*": "[#2].&"
        }
      }
    }
  }
]```