QueryRecord处理器在NiFi中执行聚合SQL功能

时间:2018-10-17 19:26:54

标签: mysql apache apache-nifi

我有followimg流程:

QueryDatabaseTable-> QueryRecord-> UpdateAttribute-> MergeContent-> PutelasticsearchHttp

这个想法是从数据库中获取记录并在字段上执行聚合功能。在我的数据库表中,我有以下4个字段:

DeptId  DepartmentName  Address  ExperienNo

1    DS    San Jose      4

2    GT    San Fran      6

3    At    Oakland       8

4    BMS   detroit       3

5    RT    Haawai        9

并且我已经将QueryREcord的控制器服务设置为Avroreader和CSVSetWritter,并具有以下模式:

{
"type": "record",
"name": "SQLSchema",
"fields" : [
{"name": "DeptId", "type": "int"},
{"name": "DepartmentName", "type": "string"},
{"name": "Address", "type": "string"},
{"name": "ExperienceNo", "type": "int"},
{"name": "Total_Experience", "type": "int"}
]
}

我想在两个字段(DeptId-ExperienceNo)上进行区别

我的SQL查询如下:

SELECT DeptId, DepartmentName,Address,ExperienceNo,
(DeptId - ExperienceNo) AS Total_Experience FROM flowfile

我得到error as 'Total_Experience' cannot be null

但是,相同的查询在MySQL中运行良好。如何实现这一点,在这里我可以对字段和别名执行SQL聚合功能,并将其作为新的 dynamic 列。

谢谢。

任何建议都值得赞赏。

1 个答案:

答案 0 :(得分:2)

您的 csv阅读器控制器服务已配置

{"name": "Total_Experience", "type": "int"}字段没有默认值,并且在输入数据中没有得到该字段。

因此处理器正在抱怨 Total_Experience不能是null

要解决此问题,请更改 avro模式,以在 Total_Experience 字段中添加 null 值。

  

Avro模式:

{
"type": "record",
"name": "SQLSchema",
"fields" : [
{"name": "DeptId", "type": "int"},
{"name": "DepartmentName", "type": "string"},
{"name": "Address", "type": "string"},
{"name": "ExperienceNo", "type": "int"},
{"name": "Total_Experience", "type": ["null","int"]}
]
}

CsvReader控制器服务配置:

enter image description here

输出:

DeptId,DepartmentName,Address,ExperienceNo,Total_Experience
1,DS,San Jose,4,-3
2,GT,San Fran,6,-4
3,At,Oakland,8,-5
4,BMS,detroit,3,1
5,RT,Haawai,9,-4

正确的做法是:

配置 CSV阅读器而没有 Total_Experience 字段, 通过using Query record处理器创建此字段时,在 CsvSetWriter 控制器服务中包括 Total_Experience

相关问题