Import error No module named constant_time while accessing server

…衆ロ難τιáo~ 提交于 2019-12-25 08:39:47

问题


This is the follow up of Import Modules in Nifi ExecuteScript

I am new to python as well as nifi. I am trying to execute my python script in ExecuteScript processor.

I want to access a server. so i used paramiko client. But when i run the processor, it shows "Import error No module named constant_time" at line session.write(). Though i have this constant_time.py under "/usr/local/lib/python2.7/dist-packages/ "

I have also the path "/usr/local/lib/python2.7/dist-packages/ " in sys.path. I have also given this path in the "Module Directory" property.

This is my code:

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)

Any help would be appreciated.


回答1:


As explained by Matt Burgess in this answer, the ExecuteScript processor uses Jython, not Python, so compiled modules are not available. There is ongoing discussion of how/if to add this feature.



来源:https://stackoverflow.com/questions/40744911/import-error-no-module-named-constant-time-while-accessing-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!