簡體   English   中英

nifi + executescript使用python計算文件中的行

[英]nifi + executescript count lines in file using python

嗨,我需要獲取csv文件中的行數,而第一行是標頭,我需要將行數存儲在屬性中,並解析未觸及下一個處理器的流文件

我當時在考慮使用extracttext,但是我不認為正則表達式可以做到這一點。

因此,下一步將是executeScript處理器。 我想到了帶有以下模板的python腳本

flowFile = session.get() 
if (flowFile != None):
# All processing code starts at this indent
attrMap = ['numberOflines': '1', 'myAttr2': Integer.toString(2)]
flowFile = session.get()
if(!flowFile) return
#Do something to get numbers of lines in the flow file
i =0;
    for line in flowfile
        i+=1

flowFile = session.putAttribute(flowFile, 'attribute_numberOfLines', i)
if errorOccurred:
    session.transfer(flowFile, REL_FAILURE)
else:
    session.transfer(flowFile, REL_SUCCESS)

最后的隱式收益

這將無法運行

嘗試將“行拆分計數”設置為比文件中可能的最大行數(例如一百萬)高一些的SplitText處理器。 如果希望總行數減去標題,也可以將“標題行數”設置為1。 您將獲得相同的流文件,但具有將包含行數的屬性text.line.count。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM