本篇主要是介绍自定义处理器的开发方式及Nifi处理器开发的一些细节

Nifi-Processor自定义开发的流程

之前说过,大部分的数据处理,我们可以基于ExcuseGroovyScript处理器,编写Groovy脚本去完成(或者Jpython,Js脚本等对应的组件),只能说这是基于Nifi平台的使用层面,还不能算是对于Nifi的开发,真正基于Nifi开发,至少要掌握Nifi扩展自定义组件,即根据实际需求,进行内置处理器(Processor)的开发。当然Nifi本身官方是给出了扩展的口,如果我们下载Nifi源码,就能看到,在Nifi源码中有个  nifi-example-bundle 的代码包,就是Nifi提供的扩展方式,里面附了一个小样例,感兴趣的可以去看Nifi的源码。这里主要是对开发自定义处理器进行一个完整的经验总结。

总体来说,Nifi-Processor自定义开发的流程就是:基于Nifi规则下的编码—>代码打包为nar包——>发布使用。

基于Nifi规则下的编码

Nifi本身提供了扩展处理器的方法,并有一套完整的接口、类,只需要按着规则去实现接口、继承抽象类、覆盖方法,实现一个自己的Processor类

代码打包为nar包

Nifi内置Processor的存在形式就是nar包(Nifi自身定义的),必须将自定义的代码,按着一定的规则进行打包,最终你的Processor是以nar的形式嵌入Nifi中,其实下面的  “自定义Nifi-Processor项目的两种搭建方式” 主要就是maven项目两种不同的表现方式,根本目的是为了最终能生成nar包

发布使用

完成开发后,将打好的nar包,放到Nifi安装目录的lib下即可,lib目录本身存放了Nifi出厂自带的数据处理器nar包,如果想依样画葫芦的学习,可以随便摘取一个nar包,反编译一下看看它的写法。

自定义Nifi-Processor项目的两种搭建方式

nifi的项目主要是maven项目,我们必须按照maven规范进行开发Nifi的组件,这里经过踩坑,博主总结了两种Nifi处理器开发的方式:

  • 基于Nifi官方提供的bundle模式
  • 基于普通maven项目,通过pom文件的方式完成nar包的构建

不管哪种方式,自定义Nifi-Processor组件项目根本的步骤就是:

  1. resources目录下建立 META-INF.services目录,然创建文件 org.apache.nifi.processor.Processor
  2. org.apache.nifi.processor.Processor 文件内声明自己的Processor类的全路径
  3. 将项目配置成能够打包为nar包的结构

其实这种只是在  “将项目配置成能够打包为nar包的结构”  上有差异,本质上都是为了打成Nifi推出的nar程序包。总体上来讲,第一种更为规范,更符合项目工程化管控;第二种有点野路子的感觉,就是一个独立的maven项目,但是好理解、操作方便,建议新手可以从第二种方式入手。

基于官方给出的bundle模式的开发

这种方式是官方给出的,特点就是便于管理和控制开发规范,我们从Nifi给出的样例码说起;

下载Nifi源码后,可以看见 nifi-example-bundle 的代码包,其实这一代码包就是标准的自定义Processor开发的模板,自定义创建的时候,可以参考该样例,创建一个maven项目,进行开发。

它本身是基于maven模块化的构建,整体的目录结构是nifi-example-bundle作为一个整体项目,下属两个模块包,nifi-nifi-example-nar和nifi-nifi-example-processors,其中nifi-example-bundle作为一个根项目,nifi-nifi-example-nar里只有一个pom文件,负责将程序打包成nar包,nifi-nifi-example-processors才是真正的灵魂,自定义Processor代码的存放处。

nifi-example-bundle

整体项目nifi-example-bundle作为一个maven根项目,通过pom文件管控nifi-nifi-example-nar和nifi-nifi-example-processors,其pom文件结构为:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <parent>
  7. <groupId>org.apache.nifi</groupId>
  8. <artifactId>nifi-external</artifactId>
  9. <version>1.11.4</version>
  10. </parent>
  11.  
  12. <artifactId>nifi-example-bundle</artifactId>
  13. <packaging>pom</packaging>
  14.  
  15. <modules>
  16. <module>nifi-nifi-example-processors</module>
  17. <module>nifi-nifi-example-nar</module>
  18. </modules>
  19.  
  20. <dependencyManagement>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.apache.nifi</groupId>
  24. <artifactId>nifi-nifi-example-processors</artifactId>
  25. <version>1.13.2</version>
  26. </dependency>
  27. </dependencies>
  28. </dependencyManagement>
  29. </project>

nifi-nifi-example-processors

nifi-nifi-example-processors项目是核心的,也是我们主要写代码的地方,我们要写的自定义Processor即在这里完成,

1、在resources目录下建立 META-INF.services,然创建文件 org.apache.nifi.processor.Processor

  1. # Licensed to the Apache Software Foundation (ASF) under one or more
  2. # contributor license agreements. See the NOTICE file distributed with
  3. # this work for additional information regarding copyright ownership.
  4. # The ASF licenses this file to You under the Apache License, Version 2.0
  5. # (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. org.apache.nifi.processors.WriteResourceToStream

2、开发自己的Processor程序,建立一个Processor类,并且继承实现抽象类 AbstractProcessor,根据自己需要实现一些方法

  1. package org.apache.nifi.processors;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.nio.charset.Charset;
  7. import java.util.Collections;
  8. import java.util.HashSet;
  9. import java.util.Set;
  10. import org.apache.commons.io.IOUtils;
  11. import org.apache.nifi.annotation.documentation.CapabilityDescription;
  12. import org.apache.nifi.annotation.documentation.Tags;
  13. import org.apache.nifi.annotation.lifecycle.OnScheduled;
  14. import org.apache.nifi.flowfile.FlowFile;
  15. import org.apache.nifi.processor.AbstractProcessor;
  16. import org.apache.nifi.processor.ProcessContext;
  17. import org.apache.nifi.processor.ProcessSession;
  18. import org.apache.nifi.processor.ProcessorInitializationContext;
  19. import org.apache.nifi.processor.Relationship;
  20. import org.apache.nifi.processor.exception.ProcessException;
  21. import org.apache.nifi.processor.io.OutputStreamCallback;
  22.  
  23. @Tags({ "example", "resources" })
  24. @CapabilityDescription("This example processor loads a resource from the nar and writes it to the FlowFile content")
  25. public class WriteResourceToStream extends AbstractProcessor {
  26.  
  27. public static final Relationship REL_SUCCESS = new Relationship.Builder()
  28. .name("success")
  29. .description("files that were successfully processed").build();
  30. public static final Relationship REL_FAILURE = new Relationship.Builder()
  31. .name("failure")
  32. .description("files that were not successfully processed").build();
  33.  
  34. private Set<Relationship> relationships;
  35.  
  36. private String resourceData;
  37.  
  38. @Override
  39. protected void init(final ProcessorInitializationContext context) {
  40.  
  41. final Set<Relationship> relationships = new HashSet<Relationship>();
  42. relationships.add(REL_SUCCESS);
  43. relationships.add(REL_FAILURE);
  44. this.relationships = Collections.unmodifiableSet(relationships);
  45. final InputStream resourceStream = getClass()
  46. .getClassLoader().getResourceAsStream("file.txt");
  47. try {
  48. this.resourceData = IOUtils.toString(resourceStream, Charset.defaultCharset());
  49. } catch (IOException e) {
  50. throw new RuntimeException("Unable to load resources", e);
  51. } finally {
  52. IOUtils.closeQuietly(resourceStream);
  53. }
  54.  
  55. }
  56.  
  57. @Override
  58. public Set<Relationship> getRelationships() {
  59. return this.relationships;
  60. }
  61.  
  62. @OnScheduled
  63. public void onScheduled(final ProcessContext context) {
  64.  
  65. }
  66.  
  67. @Override
  68. public void onTrigger(final ProcessContext context,
  69. final ProcessSession session) throws ProcessException {
  70. FlowFile flowFile = session.get();
  71. if (flowFile == null) {
  72. return;
  73. }
  74.  
  75. try {
  76. flowFile = session.write(flowFile, new OutputStreamCallback() {
  77.  
  78. @Override
  79. public void process(OutputStream out) throws IOException {
  80. IOUtils.write(resourceData, out, Charset.defaultCharset());
  81.  
  82. }
  83. });
  84. session.transfer(flowFile, REL_SUCCESS);
  85. } catch (ProcessException ex) {
  86. getLogger().error("Unable to process", ex);
  87. session.transfer(flowFile, REL_FAILURE);
  88. }
  89. }
  90. }

  3、nifi-nifi-example-processors的pom文件样例:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4.  
  5. <parent>
  6. <groupId>org.apache.nifi</groupId>
  7. <artifactId>nifi-example-bundle</artifactId>
  8. <version>1.13.2</version>
  9. </parent>
  10.  
  11. <artifactId>nifi-nifi-example-processors</artifactId>
  12. <packaging>jar</packaging>
  13.  
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.apache.nifi</groupId>
  17. <artifactId>nifi-api</artifactId>
  18. <version>1.13.2</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.apache.nifi</groupId>
  22. <artifactId>nifi-processor-utils</artifactId>
  23. <version>1.13.2</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.apache.nifi</groupId>
  27. <artifactId>nifi-mock</artifactId>
  28. <version>1.13.2</version>
  29. <scope>test</scope>
  30. </dependency>
  31. <dependency>
  32. <groupId>commons-io</groupId>
  33. <artifactId>commons-io</artifactId>
  34. <version>2.6</version>
  35. </dependency>
  36. </dependencies>
  37. <build>
  38. <plugins>
  39. <plugin>
  40. <groupId>org.apache.rat</groupId>
  41. <artifactId>apache-rat-plugin</artifactId>
  42. <configuration>
  43. <excludes>
  44. <exclude>src/main/resources/file.txt</exclude>
  45. </excludes>
  46. </configuration>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

