【问题标题】:How can I extract the name directories from an attribute in Apache Nifi?如何从 Apache Nifi 中的属性中提取名称目录?
【发布时间】:2020-10-08 01:41:06
【问题描述】:

如何在 Apache Nifi 中提取名称目录? 我想从流文件的“绝对路径”属性中提取名称,并将其保存为同一个流文件的属性。

例如:absolute.path 的值为“C:\Users\01_SEG\2019” 并将它们放入新属性中,例如:year= 2019, time=01_SEG

我尝试使用“UpdateAttribute”中的 replaceAll 函数从“absolute.path”中提取信息,但无法识别带有 () 的组。我不知道如何操作 NiFi 中正则表达式中的组和 NiFi 中的属性。 比如我想删除目录的最后两个名字,但是不行。

${'absolute.path':replaceAll('(\\[0-9]{2}_SEG)(\\([0-9]{4}\/)$)','')}

此表达式适用于任何 Java 正则表达式,但在这里不行。 如果我尝试类似:

time = ${"absolute.path":replaceFirst(".*\\(.*)\\\d{4}", "$1")}

有如下错误: Error

【问题讨论】:

  • 请出示您使用的代码。
  • 谢谢,知道代码可用。 @WiktorStribiżew

标签: regex apache-nifi


【解决方案1】:

UpdateAttribute 处理器允许任意新/现有属性通过其他属性的表达式语言从regular expression parsing 分配值。您的场景示例:

现有属性absolute.path = "C:\Users\01_SEG\2019"

添加动态属性:

  • 时间 = ${"absolute.path":replaceFirst(".+\\\\(\\w+)\\\\\\d{4}", "$1")}
  • = ${"absolute.path":replaceFirst(".*(\d{4})$", "$1")}

生成的流文件将具有以下属性:

  • absolute.path = "C:\Users\01_SEG\2019"
  • 时间 = "01_SEG"
  • 年份 = "2019"

表达式中属性absolute.path的额外引用是因为它包含.字符。

更新

由于 Windows 斜线的方向,斜线转义需要加倍。

$1 在正则表达式中引用“组 1”,因此我们提取组中包含的任何内容,然后替换整个匹配项(整个原始value) 仅包含组的内容。

如果您希望两个组的表达式相同,您可以指定一个包含两个组的表达式,并将一个属性替换为$1,另一个属性替换为$2

刚才在本地环境中运行的输出:

--------------------------------------------------
Standard FlowFile Attributes
Key: 'entryDate'
    Value: 'Wed Oct 07 10:37:39 PDT 2020'
Key: 'lineageStartDate'
    Value: 'Wed Oct 07 10:37:39 PDT 2020'
Key: 'fileSize'
    Value: '0'
FlowFile Attribute Map Content
Key: 'absolute.path'
    Value: 'C:\Users\01_SEG\2019'
Key: 'filename'
    Value: 'b16c325e-6d21-4d54-82f6-971c47152d63'
Key: 'path'
    Value: './'
Key: 'time'
    Value: '01_SEG'
Key: 'uuid'
    Value: 'b16c325e-6d21-4d54-82f6-971c47152d63'
Key: 'year'
    Value: '2019'
--------------------------------------------------

【讨论】:

  • 抱歉,第二个参数是什么? “1 美元”。我试了,但有错误。
  • 您知道如何在正则表达式中放入某个属性的值吗?另外,希望使用“$”来处理行尾。我尝试使用它,但 nifi 给我一个错误
  • 谢谢! @Andy我使用Windows,我忘记了。还有一个问题,如果我想匹配正则表达式中的属性值,只能使用表达式 '${nameAttribute}' ?
  • 是的,评估属性值的表达式语言术语应该出现在正则表达式应用程序之前,这样应该可以工作。
猜你喜欢
  • 2017-07-18
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2011-12-11
相关资源
最近更新 更多