![](/img/trans.png)
[英]How to pass flow files to the Execute Python script and use attributes & Nifi variables to store that file?
[英]How to create multiple flow files from one incoming flow files in nifi using ExecuteScript with python
在本地運行,這完全符合我的要求(在位置 7-10 有一個帶有許多不同代碼的傳入流文件,每個唯一代碼輸出 1 個文件)例如,如果記錄 1-5 在位置 7-10 中有 1234,並且記錄 6 在位置 7-10 中有 2345 個,記錄 7 在位置 7-10 中有 1234 個,那么將有一個名為 1234_file.txt 的文件,其中包含第 1-5 行和第 7 行,第二個文件 2345_file.txt 將具有第 6 行輸入文件:
f = open("test_comp.txt", "r")
for x in f:
comp = x[6:10]
print(comp)
n = open(comp+"_file.txt","a")
n.write(x)
n.close()
f.close()
在 nifi 中,我正在嘗試這個:
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
class PyStreamCallback(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
f = open(inputStream, 'r')
for x in f:
comp = x[6:10]
print("comp: ",comp)
newFile = open(comp+"_file.txt","a")
newFile.write(x)
flowFile = session.get()
if (flowFile != None):
flowFile = session.write(flowFile, PyStreamCallback())
session.transfer(flowFile, REL_SUCCESS)
session.commit()
它似乎正在獲取輸入並按預期將 comp 正確存儲為位置 7-10,但我沒有得到多個流文件(對於 x[6:10] 中的每個唯一字符串。輸出的流文件是 1零字節文件。
對我缺少的東西有什么想法嗎??
你是直接寫入到文件系統中的文件,而不是flowfiles這是NiFi生態系統中的對象。 我建議閱讀Apache NiFi 開發人員指南以了解有關這些模式的上下文,並查看一些Python ExecuteScript 示例以查看相關的 Python 代碼。
您需要創建多個流文件對象,將數據映射到它們,然后將它們全部傳輸到相應的關系,而不是寫出單個流文件。
您是否需要使用自定義 Python 代碼而不是SplitRecord
和/或PartitionRecord
處理器來執行此操作? 我認為PartitionRecord
可以很容易地解決您描述的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.