nifi-nifi-example-nar

这一maven只是负责把Processor的jar整合打包成nar包,只有一个pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <parent>
  7. <groupId>org.apache.nifi</groupId>
  8. <artifactId>nifi-example-bundle</artifactId>
  9. <version>1.13.2</version>
  10. </parent>
  11.  
  12. <artifactId>nifi-example-nar</artifactId>
  13. <packaging>nar</packaging>
  14. <properties>
  15. <maven.javadoc.skip>true</maven.javadoc.skip>
  16. <source.skip>true</source.skip>
  17. </properties>
  18.  
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.apache.nifi</groupId>
  22. <artifactId>nifi-nifi-example-processors</artifactId>
  23. </dependency>
  24. </dependencies>
  25.  
  26. </project>

完整的demo代码可以去github中查看 :https://github.com/GCC1566/Apache-Nifi-Processor/tree/master/nifi-JsonProcessor-bundle

独立的maven程序模式开发

这种方式很独立,即当作一个maven项目去建立,不需要考虑过多。

按着maven项目的构建方式,构建一个基本的maven项目:

修改pom.xml文件

这种方式看起来更容易,只是所有的难点都集中在了pom文件,具体的pom文件样例下面给出,可直接复制到自己的项目中,有额外需要的依赖,自行追加。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>net.gcc.nifi</groupId>
  8. <artifactId>JsonDistributeProcessor</artifactId>
  9. <version>1.0</version>
  10. <packaging>nar</packaging>
  11.  
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <maven.compiler.source>1.8</maven.compiler.source>
  15. <maven.compiler.target>1.8</maven.compiler.target>
  16. <nifi.version>1.13.2</nifi.version>
  17. </properties>
  18.  
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.apache.nifi</groupId>
  22. <artifactId>nifi-api</artifactId>
  23. <version>${nifi.version}</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.apache.nifi</groupId>
  27. <artifactId>nifi-utils</artifactId>
  28. <version>${nifi.version}</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.apache.nifi</groupId>
  32. <artifactId>nifi-processor-utils</artifactId>
  33. <version>${nifi.version}</version>
  34. </dependency>
  35.  
  36. <dependency>
  37. <groupId>org.apache.nifi</groupId>
  38. <artifactId>nifi-mock</artifactId>
  39. <version>${nifi.version}</version>
  40. <scope>test</scope>
  41. </dependency>
  42. <dependency>
  43. <groupId>junit</groupId>
  44. <artifactId>junit</artifactId>
  45. <version>4.13</version>
  46. <scope>test</scope>
  47. </dependency>
  48. </dependencies>
  49.  
  50. <build>
  51. <plugins>
  52. <plugin>
  53. <groupId>org.apache.nifi</groupId>
  54. <artifactId>nifi-nar-maven-plugin</artifactId>
  55. <version>1.3.1</version>
  56. <extensions>true</extensions>
  57. </plugin>
  58. <plugin>
  59. <groupId>org.apache.maven.plugins</groupId>
  60. <artifactId>maven-surefire-plugin</artifactId>
  61. <version>2.18.1</version>
  62. </plugin>
  63. <plugin>
  64. <groupId>org.apache.maven.plugins</groupId>
  65. <artifactId>maven-resources-plugin</artifactId>
  66. <version>3.1.0</version>
  67. </plugin>
  68. <plugin>
  69. <groupId>org.apache.maven.plugins</groupId>
  70. <artifactId>maven-compiler-plugin</artifactId>
  71. <version>3.1</version>
  72. </plugin>
  73. <plugin>
  74. <groupId>org.apache.maven.plugins</groupId>
  75. <artifactId>maven-install-plugin</artifactId>
  76. <version>2.5.2</version>
  77. </plugin>
  78. <plugin>
  79. <groupId>org.apache.maven.plugins</groupId>
  80. <artifactId>maven-deploy-plugin</artifactId>
  81. <version>2.8.2</version>
  82. </plugin>
  83. </plugins>
  84. </build>
  85.  
  86. </project>

在项目里建立自己的java文件,实现 AbstractProcessor 类

前置都准备好了,就可以开始编写自定义的Processor,就随便建个类,并让他继承 AbstractProcessor,然后完成一些方法。

例如:

