首页 > 解决方案 > PutSQL 失败时发送电子邮件

问题描述

我想在PutSQL失败时发送电子邮件,PutSQL's Rollback on Failure=true,因为源代码不支持PutSQL失败时发送电子邮件,所以我添加了REL_ALERT关系。我添加了三行代码以在PutSQL失败时发送电子邮件,但它们都不起作用。

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

标签: apache-nifi

解决方案


您可以将流文件路由到多个关系(有关详细信息,请参阅Apache NiFi 开发人员指南——基于内容的路由(一对多))。我不确定您是说在失败时回滚为true,还是您没有意识到可以将其更改为false

如果您需要回滚失败并接收电子邮件警报,您有几个选择:

  • 修改PutSQL源代码以克隆流文件并将其发送到上述alert关系。您需要使用类似于RouteOnContent(见下文)的代码。
  • 注册一个电子邮件日志追加器,在$NIFI_HOME/conf/logback.xml其中获取WARNERROR级别日志事件org.apache.nifi.processors.standard.PutSQL

将流文件路由到多个关系的示例代码

final Relationship firstRelationship = destinations.iterator().next();
destinations.remove(firstRelationship);

for (final Relationship relationship : destinations) {
    FlowFile clone = session.clone(flowFile);
    clone = session.putAttribute(clone, ROUTE_ATTRIBUTE_KEY, relationship.getName());
    session.getProvenanceReporter().route(clone, relationship);
    session.transfer(clone, relationship);
    logger.info("Cloning {} to {} and routing clone to {}", new Object[]{flowFile, clone, relationship});
}

flowFile = session.putAttribute(flowFile, ROUTE_ATTRIBUTE_KEY, firstRelationship.getName());
session.getProvenanceReporter().route(flowFile, firstRelationship);
session.transfer(flowFile, firstRelationship);
logger.info("Routing {} to {}", new Object[]{flowFile, firstRelationship});

推荐阅读