Java 我们可以和如何将ApacheNIFI流文件及其属性保存到磁盘,以便在定制处理器单元测试用例中使用/重新读取它们吗
我试图探索如何为ApacheNIFI编写单元测试用例,这样我就可以避免“更改代码、构建nar、将nar粘贴到lib文件夹中、重新启动Nifi”循环。然而,我想,为此,我还需要将流文件捕获到本地磁盘,并在每次运行单元测试用例时重新加载它们。我遇到了一篇文章,它要求将流文件序列化到磁盘,然后读取这些文件,并将它们排队到单元测试中的处理器,以将它们提供给我目前正在开发的自定义处理器。本文要求使用Java 我们可以和如何将ApacheNIFI流文件及其属性保存到磁盘,以便在定制处理器单元测试用例中使用/重新读取它们吗,java,apache-nifi,Java,Apache Nifi,我试图探索如何为ApacheNIFI编写单元测试用例,这样我就可以避免“更改代码、构建nar、将nar粘贴到lib文件夹中、重新启动Nifi”循环。然而,我想,为此,我还需要将流文件捕获到本地磁盘,并在每次运行单元测试用例时重新加载它们。我遇到了一篇文章,它要求将流文件序列化到磁盘,然后读取这些文件,并将它们排队到单元测试中的处理器,以将它们提供给我目前正在开发的自定义处理器。本文要求使用MergeContent和FlowFileV3选项,然后使用PutFile。我能够以.pkg格式保存这些文件
MergeContent
和FlowFileV3
选项,然后使用PutFile
。我能够以.pkg
格式保存这些文件。我使用GetFile
、indentifymetype
和UnpackContent
处理器,在同一篇文章中建议的单元测试代码中重新阅读它们。但是,我在代码中执行此操作,如下所示:
//Get File
TestRunner getFileRunner = TestRunners.newTestRunner(new GetFile());
getFileRunner.setProperty(GetFile.DIRECTORY, "C:\\Mahesh\\delete\\serialized-flow-file-2");
getFileRunner.setProperty(GetFile.KEEP_SOURCE_FILE, "true");
getFileRunner.run(1);
List<MockFlowFile> getFileResult = getFileRunner.getFlowFilesForRelationship(GetFile.REL_SUCCESS);
List<? extends FlowFile> getFileFFResult = getFileResult;
//IdentifyMimeType
TestRunner identifyMimeTypeRunner = TestRunners.newTestRunner(new IdentifyMimeType());
identifyMimeTypeRunner.enqueue(getFileFFResult.toArray(new FlowFile[getFileFFResult.size()]));
identifyMimeTypeRunner.run(1);
List<MockFlowFile> identifyMimeTypeResult = identifyMimeTypeRunner.getFlowFilesForRelationship(IdentifyMimeType.REL_SUCCESS);
List<? extends FlowFile> identifyMimeTypeFFResult = identifyMimeTypeResult;
//UnpackContent
TestRunner unpackContentRunner = TestRunners.newTestRunner(new UnpackContent());
unpackContentRunner.enqueue(identifyMimeTypeFFResult.toArray(new FlowFile[identifyMimeTypeFFResult.size()]));
unpackContentRunner.run(1);
List<MockFlowFile> unpackContentResult = unpackContentRunner.getFlowFilesForRelationship(UnpackContent.REL_SUCCESS);
List<? extends FlowFile> unpackContentFFResult = unpackContentResult;
我有一些疑问:
FlowFile已用于由ProcessSession.read创建的活动回调或InputStream(FlowFile)尚未关闭
MergeContent
和PutFile
保存这些流文件,然后使用GetFile
、identificationmimetype
和unpaccontent
读取它们的方法正确吗?我正在考虑将UnpackContent
的输出提供给我的定制处理器的TestRunner
?这些都对吗?或者他们的方法更可取/更标准,而我只是错过了e.printStackTrace()
,它打印了以下内容:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.AssertionError: java.lang.IllegalStateException: FlowFile[0,622261873281800.mockFlowFile,0B] already in use for an active callback or InputStream created by ProcessSession.read(FlowFile) has not been closed
at org.apache.nifi.util.StandardProcessorTestRunner.run(StandardProcessorTestRunner.java:201)
at org.apache.nifi.util.StandardProcessorTestRunner.run(StandardProcessorTestRunner.java:160)
at org.apache.nifi.util.StandardProcessorTestRunner.run(StandardProcessorTestRunner.java:155)
at org.apache.nifi.util.StandardProcessorTestRunner.run(StandardProcessorTestRunner.java:150)
at MyCustomProcessorTest.testOnTrigger(MyCustomProcessorTest.java:47)
... 23 more
Caused by: java.lang.IllegalStateException: FlowFile[0,622261873281800.mockFlowFile,0B] already in use for an active callback or InputStream created by ProcessSession.read(FlowFile) has not been closed
at org.apache.nifi.util.MockProcessSession.validateState(MockProcessSession.java:1014)
at org.apache.nifi.util.MockProcessSession.putAllAttributes(MockProcessSession.java:488)
at org.apache.nifi.util.MockProcessSession.inheritAttributes(MockProcessSession.java:1044)
at org.apache.nifi.util.MockProcessSession.create(MockProcessSession.java:299)
at org.apache.nifi.util.MockProcessSession.create(MockProcessSession.java:62)
at org.apache.nifi.processors.standard.UnpackContent$FlowFileStreamUnpacker$1.process(UnpackContent.java:415)
at org.apache.nifi.util.MockProcessSession.read(MockProcessSession.java:547)
at org.apache.nifi.util.MockProcessSession.read(MockProcessSession.java:529)
at org.apache.nifi.processors.standard.UnpackContent$FlowFileStreamUnpacker.unpack(UnpackContent.java:409)
at org.apache.nifi.processors.standard.UnpackContent.onTrigger(UnpackContent.java:255)
at org.apache.nifi.processor.AbstractProcessor.onTrigger(AbstractProcessor.java:27)
at org.apache.nifi.util.StandardProcessorTestRunner$RunProcessor.call(StandardProcessorTestRunner.java:251)
at org.apache.nifi.util.StandardProcessorTestRunner$RunProcessor.call(StandardProcessorTestRunner.java:245)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
其中
MyCustomProcessorTest.java:47
是unpackContentRunner.run(1)
模拟框架并不是真正用来编写链接多个处理器的测试。模拟框架用于对单个处理器进行单元测试
使用模拟框架设置流文件有许多不同的方法。流文件的内容可以来自文件、字符串、输入流或字节数组:
可以指定可选的属性映射来设置预期的流文件属性
一种常见的方法是在src/test/resources中为自定义处理器所需的任何数据设置文件,然后调用testRunner.enqueue(pathToTestFile)。模拟框架实际上并不打算编写将多个处理器链接在一起的测试。模拟框架用于对单个处理器进行单元测试 使用模拟框架设置流文件有许多不同的方法。流文件的内容可以来自文件、字符串、输入流或字节数组: 可以指定可选的属性映射来设置预期的流文件属性
一种常见的方法是在src/test/resources中为自定义处理器需要的任何数据设置文件,然后调用testRunner.enqueue(pathToTestFile)。表示“将给定路径中的内容读取到内存中,并使用给定属性从该内容创建一个流文件,并将该流文件添加到处理器的输入队列中”。这是否会向所有流文件添加相同的属性集?如果是,如果这些流文件需要具有不同的属性,处理器才能以不同的方式处理它们,该怎么办?您可以多次调用enqueue来创建多个流文件,每次传入不同的路径并映射“将给定路径中的内容读取到内存中,并使用给定属性从此内容创建一个流文件,并将此流文件添加到处理器的输入队列中“。这是否会向所有流文件添加相同的属性集?如果是,如果这些流文件需要具有不同的属性,处理器才能以不同的方式处理它们,该怎么办?您可以多次调用enqueue来创建多个流文件,每次传入不同的路径并映射
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.AssertionError: java.lang.IllegalStateException: FlowFile[0,622261873281800.mockFlowFile,0B] already in use for an active callback or InputStream created by ProcessSession.read(FlowFile) has not been closed
at org.apache.nifi.util.StandardProcessorTestRunner.run(StandardProcessorTestRunner.java:201)
at org.apache.nifi.util.StandardProcessorTestRunner.run(StandardProcessorTestRunner.java:160)
at org.apache.nifi.util.StandardProcessorTestRunner.run(StandardProcessorTestRunner.java:155)
at org.apache.nifi.util.StandardProcessorTestRunner.run(StandardProcessorTestRunner.java:150)
at MyCustomProcessorTest.testOnTrigger(MyCustomProcessorTest.java:47)
... 23 more
Caused by: java.lang.IllegalStateException: FlowFile[0,622261873281800.mockFlowFile,0B] already in use for an active callback or InputStream created by ProcessSession.read(FlowFile) has not been closed
at org.apache.nifi.util.MockProcessSession.validateState(MockProcessSession.java:1014)
at org.apache.nifi.util.MockProcessSession.putAllAttributes(MockProcessSession.java:488)
at org.apache.nifi.util.MockProcessSession.inheritAttributes(MockProcessSession.java:1044)
at org.apache.nifi.util.MockProcessSession.create(MockProcessSession.java:299)
at org.apache.nifi.util.MockProcessSession.create(MockProcessSession.java:62)
at org.apache.nifi.processors.standard.UnpackContent$FlowFileStreamUnpacker$1.process(UnpackContent.java:415)
at org.apache.nifi.util.MockProcessSession.read(MockProcessSession.java:547)
at org.apache.nifi.util.MockProcessSession.read(MockProcessSession.java:529)
at org.apache.nifi.processors.standard.UnpackContent$FlowFileStreamUnpacker.unpack(UnpackContent.java:409)
at org.apache.nifi.processors.standard.UnpackContent.onTrigger(UnpackContent.java:255)
at org.apache.nifi.processor.AbstractProcessor.onTrigger(AbstractProcessor.java:27)
at org.apache.nifi.util.StandardProcessorTestRunner$RunProcessor.call(StandardProcessorTestRunner.java:251)
at org.apache.nifi.util.StandardProcessorTestRunner$RunProcessor.call(StandardProcessorTestRunner.java:245)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)