问题描述
我在 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 架构) - 同时配置
JsonTreeReader
和CsvRecordSetWriter
,并将架构名称"属性设置为上面提供的名称,在本例中为mySchema
. - 根据需要将
ConvertRecord
的关系连接到下游处理器.
- Connect your source processor which generates/outputs the JSON files to
ConvertRecord
. - Configure
ConvertRecord
and set 'Record Reader' to useJsonTreeReader
controller service and 'Record Writer' to useCSVRecordSetWriter
controller service - Configure both the controller services and set
Schema Registry
property to useAvroSchemaRegistry
- 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 useInferAvroSchema
processor to generate Avro schema for your JSON) - Configure both
JsonTreeReader
andCsvRecordSetWriter
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!