这是Import Modules in Nifi ExecuteScript
的后续行动我是python以及nifi的新手。我试图在ExecuteScript处理器中执行我的python脚本。
我想访问服务器。所以我用paramiko客户端。但是当我运行处理器时,会在session.write()行显示“导入错误没有名为constant_time的模块”。虽然我在“/usr/local/lib/python2.7/dist-packages/”下有这个constant_time.py
我在sys.path中也有路径“/usr/local/lib/python2.7/dist-packages/”。我还在“模块目录”属性中给出了此路径。
这是我的代码:
import json, pysftp, paramiko
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
class ModJSON(StreamCallback):
def __init__(self):
pass
def process(self, inputStream, outputStream):
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
inputText = text.rstrip('\r\n')
json_content = json.loads(inputText)
body = ''
try:
body = json_content['id']['body']
body_encoded = body.encode('utf-8')
except (KeyError,TypeError,ValueError):
body_encoded = ''
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.load_system_host_keys()
ssh_client.connect('server', username='xxx', password='xxxx')
sftp_client = ssh_client.open_sftp()
text_file = sftp_client.open ('/doc/body.txt', 'w')
text_file.write("%s"%body_encoded)
text_file.close()
outputStream.write(bytearray(json.dumps(body, indent=4).encode('utf-8')))
flowFile = session.get()
if (flowFile != None):
flowFile = session.write(flowFile, ModJSON())
flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json')
session.transfer(flowFile, REL_SUCCESS)
任何帮助都将不胜感激。
答案 0 :(得分:0)
正如Matt Burgess在this answer中所解释的那样,ExecuteScript
处理器使用 Jython ,而不是 Python ,因此编译后的模块不可用。目前正在讨论如何/是否添加此功能。