NiFi 使用 ConvertRecord 将 json 转换为 csv [英] NiFi convert json to csv using ConvertRecord

查看:70
本文介绍了NiFi 使用 ConvertRecord 将 json 转换为 csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 apache nifi 中有一个包含动态字段(最多 11 个字段)的 json 流,我想将其转换为 csv 文件.

i have a stream of json in apache nifi that contain dynamic fields (maximum 11 fields) and i want to convert it to csv file.

示例 json:

{  
   "field1":"some text",
   "field2":"some text",
   "field3":"some text",
   "field4":"some text",
   "field5":"some text",
   "field6":"some text",
   "field7":"some text"
}

我不想使用替换或 json 评估;我如何使用 ConvertRecord 做到这一点?使用这个处理器很奇怪而且很难工作......

i don't wanna using replace or json evaluate; how i do it with ConvertRecord? using this processor is so odd and hard to work...

关于动态字段的清晰表达:我总共有 11 个字段.一条记录可能包含 7 个字段,下一条记录可能包含 11 个字段和接下来的 9 个字段...

Clear expression about dynamic fields: i have 11 fields at total. one record may have contain 7 fields, and next record may contain 11 fields and next 9 fields...

推荐答案

下面提供的步骤将帮助您完成这项工作.:

The steps provided below will help you in getting this done.:

  • 将生成/输出 JSON 文件的源处理器连接到 ConvertRecord.
  • 配置ConvertRecord并设置'Record Reader'使用JsonTreeReader控制器服务和'Record Writer'使用CSVRecordSetWriter控制器服务
  • 配置控制器服务并将Schema Registry 属性设置为使用AvroSchemaRegistry
  • 配置AvroSchemaRegistry.转到属性"选项卡,然后点击 + 按钮,您可以添加动态属性.
  • 提供一些属性名称(例如:mySchema),并为值提供输入 JSON 所需的 Avro 架构.(您可以使用 InferAvroSchema 处理器为您的 JSON 生成 Avro 架构)
  • 同时配置 JsonTreeReaderCsvRecordSetWriter,并将架构名称"属性设置为上面提供的名称,在本例中为 mySchema.
  • 根据需要将 ConvertRecord 的关系连接到下游处理器.
  • Connect your source processor which generates/outputs the JSON files to ConvertRecord.
  • Configure ConvertRecord and set 'Record Reader' to use JsonTreeReader controller service and 'Record Writer' to use CSVRecordSetWriter controller service
  • Configure both the controller services and set Schema Registry property to use AvroSchemaRegistry
  • Configure AvroSchemaRegistry. Go to 'Properties' tab and click the + button which lets you add a dynamic property.
  • Give some property name (ex: mySchema) and for the value, give the Avro schema expected for your input JSON. (You can use InferAvroSchema processor to generate Avro schema for your JSON)
  • Configure both JsonTreeReader and CsvRecordSetWriter and set the 'Schema Name' property to the name provided above, in this case, mySchema.
  • Connect the relationships of ConvertRecord to downstream processors according to your need.

这篇关于NiFi 使用 ConvertRecord 将 json 转换为 csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