如何在ApacheNIFI中使用python打印?

2024-09-24 00:36:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我是ApacheNIFI新手,有兴趣在ExecuteScript处理器中使用打印,并使用Python作为脚本引擎

为了一步一步地调试脚本,我希望能够使用内置的Python打印函数。到目前为止,我找到的唯一方法是使用outputStream.write(),但是这个方法是有限的,因为您只能输出字符串

有人知道在ApacheNIFI中调试Python脚本的更好的解决方案吗

多谢各位


Tags: 方法字符串引擎脚本解决方案处理器内置write
1条回答
网友
1楼 · 发布于 2024-09-24 00:36:08

Python内置的print()函数打印到标准输出(即通常是控制台)。在NiFi,这对你不起作用。您应该使用框架注入到脚本中的log对象。此log对象具有多个函数,允许以不同的严重性级别(debuginfowarnerror等)进行输出。默认情况下,ExecuteScript处理器只在warn或更高级别写入消息,因此您可以使用log.warn("my message")或在conf/logback.xml中更改处理器的默认日志级别。输出将显示在logs/nifi-app.log文件中

示例脚本(将existing_attr设置为前一个处理器的当前时间):

flowFile = session.get()
if (flowFile != None):
    log.warn("Running inside the python script: " + flowFile.getAttribute("existing_attr"))
    flowFile = session.putAttribute(flowFile, "modified_from_script", "true")
    session.transfer(flowFile, REL_SUCCESS)

logs/nifi-app.log中输出(最后一行来自脚本内部):

2020-08-04 19:39:51,762 INFO [NiFi Web Server-119] o.a.n.controller.StandardProcessorNode Starting GenerateFlowFile[id=ba947905-0173-1000-d104-9e557d7a6e94]
2020-08-04 19:39:51,762 INFO [Timer-Driven Process Thread-7] o.a.n.c.s.TimerDrivenSchedulingAgent Scheduled LogAttribute[id=ba94af15-0173-1000-3f99-5d91aebbeee9] to run with 1 threads
2020-08-04 19:39:51,763 INFO [Timer-Driven Process Thread-4] o.a.n.c.s.TimerDrivenSchedulingAgent Scheduled GenerateFlowFile[id=ba947905-0173-1000-d104-9e557d7a6e94] to run with 1 threads
2020-08-04 19:39:52,135 INFO [Timer-Driven Process Thread-2] o.a.n.c.s.TimerDrivenSchedulingAgent Scheduled ExecuteScript[id=bc72b93f-0173-1000-0716-e5b91a545dc5] to run with 1 threads
2020-08-04 19:39:52,139 WARN [Timer-Driven Process Thread-3] o.a.nifi.processors.script.ExecuteScript ExecuteScript[id=bc72b93f-0173-1000-0716-e5b91a545dc5] Running inside the python script: 2020-08-04 19:39:51.763 -0700

More info about getting started with scripting in NiFi

相关问题 更多 >