在Nifi中使用python解析消息并另存为json

时间:2018-10-03 20:21:42

标签: python json jython apache-nifi

我正在尝试从其他Nifi处理器中的Nifi处理器(Collected_data)读取消息(16位数字),该消息包含将其解析为json(python字典)的python脚本,我需要将其保存为json,分离的json文件。

代码:

import datetime
import hashlib
import sys
from urlparse import urlparse, parse_qs
from datetime import *
from time import time
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

class PyStreamCallback(StreamCallback):
    def __init__(self):
        pass
    def process(iself, inputStream, outputStream):
        data = IOUtils.readLines(inputStream)
        buf = (hashlib.sha256(bytearray.fromhex(data)).hexdigest())
        buf = int(buf, 16)
        buf_check = str(buf)
        if buf_check[17] == 2:
            pass
        datetime_now = datetime.now()
        log_date = datetime_now.isoformat()
        try:
            mac = buf_check[16:22].upper()
            ob_id = buf_check[4:]
            action = buf_check[1:2]
            time_a = int(time())
            dict_test = {
            "user": {
                "gur" : 'false'
            },
            "device" : {
                "type" : "box",
                "mac": mac
            },
            "event" : {
                "origin" : "tv",
                "timestamp": time_a,
                "type": "zap",
                "product-type" : "radiochannel",
                "channel": {
                    "id" : 'channel_id',
                    "ob-id": ob_id
                },
                "content": {
                    "action": action
                }
            }
            }
            outputStream.write(bytearray(json.dumps(dict_test, indent=4).encode('utf-8')))
            return dict_test
        except Exception as e:
            print('%s nod PARSE 500 \"%s\"' % (log_date, e))

flowFile = session.get()
if (flowFile != None):
    flowFile = session.write(flowFile,PyStreamCallback())
    flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json')
    session.transfer(flowFile, REL_SUCCESS)

此消息发送到Nifi中的进程PutFile中,并且每条消息都需要另存为文件,并在其中解析json。 然后我创建了16位数字+“ translated.json”文件,但该文件中的下一个文件是

^A^D^@7^A^@^A ^@^@^Q<9e>^@^@    <99>^@^D0<94>À6^A 

欢迎任何想法,暗示或建议! 提前致谢。

0 个答案:

没有答案