使用NiFi将CSV转换为嵌套JSON

时间:2018-08-21 09:53:00

标签: apache-nifi

我想使用nifi从csv创建嵌套的json- CSV文件:

“ Foo”,12,“纽约”,“北大街”,“ 123213” “ Foo1”,12,“纽约”,“北大街”,“ 123213” “ Foo2”,12,“纽约”,“北大街”,“ 123213”

必需的Json:

{
    "studentName":"Foo",
    "Age":"12",
    "address__city":"newyork",
    "address":{
        "address__address1":"North avenue",
        "address__zipcode":"123213"
    }
}

我正在通过应用avro模式使用nifi 1.4 convertRecord处理器,但无法获取嵌套的json。

Avro模式:

{
  "type" : "record",
  "name" : "MyClass",
  "namespace" : "com.test.avro",
  "fields" : [ {
    "name" : "studentName",
    "type" : "string"
  }, {
    "name" : "Age",
    "type" : "string"
  }, {
    "name" : "address__city",
    "type" : "string"
  }, {
    "name" : "address",
    "type" : {
      "type" : "record",
      "name" : "address",
      "fields" : [ {
        "name" : "address__address1",
        "type" : "string"
      }, {
        "name" : "address__zipcode",
        "type" : "string"
      } ]
    }
  } ]
}

1 个答案:

答案 0 :(得分:0)

您将需要:

  1. 使用SplitRecord将流文件拆分为单个记录
  2. 使用ConvertRecord将CSV文件转换为平面JSON文件
  3. 使用JOLT转换处理器将您的平面JSON转换为所需格式的嵌套JSON对象。