我有我的ExecuteScript处理器,如果文件最初是utf-16的话,我会尝试转换到utf-8的任何文件。
迄今为止:
flowFileList = session.get(100)
if not flowFileList.isEmpty():
for flowFile in flowFileList:
# Process each FlowFile here:
flowFileList.decode("utf-16").encode("utf-8")我觉得这应该是一个相当容易的操作,正如这些答案所定义的:这里、这里和这里。
这会引发一个错误,即“对象中没有属性'decode‘in”。
如果这是个愚蠢的问题,请随便说。谢谢
NiFi ExecuteScript:烹饪书烹饪书
发布于 2018-12-10 22:46:07
问题是您是在flowfileList对象上调用flowfileList,而不是单个流文件。
此外,您还需要实际访问then文件内容,然后使用新的编码设置内容。现在,您正在将flowfile对象视为字符串,但它不是。我离开了我的电脑,但稍后会有工作的示例代码。
更新
我将提供有用的Python代码来演示这一点,但是为什么不能只使用ConvertCharacterSet处理器呢?这接受输入字符集和输出字符集。
下面是工作代码,它将将传入的流文件内容从UTF-16转换为UTF-8。您应该尝试过滤现有的UTF-8内容,以跳过这个处理器,或者添加识别它的代码,而不对其进行处理。对于同样的行为,您也可能对跟踪NIFI-4550 -加法InferCharacterSet处理器感兴趣。
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
# Define a subclass of StreamCallback for use in session.write()
class PyStreamCallback(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
text = IOUtils.toString(inputStream, StandardCharsets.UTF_16)
outputStream.write(bytearray(text.encode('utf-8')))
# end class
flowFileList = session.get(100)
if not flowFileList.isEmpty():
for flowFile in flowFileList:
flowFile = session.write(flowFile, PyStreamCallback())
flowFile = session.putAttribute(flowFile, 'script_character_set', 'UTF-8')
session.transfer(flowFile, REL_SUCCESS)
# implicit return at the endhttps://stackoverflow.com/questions/53713733
复制相似问题