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

nifi + executescript count lines in file using python

您好,我需要获取 csv 文件中不包括第一行的行数 headers 我需要将行数存储在属性中并将未触及的流文件解析到下一个处理器

我正在考虑使用 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)

隐式return结尾

这不会运行

尝试使用 SplitText 处理器,将行拆分计数设置为高于文件中可能的最大行数(例如 100 万)的某个数字。如果您希望总行数减去 header,您还可以将 Header 行计数设置为 1。您将获得相同的流文件,但具有包含行数的属性 text.line.count。