统一错误处理脚本实现
2. 统一错误处理脚本实现
在错误处理组中,可以使用以下方法记录信息:
方法一:使用ExecuteScript处理器(Python示例)
python
复制
from java.lang import Exception
from org.apache.nifi.processor import Processor# 获取当前FlowFile和处理器信息
flowFile = session.get()
if flowFile is not None:try:# 获取原始处理器名称(需预先通过属性传递)processor_name = flowFile.getAttribute("original.processor.name")# 获取异常信息(可通过FlowFile属性或日志获取)error_msg = flowFile.getAttribute("error.message")# 记录到日志log.info(f"Processor {processor_name} failed with error: {error_msg}")# 可选:将错误信息写入数据库或文件# ...session.transfer(flowFile, REL_SUCCESS)except Exception as e:log.error("Error handling failed: " + str(e))session.transfer(flowFile, REL_FAILURE)