demo:

  1. package net.gcc.nifi.processors;
  2. import org.apache.nifi.annotation.behavior.SideEffectFree;
  3. import org.apache.nifi.annotation.documentation.CapabilityDescription;
  4. import org.apache.nifi.annotation.documentation.Tags;
  5. import org.apache.nifi.components.PropertyDescriptor;
  6. import org.apache.nifi.expression.ExpressionLanguageScope;
  7. import org.apache.nifi.flowfile.FlowFile;
  8. import org.apache.nifi.processor.*;
  9. import org.apache.nifi.processor.exception.ProcessException;
  10. import org.apache.nifi.processor.util.StandardValidators;
  11. import org.apache.nifi.util.StringUtils;
  12.  
  13. import java.util.*;
  14. import java.util.concurrent.ConcurrentHashMap;
  15.  
  16. /**
  17. * JSONDistributeProcessor
  18. * According to the rules, distribute the data to the specified Relationship
  19. * @author GCC
  20. */
  21.  
  22. @SideEffectFree
  23. @Tags({"JsonDataDistribute","net.gcc"})
  24. @CapabilityDescription("Divide data according to configuration")
  25. public class JsonDataDistributeProcessor extends AbstractProcessor{
  26.  
  27. public static final Relationship REL_SUCCESS = new Relationship.Builder()
  28. .name("success")
  29. .description("files that were successfully processed").build();
  30. public static final Relationship REL_FAILURE = new Relationship.Builder()
  31. .name("failure")
  32. .description("files that were not successfully processed").build();
  33.  
  34. private Set<Relationship> relationships;
  35.  
  36. @Override
  37. protected void init(final ProcessorInitializationContext context) {
  38.  
  39. final Set<Relationship> relationships = new HashSet<Relationship>();
  40. relationships.add(REL_SUCCESS);
  41. relationships.add(REL_FAILURE);
  42. this.relationships = Collections.unmodifiableSet(relationships);
  43.  
  44. }
  45.  
  46. @Override
  47. public Set<Relationship> getRelationships() {
  48. return this.relationships;
  49. }
  50.  
  51. @OnScheduled
  52. public void onScheduled(final ProcessContext context) {
  53.  
  54. }
  55.  
  56. @Override
  57. public void onTrigger(final ProcessContext context,
  58. //具体的数据处理
  59. }
  60. }

resources目录下建立 META-INF.services,然创建文件 org.apache.nifi.processor.Processor

类实现后,将实现类的全路径写在Nifi规定的配置文件种。

org.apache.nifi.processor.Processor文件的内容样例(可直接复制,只需要改动最后一行即可):

  1. # Licensed to the Apache Software Foundation (ASF) under one or more
  2. # contributor license agreements. See the NOTICE file distributed with
  3. # this work for additional information regarding copyright ownership.
  4. # The ASF licenses this file to You under the Apache License, Version 2.0
  5. # (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. net.gcc.nifi.processors.JsonDataDistributeProcessor

这里,最后一行为个人自定义Processor组件的类全路径,一个maven项目中可以创建多个Processor,只需要在这里追加。

完整的demo代码可以去github中查看 :https://github.com/GCC1566/JsonDistributeProcessor

关于Nifi-Processor开发的一些知识

FlowFile

FlowFile是一种逻辑概念,它将一段数据与一组关于该数据的属性相关联。这些属性包括FlowFile的唯一标识符,以及其名称,大小和任何数量的其他特定于流的值。虽然FlowFile的内容和属性可以更改,但FlowFile对象是不可变的。ProcessSession可以对FlowFile进行修改。

FlowFiles的核心属性在org.apache.nifi.flowfile.attributes.CoreAttributes枚举中定义。您将看到的最常见属性是filename,path和uuid。引号中的字符串是CoreAttributes枚举中属性的值。

  • Filename(“filename”):FlowFile的文件名。文件名不应包含任何目录结构。

  • UUID(“uuid”):分配给此FlowFile的通用唯一标识符,用于区分FlowFile与系统中的其他FlowFiles。

  • Path(“path”):FlowFile的路径指示FlowFile所属的相对目录,不包含文件名。

  • Absolute Path (“absolute.path”):FlowFile的绝对路径表示FlowFile所属的绝对目录,不包含文件名。

  • Priority(“priority”):表示FlowFile优先级的数值。

  • MIME Type(“mime.type”):此FlowFile的MIME类型。

  • Discard Reason(“discard.reason”):指定丢弃FlowFile的原因。

  • Alternative Identifier(“alternate.identifier”):表示已知引用此FlowFile的FlowFile的UUID以外的标识符。

ProcessSession

ProcessSession通常简称为“会话”,它提供了一种机制,通过该机制可以创建,销毁,检查,克隆FlowFiles并将其传输到其他处理器。此外,ProcessSession还提供了通过添加或删除属性或修改FlowFile内容来创建FlowFiles的修改版本的机制。ProcessSession还公开了一种用于发布源代码事件的机制,该机制提供了跟踪FlowFile的沿袭和历史的能力。在一个或多个FlowFiles上执行操作后,可以提交或回滚ProcessSession。

ProcessorInitializationContext

创建处理器后,initialize将使用InitializationContext对象调用其方法。此对象向处理器公开配置,该配置在处理器的整个生命周期内不会更改,例如处理器的唯一标识符。

