首页 > 解决方案 > Nifi从数据库添加属性

问题描述

我目前正在 Nifi 中从 FTP 获取文件,但在获取文件之前我必须检查一些条件。场景是这样的。

列出 FTP -> 检查条件 -> 获取 FTP

在检查条件部分,我从数据库中获取了一些值并与文件名进行比较。那么我可以使用更新属性从数据库中获取一些记录并使其成为这样吗?

列出 FTP -> 更新属性(来自 DB)-> 属性路由 -> 获取 FTP

标签: apache-nifi

解决方案


我认为您的流程如下所示

流动:

1.ListFTP //to list the files
2.ExecuteSQL //to execute query in db(sample query:select max(timestamp) db_time from table)
3.ConvertAvroToJson //convert the result of executesql to json format
4.EvaluateJsonPath //keep destination as FlowfileAttribute and add new property as db_time as $.db_time
5.ROuteOnAttribute //perform check filename timestamp vs extracted timestamp by using nifi expresson language
6.FetchFile //if condition is true then fetch the file

在此处输入图像描述

RouteOnAttribute 配置:

我假设文件名类似于fn_2017-08-2012:09:10并且 executesql 已返回2017-08-2012:08:10

表达:

${filename:substringAfter('_'):toDate("yyyy-MM-ddHH:mm:ss"):toNumber()
:gt(${db_time:toDate("yyyy-MM-ddHH:mm:ss"):toNumber()})}

通过使用上面的表达式,我们使用处理器添加了filename value same as ListFTP文件名和db_time属性EvaluateJsonPath,我们将时间戳更改为数字,然后进行比较。

有关 NiFi 表达语言的更多详细信息,请参阅此链接。

在此处输入图像描述


推荐阅读