【问题标题】:NiFi: Converting a Nested JSON File into CSVNiFi:将嵌套的 JSON 文件转换为 CSV
【发布时间】:2019-05-07 21:34:08
【问题描述】:

我是 Nifi 的新手,我必须使用来自 Kafka 的 JSON 主题 并想将其转换为 CSV 文件,我只需要选择几个标量和一些嵌套字段。

我需要做以下事情:

 Consume topic - Done
 Json to CSV
 Include header in the CSV file
 Merge into single file (if its split)
 Give a proper filename with date

点击以下链接: https://community.hortonworks.com/articles/64069/converting-a-large-json-file-into-csv.html

但不确定这是否是正确的方法,也不知道如何制作单个文件。 我正在使用NiFi (1.8) and schema is stored in Confluent Schema Registry

Json 示例:

{
  "type" : "record",
  "name" : "Customer",
  "namespace" : "namespace1"
  "fields" : [ {
    "name" : "header",
    "type" : {
      "type" : "record",
      "name" : "CustomerDetails",
      "namespace" : "namespace1"
      "fields" : [ {
        "name" : "Id",
        "type" : "string"
      }, {
        "name" : "name",
        "type" : "string"
      }, {
        "name" : "age",
        "type" : [ "null", "int" ],
        "default" : null
      }, {
        "name" : "comm",
        "type" : [ "null", "int" ],
        "default" : null
      } ]
    },
    "doc" : ""
  }, {
    "name" : "data",
    "type" : {
      "type" : "record",
      "name" : "CustomerData"
      "fields" : [ {
        "name" : "tags",
        "type" : {
          "type" : "map",
          "values" : "string"
        }
      }, {
        "name" : "data",
        "type" : [ "null", "bytes" ]
        "default" : null
      } ]
    },
    "doc" : ""
  } ]
}

请指导我。

【问题讨论】:

  • JSON 是什么样的,您打算如何将嵌套字段表示为 CSV 值?
  • 我已经更新了示例 json。嵌套字段应作为 csv 文件中的单个记录进入。

标签: apache-kafka apache-nifi


【解决方案1】:

尝试使用带有 JsonTreeReader 和 CSVRecordSetWriter 的 ConvertRecord。可以将编写器配置为包含标题行,您无需拆分/合并。 UpdateAttribute 可用于设置 filename 属性,该属性是流文件的关联文件名(例如,由 PutFile 使用)。

如果 ConvertRecord 没有给你想要的输出,你能详细说明它给你的和你期望的之间的区别吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-30
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 2020-10-28
    • 2017-05-02
    相关资源
    最近更新 更多