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