ProcessContext

ProcessContext提供了处理器和框架之间的桥梁。它提供有关处理器当前如何配置的信息,并允许处理器执行特定于Framework的任务,例如产生其资源,以便框架将安排其他处理器运行而不会不必要地消耗资源。

PropertyDescriptor

PropertyDescriptor定义将由Processor,ReportingTask或ControllerService使用的属性。属性的定义包括其名称,属性的描述,可选的默认值,验证逻辑,以及关于处理器是否有效所需的属性的指示符。PropertyDescriptors是通过实例化PropertyDescriptor.Builder 类的实例,调用适当的方法来填充有关属性的详细信息,最后调用该build方法来创建的。

Validator(验证器)

PropertyDescriptor必须指定一个或多个Validator,可用于确保用户输入的属性值有效。如果Validator指示属性值无效,则在属性生效之前,将无法运行或使用Component。如果未指定Validator,则假定Component无效,NiFi将报告该属性不受支持。

Relationship

关系定义FlowFile可以从处理器传输到的路由。通过实例化Relationship.Builder 类的实例,调用适当的方法来填充关系的细节,最后调用 build方法来创建关系。

ComponentLog

鼓励处理器通过ComponentLog接口执行日志记录 ,而不是获取第三方记录器的直接实例。这是因为通过ComponentLog进行日志记录允许框架将超出可配置严重性级别的日志消息呈现给用户界面,从而允许在发生重要事件时通知监视数据流的人员。此外,它通过在DEBUG模式下记录堆栈跟踪并在日志消息中提供处理器的唯一标识符,为所有处理器提供一致的日志记录格式。

Processor的职责和一些基本概念

我们来看一个自定义Processor的继承结构:

图中,Tags,SideEffectFree、CapabilityDescription,是基于注解的,都是为了UI页面上的展示,这里我们忽略不看,主要看它的主线,整个自定义Processor继承自AbstractProcessor,而AbstractProcessor又继承了一系列。这里说明一下,虽然Processor是一个可以直接实现的接口,但这样做非常罕见,因为它org.apache.nifi.processor.AbstractProcessor是几乎所有处理器实现的基类。AbstractProcessor类提供的功能的显著,这使得开发的处理器更容易,更方便的任务。对于本文档的范围,我们将主要关注AbstractProcessor处理Processor API时的类。

处理器AbstractProcessor

处理器有很多方法,关乎着处理器的加载、运行、处理数据等等,这里只介绍几个最为重要的。

init()

该方法在处理器初始化的时候被调用,该方法采用单个参数,即类型ProcessorInitializationContext。上下文对象为Processor提供ComponentLog,Processor的唯一标识符和ControllerServiceLookup,可用于与配置的ControllerServices交互。每个这样的对象是由AbstractProcessor存储,并且可以由子类经由获得getLoggergetIdentifier和 getControllerServiceLookup方法。

getRelationships()

处理器通过覆盖该getRelationships方法来公开有效的关系集 。这个方法没有参数,并返回SetRelationship 对象。对于大多数处理器,此Set将是静态的,但其他处理器将根据用户配置动态生成Set。对于Set为静态的那些处理器,建议在Processor的构造函数或init方法中创建一个不可变的Set并返回该值,而不是动态生成Set。这种模式有助于实现更清晰的代码和更好的性能。

getSupportedPropertyDescriptors()

大多数处理器在能够使用之前需要一些用户配置。处理器支持的属性通过该getSupportedPropertyDescriptors方法向页面的组件公开 。这个方法没有参数,并返回List的 PropertyDescriptor对象。List中对象的顺序很重要,因为它决定了在用户界面中呈现属性的顺序。

PropertyDescriptor目的是通过创建一个新的实例构造PropertyDescriptor.Builder对象,调用构建器的适当的方法,并最终调用build方法。

虽然此方法涵盖了大多数用例,但有时需要允许用户配置名称未知的其他属性。这可以通过覆盖该getSupportedDynamicPropertyDescriptor方法来实现 。此方法将 String唯一参数作为参数,该参数指示属性的名称。该方法返回一个PropertyDescriptor对象,该 对象可用于验证属性的名称以及值。应该构建从此方法返回的任何PropertyDescriptor,isDynamicPropertyDescriptor.Builder类中将值设置为true 。AbstractProcessor的默认行为是不允许任何动态创建的属性。

然后处理器的属性,是需要验证的,如果处理器的配置无效,则无法启动处理器。可以通过在PropertyDescriptor上设置Validator或通过PropertyDescriptor.Builder的allowableValues方法或identifiesControllerService方法限制属性的允许值来验证Processor属性。

但是,有时候单独验证处理器的属性是不够的。为此,AbstractProcessor公开了一个customValidate方法。该方法采用单个参数类型ValidationContext。此方法的返回值是描述验证期间发现的任何问题Collection的 ValidationResult对象。只应返回其isValid方法返回的ValidationResult对象 false。仅当所有属性根据其关联的Validators和Allowable Values有效时,才会调用此方法。即,只有当所有属性本身都有效时才会调用此方法,并且此方法允许整体验证处理器的配置。

