NIFI 自定义 processor 实现

本文介绍了如何在Apache NiFi中开发自定义Processor,从简介到开发、测试和部署的详细步骤。重点在于理解Processor的核心方法onTrigger,以及如何配置和测试自定义组件,以满足特定的业务需求。
摘要由CSDN通过智能技术生成

NIFI 简介

Apache NiFi 为数据流设计,支持高度可配置的指示图的数据路由、转换和系统中介逻辑,支持从多种数据源动态拉取数据,是一个易于使用、功能强大而且可靠的数据拉取、数据处理和分发系统。

如何实现自定义 processor ?

许多业务 仅仅使用 官方 提供的组件 不能够满足 性能上的需求,往往要通过高度可定制的组件来完成特定的业务需求。而 NiFi 提供了自定义组件的这种方式。大部分 NiFi 使用者 都是通过 NiFi 的 Processor 来实现自己的业务的。
https://github.com/apache/nifi/blob/master/nifi-api/src/main/java/org/apache/nifi/processor/AbstractProcessor.java

package org.apache.nifi.processor;

import org.apache.nifi.processor.exception.ProcessException;

public abstract class AbstractProcessor extends AbstractSessionFactoryProcessor {
    public AbstractProcessor() {
    }

    @Override
    public final void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
        final ProcessSession session = sessionFactory.createSession();
        try {
            onTrigger(context, session);
            session.commit();
        } catch (final Throwable t) {
            session.rollback(true);
            throw t;
        }
    }

    public abstract void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException;
}

任何自定义 processor 都必须 直接或者间接 继承 AbstractProcessor,整个 Processor 的核心部分 是 onTrigger 方法,onTrigger方法会在一个flow file被传入处理器时调用。

友情提示:关于 NiFi 的一些概念,可以查看如下链接:
https://blog.csdn.net/mearsedy/article/details/78178083
https://blog.csdn.net/hopeatme/article/details/50815448
http://nifi.apache.org/developer-guide.html

开发

https://github.com/aperepel/nifi-workshop
从上述链接 clone 代码,结构如下

tree nifi-workshop
nifi-workshop
├── Chuck_Norris_Speaking.xml
├── nifi-workshop-demo-nar
│   └── pom.xml
├── nifi-workshop-demo-processors
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── java
│       │   │   └── com
│       │   │       └── hortonworks
│       │   │           └── iot
│       │   │               └── MyProcessor.java
│       │   └── resources
│       │       └── META-INF
│       │           └── services
│       │               └── org.apache.nifi.processor.Processor
│       └── test
│           └── java
│               └── com
│                   └── hortonworks
│                       └── iot
│                           └── MyProcessorTest.java
├── pom.xml
├── README.md
└── screenshot.png

16 directories, 9 files

上述 pom 文件中的 NiFi 版本比较古老(0.3.0,Latest commit on 29 Sep 2015),虽然能运行通过,但是为了跟官方保持一致(1.7.0),需要更新相应的 pom 文件。

  • 更新后的 nifi-workshop 目录下的 pom 文件为
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements. See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ht
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值