如何在Nifi执行脚本处理器中将测试文件获取到输出流流文件

时间:2019-09-27 10:21:56

标签: python apache-nifi

我试图在Nifi ExcuteScript处理器中编写一个文本文件,其中包含字典到outputStream回调。这是我的代码,但无法正常工作-

import json
import java.io
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
import os
import java.io.FileOutputStream

ofile=open("/home/nifi/data/outfile.txt", 'a')
with open("/home/nifi/data/validation.json",'r') as vfile:
    validation = json.loads(vfile.read())
finaldict={}
dict1={}

class ModJSON(StreamCallback):
  def __init__(self):
        pass
  def process(self, inputStream, outputStream):
    text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
    invalidrecord = json.loads(text)

    for record in invalidrecord:
        #print(finaldict)
        finaldict.update(record)
        # print(finaldict)
        finaldict['errors'] = []
        errordict = {}
        # loop through each field from validation json
        for field in validation:
            for element, value in record.items():
                if field == element:
                    dtype = type(value)
                    dict1 = validation[field]
                    for validtype in dict1:
                        if validtype['validationType'] == 'datatype':
                            if str(dtype) == validtype['check']:
                                pass
                            else:
                                errordict = {"element": field}
                                errordict.update({"errorCode": validtype['errorCode']})
                                finaldict["errors"].append(errordict)
                        if validtype['validationType'] == 'maxlength':
                            dlen = len(str(value))
                            if str(dlen) <= str(validtype['check']):
                                pass
                            else:
                                errordict = {"element": field}
                                errordict.update({"errorCode": validtype['errorCode']})
                                finaldict["errors"].append(errordict)
                else:
                    pass
        json.dump(finaldict, ofile)

    outputStream.write(bytearray(ofile.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)
session.commit()

在上面的代码中,outputStream.write(bytearray(ofile.encode('utf-8')))没有将outfile.txt写入flowfile并抛出:

  

session.transfer(flowFile,REL_SUCCESS)出现NullPointerException

有人可以建议这些代码有什么问题吗?

相关问题在下面的链接中,但找不到我想要的内容,因为它没有将text_file发送到nifi的flowfile输出中。

Access Json element and write to a text file using python ExecuteScript processor

0 个答案:

没有答案