onPropertyModified()

这方法总的作用就是:响应配置更改,也就是当页面组件的配置标签页里配置发生变化,该方法就会被执行一次。当用户更改Processor的属性值时,onPropertyModified将为每个已修改的属性调用该 方法。该方法有三个参数:PropertyDescriptor,它指示修改了哪个属性,旧值和新值。如果属性没有先前的值,则第二个参数将是null。如果删除了属性,则第三个参数将是null。重要的是要注意,无论值是否有效,都将调用此方法。只有在实际修改了值时才会调用此方法,而不是在用户更新处理器而不更改其值时调用此方法。在调用此方法时,保证调用此方法的线程是当前在Processor中执行代码的唯一线程,除非Processor本身创建自己的线程。

onTrigger()

当处理器有工作要做时,它计划onTrigger通过框架调用其方法来完成。该方法有两个参数:a ProcessContext和aProcessSession。该onTrigger方法的第一步通常是通过调用getProcessSession上的一个方法来获取要在其上执行工作的FlowFile 。对于从外部源将数据提取到NiFi的处理器,将跳过此步骤。然后,处理器可以自由检查FlowFile属性; 添加,删除或修改属性; 读取或修改FlowFile内容; 并将FlowFiles传输到适当的关系。

处理器被触发时(When Processors are Triggered)

onTrigger只有在计划运行处理器并且处理器存在工作时,才会调用处理器的方法。如果满足以下任何条件,则称处理器存在工作:

    • 目标为Processor的Connection在其队列中至少有一个FlowFile

    • 处理器没有传入的连接

    • 处理器使用@TriggerWhenEmpty批注进行批注

有几个因素会导致onTrigger调用Processor的 方法。首先,除非用户已将处理器配置为运行,否则不会触发处理器。如果计划运行处理器,则周期性地(该周期由用户界面中的用户配置)检查处理器是否有工作,如上所述。如果是这样,框架将检查处理器的下游目的地。如果处理器的任何出站连接已满,则默认情况下,将不会安排处理器运行。

但是,@TriggerWhenAnyDestinationAvailable注释可以添加到Processor的类中。在这种情况下,需求被更改,以便只有一个下游目标必须“可用”(如果连接的队列未满,则目标被视为“可用”),而不是要求所有下游目标都可用。

与处理器调度有关的还有@TriggerSerially 注释。使用此Annotation的处理器永远不会有多个线程onTrigger同时运行该方法。但是,必须注意,执行代码的线程可能会从调用更改为调用。因此,仍然必须注意确保处理器是线程安全的!

自定义Nifi-Processor组件的单元测试

处理器或控制器服务的大多数单元测试都是通过创建TestRunner 类的实例来完成的。

一般需要额外引入maven依赖,由apache-nifi官方提供

  1. <dependency>
  2. <groupId>org.apache.nifi</groupId>
  3. <artifactId>nifi-mock</artifactId>
  4. <version>${nifi version}</version>
  5. </dependency>

TestRunner

TestRunner类是Nifi专门用来模拟实际环境下,运行Processor或者ControllerService的一个接口,它的实例可以通过  TestRunner.newTestRunner()来创建,newTestRunner方法的参数是你要运行的Proccessor的类;例如

  1. TestRunner run = TestRunner.newTestRunner(JSONDistributeProcessor.class);

添加ControllerServices

在启动一个处理器的时候,有些处理器可能需要额外需要ControllerService才能正常运行,这时候,可以通过模拟追加ControllerService来完成。

它的性质可以通过调用被设置:

setProperty(ControllerService, PropertyDescriptor, String)

setProperty(ControllerService, String, String)

setProperty(ControllerService, PropertyDescriptor, AllowableValue)

任何一个来完成。每种方法都返回一个 ValidationResult。然后可以检查此对象以确保通过调用该属性有效isValid。可以通过调用setAnnotationData(ControllerService, String)方法来设置注释数据。

assertValid(ControllerService) 方法来模拟验证ControllerService有效

assertNotValid(ControllerService) 来模拟验证ControllerService无效

将Controller Service添加到Test Runner并进行配置后,通过调用:

enableControllerService(ControllerService)方法启用它 。

如果Controller Service无效,则此方法将抛出IllegalStateException。

设置属性值

每个处理器可能需要具备一定的属性配置,也就是Web页面中每个Processor的配置页面

这里可以通过:

setProperty(PropertyDescriptor, String)方法进行追加配置

每个setProperty方法再次返回一个ValidationResult属性,可用于确保属性值有效。

预制FlowFiles测试数据

然后模拟前置管道涌入的数据,待测数据,可以通过TestRunner的enqueue方法来模拟批量的FlowFile数据,本身enqueue方法本身支持几种输入:byte[] ,InputStram、Path、Map

