Apache nifi Nifi从数据库添加属性

Apache nifi Nifi从数据库添加属性,apache-nifi,Apache Nifi,我目前正在从Nifi中的FTP获取文件,但在获取文件之前,我必须检查一些条件。情况是这样的 列出FTP->检查条件->获取FTP 在检查条件部分,我从数据库中获取了一些值,并与文件名进行了比较。所以,我可以使用update属性从数据库中获取一些记录,并使其成为这样吗 列出FTP->Update Attribute(从DB)->Route on Attribute->Fetch FTP我认为您的流如下所示 流量: ${filename:substringAfter('_'):toDate("yyy

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

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

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


列出FTP->Update Attribute(从DB)->Route on Attribute->Fetch FTP

我认为您的流如下所示

流量:

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

RouteOnAttribute配置:

${filename:substringAfter('_'):toDate("yyyy-MM-ddHH:mm:ss"):toNumber()
:gt(${db_time:toDate("yyyy-MM-ddHH:mm:ss"):toNumber()})}
我假设文件名类似于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()})}
通过使用上述表达式,我们获得了与ListFTP相同的
文件名值,并且通过使用
EvaluateJsonPath
处理器添加了
db_time
属性,我们将时间戳更改为数字,然后进行比较

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


因此,如果我正确理解您的用例,就好像您使用外部数据库只是为了跟踪目的。所以我想只有最新处理的时间戳就足够了。在这种情况下,我建议您使用NiFi提供的
DistributedCache
处理器和控制器服务,而不是依赖外部数据库

使用此方法,您的流程如下所示:

ListFile-->FetchDistributedMapCache--(成功)-->RouteOnAttribute->FetchFile

配置
FetchDistributedMapCache

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
  • 缓存条目标识符-这是缓存的密钥。将其设置为类似于
    lastProcessedTime
  • 将缓存值放在属性中-无论您在此处给出什么名称,都将添加为FlowFile属性,其值为缓存值。提供名称,如
    latestTimestamp
    lastProcessedTime
配置
RouteOnAttribute

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
单击
属性
选项卡中的(+)按钮,创建新的动态关系。给它起个名字,比如
success
matches
。让我们假设,您的文件名的格式为
somefile\u 1534824139
,即它有一个名称和一个
\u
以及附加的历元时间戳

在这种情况下,您可以利用NiFi表达式语言
,并利用它提供的功能。因此,对于新的动态关系,可以使用如下表达式:

  • 成功-
    ${filename:substringAfter('''u'):gt(${lastProcessedTimestamp})}
这是基于这样的假设:在
FetchDistributedMapCache
中,您已使用值
lastProcessedTimestamp
配置属性
Put Cache Value in Attribute

有用的链接


请详细说明从DB获取值需要什么?是否基于文件名,我们需要从数据库中提取记录?文件名包含时间戳,我们需要pic包含时间戳高于上次提取文件的文件。最后获取的文件的时间戳存储在DB中。因此,我需要从数据库中获取最后一个时间戳,并检查文件名是否包含高于该时间戳的时间戳,如果是,则获取文件,否则不要。这正是我想要的。这是一个更好的解决方案,但我们目前还没有使用任何分布式缓存服务。谢谢你的回答。我理解,但我建议你在有时间的时候做一些测试和基准测试(缓存服务vs数据库)。我相信缓存服务可以更好地执行,因为我们避免了数据库操作中涉及的额外开销。我接受缓存服务会给我更好的性能。我会试着用这个。谢谢