Apache nifi 如何在nifi中为消息添加时间戳?
免责声明:我对nifi一无所知 我需要从Apache nifi 如何在nifi中为消息添加时间戳?,apache-nifi,Apache Nifi,免责声明:我对nifi一无所知 我需要从ListenHTTP处理器接收消息,然后将每条消息转换为带时间戳的json消息 所以,假设我在早上5点收到消息hello world。它应该将其转换为{“timestamp”:“5am”,“message”:“helloworld”} 如何做到这一点?因此,我添加了一个带有以下代码的ExecuteScript处理器: import org.apache.commons.io.IOUtils import java.nio.charset.StandardC
ListenHTTP
处理器接收消息,然后将每条消息转换为带时间戳的json消息
所以,假设我在早上5点收到消息hello world
。它应该将其转换为{“timestamp”:“5am”,“message”:“helloworld”}
如何做到这一点?因此,我添加了一个带有以下代码的
ExecuteScript
处理器:
import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
import java.time.LocalDateTime
flowFile = session.get()
if(!flowFile)return
def text = ''
// Cast a closure with an inputStream parameter to InputStreamCallback
session.read(flowFile, {inputStream ->
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
// Do something with text here
} as InputStreamCallback)
def outputMessage = '{\"timestamp\":\"' + LocalDateTime.now().toString() + '\", \"message:\":\"' + text + '\"}'
flowFile = session.write(flowFile, {inputStream, outputStream ->
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
outputStream.write(outputMessage.getBytes(StandardCharsets.UTF_8))
} as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)
它成功了。因此,我添加了一个带有以下代码的
ExecuteScript
处理器:
import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
import java.time.LocalDateTime
flowFile = session.get()
if(!flowFile)return
def text = ''
// Cast a closure with an inputStream parameter to InputStreamCallback
session.read(flowFile, {inputStream ->
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
// Do something with text here
} as InputStreamCallback)
def outputMessage = '{\"timestamp\":\"' + LocalDateTime.now().toString() + '\", \"message:\":\"' + text + '\"}'
flowFile = session.write(flowFile, {inputStream, outputStream ->
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
outputStream.write(outputMessage.getBytes(StandardCharsets.UTF_8))
} as StreamCallback)
session.transfer(flowFile, REL_SUCCESS)
每个流文件都有属性,这些属性是存储在内存中的键/值对中的元数据片段(可用于快速读/写)。当任何操作发生时,NiFi框架都会将元数据写入与流文件相关的源事件,有时还会写入流文件本身。例如,如果ListenHTTP
是流中的第一个处理器,则任何进入流的流文件都将具有一个属性entryDate
,其格式为Thu Jan 24 15:53:52 PST 2019
。您可以使用各种处理器读取和写入这些属性(即UpdateAttribute
,RouteOnAttribute
等)
对于您的用例,您可以在ListenHTTP
处理器之后立即使用ReplaceText
处理器,搜索值为(?s)(^.*$)
(整个流文件内容,或“通过HTTP调用接收的内容”),替换值为{“timestamp_now”:“${now():format('YYYY-MM-dd HH:MM:ss.SSS Z')”,“时间戳”:“${entryDate:format('YYYY-MM-dd HH:MM:ss.SSS Z')”,“消息”:“$1”}
上述示例提供了两个选项:
entryDate
是流文件通过ListenHTTP
处理器产生的时间now()
函数获取自历元以来的当前时间戳(以毫秒为单位)format()
方法和普通方法对它们进行格式化,因此您可以使用ha
获得“5am”(完整示例:now():format('ha'):toLower()
)
示例
在端口ListenHTTP
上运行,路径9999
contentListener
如上所述ReplaceText
与日志负载LogAttribute
true
Curl-d“helloworld”-X POSThttp://localhost:9999/contentListener
示例输出:
2019-01-24 16:04:44,529 INFO [Timer-Driven Process Thread-6] o.a.n.processors.standard.LogAttribute LogAttribute[id=8246b0a0-0168-1000-7254-2c2e43d136a7] logging for flow file StandardFlowFileRecord[uuid=5e1c6d12-298d-4d9c-9fcb-108c208580fa,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1548374015429-1, container=default, section=1], offset=3424, length=122],offset=0,name=5e1c6d12-298d-4d9c-9fcb-108c208580fa,size=122]
--------------------------------------------------
Standard FlowFile Attributes
Key: 'entryDate'
Value: 'Thu Jan 24 16:04:44 PST 2019'
Key: 'lineageStartDate'
Value: 'Thu Jan 24 16:04:44 PST 2019'
Key: 'fileSize'
Value: '122'
FlowFile Attribute Map Content
Key: 'filename'
Value: '5e1c6d12-298d-4d9c-9fcb-108c208580fa'
Key: 'path'
Value: './'
Key: 'restlistener.remote.source.host'
Value: '127.0.0.1'
Key: 'restlistener.remote.user.dn'
Value: 'none'
Key: 'restlistener.request.uri'
Value: '/contentListener'
Key: 'uuid'
Value: '5e1c6d12-298d-4d9c-9fcb-108c208580fa'
--------------------------------------------------
{"timestamp_now":"2019-01-24 16:04:44.518 -0800", "timestamp_ed": "2019-01-24 16:04:44.516 -0800", "message":"helloworld"}
每个流文件都有属性,这些属性是存储在内存中的键/值对中的元数据片段(可用于快速读/写)。当任何操作发生时,NiFi框架会将元数据写入与流文件相关的源事件,有时还会写入流文件本身。例如,如果ListenHTTP
是流中的第一个处理器,则进入流的任何流文件都将具有值为的属性entryDate
它的起始时间为2019年1月24日星期四15:53:52太平洋标准时间。您可以使用各种处理器读取和写入这些属性(即UpdateAttribute
,RouteOnAttribute
等)
对于您的用例,您可以在ListenHTTP
处理器之后立即使用ReplaceText
处理器,搜索值为(?s)(^.*$)
(整个流文件内容,或“通过HTTP调用接收的内容”),替换值为{“timestamp_now”:“${now():format('YYYY-MM-dd HH:MM:ss.SSS Z'),”时间戳:“${entryDate:format('YYYY-MM-dd HH:MM:ss.SSS Z')”,“消息:“$1”}
上述示例提供了两个选项:
entryDate
是流文件通过ListenHTTP
处理器产生的时间now()
函数获取自历元以来的当前时间戳(以毫秒为单位)format()
方法和普通方法对它们进行格式化,因此您可以使用ha
获得“5am”(完整示例:now():format('ha'):toLower()
)
示例
在端口ListenHTTP
上运行,路径9999
contentListener
如上所述ReplaceText
与日志负载LogAttribute
true
Curl-d“helloworld”-X POSThttp://localhost:9999/contentListener
示例输出:
2019-01-24 16:04:44,529 INFO [Timer-Driven Process Thread-6] o.a.n.processors.standard.LogAttribute LogAttribute[id=8246b0a0-0168-1000-7254-2c2e43d136a7] logging for flow file StandardFlowFileRecord[uuid=5e1c6d12-298d-4d9c-9fcb-108c208580fa,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1548374015429-1, container=default, section=1], offset=3424, length=122],offset=0,name=5e1c6d12-298d-4d9c-9fcb-108c208580fa,size=122]
--------------------------------------------------
Standard FlowFile Attributes
Key: 'entryDate'
Value: 'Thu Jan 24 16:04:44 PST 2019'
Key: 'lineageStartDate'
Value: 'Thu Jan 24 16:04:44 PST 2019'
Key: 'fileSize'
Value: '122'
FlowFile Attribute Map Content
Key: 'filename'
Value: '5e1c6d12-298d-4d9c-9fcb-108c208580fa'
Key: 'path'
Value: './'
Key: 'restlistener.remote.source.host'
Value: '127.0.0.1'
Key: 'restlistener.remote.user.dn'
Value: 'none'
Key: 'restlistener.request.uri'
Value: '/contentListener'
Key: 'uuid'
Value: '5e1c6d12-298d-4d9c-9fcb-108c208580fa'
--------------------------------------------------
{"timestamp_now":"2019-01-24 16:04:44.518 -0800", "timestamp_ed": "2019-01-24 16:04:44.516 -0800", "message":"helloworld"}