当然也可以自行实现enqueue方法,来满足自身的扩展需要

运行处理器

配置Controller Services并将必要的FlowFile排入队列后,可以通过调用run方法来触发处理器运行TestRunner。如果在没有任何参数的情况下调用此方法,它将使用@OnScheduled注释调用Processor中的任何方法,调用Processor的onTrigger方法一次,然后运行@OnUnscheduledfinally @OnStopped方法。

如果希望在触发onTrigger其他事件@OnUnscheduled和 @OnStopped生命周期事件之前运行该方法的多次迭代,则该run(int)方法可用于指定现在onTrigger应该调用的许多迭代。

还有,当我们想要触发处理器上运行,但不会触发时间@OnUnscheduled@OnStopped 生命周期事件。例如,这有助于在这些事件发生之前检查处理器的状态。这可以使用run(int, boolean)和传递false作为第二个参数来实现。但是,在执行此操作后,调用@OnScheduled生命周期方法可能会导致问题。因此,我们现在可以onTrigger再次运行,而不会通过使用方法的run(int,boolean,boolean)版本runfalse作为第三个参数传递来发生这些事件。

如果测试多个线程发生的行为很有用,这也可以通过调用setThreadCount方法来实现 TestRunner。默认值为1个线程。如果使用多个线程,请务必记住,run调用TestRunner指定应触发处理器的次数,而不是每个线程应触发处理器的次数。因此,如果线程计数设置为2但 run(1)被调用,则只使用一个线程。

验证输出Relationship

处理器运行完毕后,单元测试通常需要验证FlowFiles是否符合预期要求。通过以下两种方法来查看:

TestRunnersassertAllFlowFilesTransferred():此方法将关系和整数作为参数,以指示应该将多少FlowFiles传输到该关系。除非将此数量的FlowFiles转移到给定的关系或者任何FlowFile被转移到任何其他关系,否则该方法将无法通过单元测试。

assertTransferCount():仅验证FlowFile计数是给定关系的预期数量。

如果想要获取实际输出的数据样例,通过以下方法:

getFlowFilesForRelationship(): 获得实际的输出FlowFiles 。这个方法返回一个List<MockFlowFile>。重要的是要注意List的类型MockFlowFile,而不是FlowFile接口。这样做是因为MockFlowFile有许多方法可以验证内容。

一个实际例子

  1. public class JsonDataDistributeProcessorTest {
  2.  
  3. private TestRunner runner = TestRunners.newTestRunner(new JsonDataDistributeProcessor());
  4.  
  5. @Test
  6. public void runProcessor(){
  7. runner.setProperty("zhangsan","{\n" +
  8. " \"logic\":\"&&\",\n" +
  9. " \"fields\":[\n" +
  10. " {\n" +
  11. " \"field\":\"name\",\n" +
  12. " \"value\":[\"张三\"]\n" +
  13. " },\n" +
  14. " {\n" +
  15. " \"field\":\"age\",\n" +
  16. " \"value\":[12]\n" +
  17. " }\n" +
  18. " ]\n" +
  19. "}");
  20. runner.setProperty("lisi","{\n" +
  21. " \"logic\":\"&&\",\n" +
  22. " \"fields\":[\n" +
  23. " {\n" +
  24. " \"field\":\"name\",\n" +
  25. " \"value\":[\"张三\"]\n" +
  26. " }" +
  27. " ]\n" +
  28. "}");
  29. runner.assertValid();
  30. runner.enqueue("[{\"name\":\"张三\",\"age\":12},\n" +
  31. "{\"name\":\"王三\",\"age\":12},\n" +
  32. "{\"name\":\"张三\",\"age\":16},\n" +
  33. "{\"sdf\":\"wn\",\"age\":18}\n" +
  34. "]");
  35. runner.run();
  36. List<MockFlowFile> zhangsanlist = runner.getFlowFilesForRelationship("zhangsan");
  37. List<MockFlowFile> lisi = runner.getFlowFilesForRelationship("lisi");
  38.  
  39. for(MockFlowFile mk:zhangsanlist) {
  40. System.out.println(mk.toString());
  41. }
  42. for(MockFlowFile mk:lisi) {
  43. System.out.print(mk.toString());
  44. }
  45.  
  46. }
  47.  
  48. }

1、Nifi:基本认识

2、Nifi:基础用法及页面常识

3、Nifi:ExcuseXXXScript组件的使用(一)

4、Nifi:ExcuseXXXScript组件的使用(二)

5、Nifi:ExcuseXXXScript组件的使用(三)

6、Nifi:自定义处理器的开发

