你们是不是要每个组提取到一个单独的属性?这对于“纯粹”的NiFi来说当然是可能的,但是对于这样长的线条,使用ExecuteScript
处理器可以更有意义地使用Groovy或Python的更复杂的正则表达式处理,并与String#split()
结合使用,并提供发布的脚本。
要执行使用ExtractText
这个任务,你会因为如下步骤进行设置:
可复制的模式:
group 1: (^\S+\s\S+\s\S+)
group 2: (?i)(?<=\s)([a-f0-9]{4})(?=\s)
group 3: (?i)(?<=\s)(PACKET\s[a-f0-9]{4,16})(?=\s)
group 4: (?i)(?<=\s\S{16}\s)([\w]{3,})(?=\s)
group 5: (?i)(?<=\s.{3}\s)([\w]{3,})(?=\s)
group 6: (?i)(?<=\s.{3}\s)([\d\.]{7,15})(?=\s)
group 7: (?i)(?<=\d\s)([a-f0-9]{4})(?=\s)
group 8: (?i)(?<=\d\s[a-f0-9]{4}\s)(.*)$
需要注意的是Include Capture Group 0
设置是非常重要的到false
。由于在NiFi中验证正则表达式的方式(目前所有正则表达式必须至少有一个捕获组 - 将使用NIFI-4095 | ExtractText should not require a capture group in every regular expression修复),您将得到重复组(group 1
和group 1.1
)。
产生的flowfile具有正确填充的属性:
完整的日志输出:
2017-06-20 14:45:57,050 INFO [Timer-Driven Process Thread-9] o.a.n.processors.standard.LogAttribute LogAttribute[id=c6b04310-015c-1000-b21e-c64aec5b035e] logging for flow file StandardFlowFileRecord[uuid=5209cc65-08fe-44a4-be96-9f9f58ed2490,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1497984255809-1, container=default, section=1], offset=444, length=148],offset=0,name=1920315756631364,size=148]
--------------------------------------------------
Standard FlowFile Attributes
Key: 'entryDate'
Value: 'Tue Jun 20 14:45:10 EDT 2017'
Key: 'lineageStartDate'
Value: 'Tue Jun 20 14:45:10 EDT 2017'
Key: 'fileSize'
Value: '148'
FlowFile Attribute Map Content
Key: 'filename'
Value: '1920315756631364'
Key: 'group 1'
Value: '6/19/2017 12:14:07 PM'
Key: 'group 1.1'
Value: '6/19/2017 12:14:07 PM'
Key: 'group 2'
Value: '0FA0'
Key: 'group 2.1'
Value: '0FA0'
Key: 'group 3'
Value: 'PACKET 0000000DF5EC3D80'
Key: 'group 3.1'
Value: 'PACKET 0000000DF5EC3D80'
Key: 'group 4'
Value: 'UDP'
Key: 'group 4.1'
Value: 'UDP'
Key: 'group 5'
Value: 'Snd'
Key: 'group 5.1'
Value: 'Snd'
Key: 'group 6'
Value: '11.222.333.44'
Key: 'group 6.1'
Value: '11.222.333.44'
Key: 'group 7'
Value: '93c8'
Key: 'group 7.1'
Value: '93c8'
Key: 'group 8'
Value: 'R Q [8085 A DR NOERROR] PTR (2)73(3)191(3)250(2)10(7)in-addr(4)arpa(0)'
Key: 'group 8.1'
Value: 'R Q [8085 A DR NOERROR] PTR (2)73(3)191(3)250(2)10(7)in-addr(4)arpa(0)'
Key: 'path'
Value: './'
Key: 'uuid'
Value: '5209cc65-08fe-44a4-be96-9f9f58ed2490'
--------------------------------------------------
6/19/2017 12:14:07 PM 0FA0 PACKET 0000000DF5EC3D80 UDP Snd 11.222.333.44 93c8 R Q [8085 A DR NOERROR] PTR (2)73(3)191(3)250(2)10(7)in-addr(4)arpa(0)
与NiFi 1.3.0的发布另一种选择是使用record processing capabilities。这是一项新功能,它允许任意输入格式(Avro,JSON,CSV等)以流方式进行分析和操作。 Mark Payne编写了一个very good tutorial here,介绍了该功能并提供了一些简单的演练。
您是否在寻找某件事[像这样](https://regex101.com/r/qFE95M/3)? – horcrux
我做了这样的事情,如果数据的格式不固定,只有空格是值得信任的:https://regex101.com/r/OikYiX/1只使用所需的组。如果以'AM'或'PM'结尾的日期是固定的,那么('''UDP','Snd'等等)中的char数量等等。你可以在@horcrux这样的正则表达式中更具体。 – Esteban
感谢你们的回复。 Horcrux建议的第一个表达式工作得很好。 Esteban建议的第二个表达式对于给定的字符串是有效的,但是如果在任何组中有任何长度变化,那么它会中断。 是的,格式不固定,所以我们只能信任空格分隔符。 非常感谢您的帮助。 – ankit