【问题标题】:Append filenames of flowfiles in NiFi to CSV file将 NiFi 中流文件的文件名附加到 CSV 文件
【发布时间】:2018-06-11 19:10:30
【问题描述】:

首先,我使用 listFile 处理器列出特定映射中的所有文件。然后,我想将 NiFi 中流文件的 ${filename} 附加到外部文件 example.csv 中。我想过在 Python 中使用 ExecuteScript,但不知道怎么做。

我尝试了以下方法:

使用两个列表处理器,一个用于地图,一个用于 example.csv 文件。 replaceText 处理器将文本替换为 ${filename}mergeContentputFile(replace example.csv)。只在第一次工作。

【问题讨论】:

  • 地图文件是什么样的?它是要合并到单个 csv 文件中的文件名列表吗?
  • 是的。例如 /path/to/files/ 包含三个文件 example1.csv、example2.csv、example3.csv。我希望这些文件名位于一个名为 files.csv 的文件中。
  • 为什么 mergeContent 只能在第一次工作?如果您要覆盖examples.csv,请务必阅读它,否则您只需附加到它并清理您已阅读的内容

标签: apache-nifi


【解决方案1】:

可以使用ExecuteScript处理器,在python中有如下脚本

from org.apache.nifi.processors.script import ExecuteScript 
import subprocess

flowFile = session.get()
if flowFile is not None:
     myCsvRow = flowFile.getAttribute('filename')
     fd = open('/path/to/your/map/files.csv','a')
     fd.write(myCsvRow)
     fd.close()
     session.transfer(flowFile,ExecuteScript.REL_SUCCESS)

【讨论】:

  • 第二行给出错误信息。通过删除,它可以完美地工作。
猜你喜欢
  • 2019-08-01
  • 1970-01-01
  • 2021-11-22
  • 2018-11-18
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
  • 2021-07-21
相关资源
最近更新 更多