Nifi:nifi内置处理器Processor的开发的更多相关文章

  1. jsp九个内置对象、四个域对象及Servlet的三大域对象

    一,什么是内置对象? 在jsp开发中会频繁使用到一些对象,如ServletContext HttpSession PageContext等.如果每次我们在jsp页面中需要使用这些对象都要自己亲自动手创 ...

  2. java基础81 jsp的内置对象(网页知识)

    1.什么是内置对象? 在jsp开发中,会频繁使用到一些对象,如:HttpSession,ServletContext,HttpServletRequest.      如果每次使用这些对象时,都要去创 ...

  3. 详解JSP九个内置对象

    [JSP]☆★之详解九个内置对象       在web开发中,为方便开发者,JSP定义了一些由JSP容器实现和管理的内置对象,这些对象可以直接被开发者使用,而不需要再对其进行实例化!本文详解,JSP2 ...

  4. ASP.NET六大巨头——内置对象(2)

    前面讲了三个内置对象,后面来探究一下另外三个内置对象Session.Server和Cookie,Session对象就是服务器给客户端的一个编号:Server对象提供对服务器上的方法和属性的访问:coo ...

  5. 深刨显式锁ReentrantLock原理及其与内置锁的区别,以及读写锁ReentrantReadWriteLock使用场景

    13.显示锁 在Java5.0之前,在协调对共享对象的访问时可以使用的机制只有synchronized和volatile.Java5.0增加了一种新的机制:ReentrantLock.与之前提到过的机 ...

  6. Harp – 内置常用预处理器的静态 Web 服务器

    Harp 是一个基于 Node.js 平台的静态 Web 服务器,内置流行的预处理器,支持把 Jade, Markdown, EJS, Less, Stylus, Sass, and CoffeeSc ...

  7. 微信内置浏览器WebApp开发,踩坑 · Issue #31 · maxzhang/maxzhang.github.com · GitHub

    最近花6天时间完成了一个七夕的小活动,是一个简单的WebApp.由于我前期对面向微信的Web开发评估不足,导致开发过程十分艰难.写这篇文章总结下,惊醒自己未来不要再犯这样的错误. 问题: 1. 有些比 ...

  8. 【微网站开发】之微信内置浏览器API使用

    最近在写微网站,发现了微信内置浏览器的很多不称心的地方: 1.安卓版的微信内浏览器底部总是出现一个刷新.前进.后退的底部栏,宽度很大,导致屏幕显示尺寸被压缩 2.分享当前网站至朋友圈时,分享的图片一般 ...

  9. JSP内置标签 JSP中JavaBean标签 JSP开发模式 EL和JSTL快速入门

    2 JSP内置标签(美化+业务逻辑)   1)为了取代<%%>脚本形式,使用JSP标签/JSP动作,目的:与JSP页面的美化,即JSP面页都是由标签组成,不再有其它的内容   2)JSP内 ...

随机推荐

  1. 通过《第一行代码》学习 Android 开发

    第一行代码 Android --第 2 版-- 郭霖 著 第 1 章:开始启程--你的第一行 Android 代码 •1.2 手把手带你搭建开发环境  Android Studio 的安装及配置  A ...

  2. vue-i18n 国际化语言切换

    vue-i18n 用于前端vue项目中,需要多语言切换的场景 安装方法(npm) npm install vue-i18n 简单使用   1.在vue项目的main.ts文件中实例化 i18n imp ...

  3. 全网最详细的新手入门Mysql命令和基础,小白必看!

    MySQL简介 什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅仅是 ...

  4. shell字符串处理总结

    1. 字符串切片 1.1 基于偏移量取字符串 返回字符串 string 的长度 ${#string} 示例 [root@centos8 script]#str=" I Love Python ...

  5. 使用Vscode 开发调试 C/C++ 项目

    需要安装的扩展 C/C++ 如果是远程 Linux上开发还需要安装 Remote Development 创建工作目录后,代码远程克隆... 省略.. 创建项目配置文件,主要的作用是代码智能提示,错误 ...

  6. ATMS中去pause Activity A.

    上文写完之后,感觉这个部分写的还是不清晰,本文继续补充一下. 首先还是看堆栈. obtain:78, PauseActivityItem (android.app.servertransaction) ...

  7. 研发效率破局之道 Facebook工作法

    如果你问中国和美国互联网公司都有什么差别,很多人会回答:低效加班文化.最近爆出的996大讨论,通过糙快猛打拼和996加班去抢占市场获得机会的成功案例越来越少.至此,只有提高效能才是出路. 由于软件开发 ...

  8. 简单模拟实现javascript中的call、apply、bind方法

    目录 引子 隐式丢失 硬绑定 实现及原理分析 总体实现(纯净版/没有注释) 写在最后 引子 读完<你不知道的JavaScript--上卷>中关于this的介绍和深入的章节后,对于this的 ...

  9. GridSearchCV 参数

    GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, n_jobs=1, iid=True, refit=True, c ...

  10. Python容器数据类型之间的转换

    强制转换成字符串 str() 将列表(list)转为字符串 var1 = [1, 2, 3, 4] var2 = str(var1) print(type(var2)) # <class 'st ...