NiFi中的输入流文件没有可行的选择

时间:2018-10-24 09:39:58

标签: python inputstream apache-nifi

我在脚本中遇到了问题,该脚本将一些消息解析为json并将其写入文件中。代码如下,错误是:在行号70的第0列的输入'flowFile'处没有可行的替代方案。

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

class PyStreamCallback(StreamCallback):
  def __init__(self):
        pass
  def process(self, inputStream, outputStream):
    data = inputStream
    datetime_now = datetime.now()
    log_date = datetime_now.isoformat()
    message = data.replace('  ', ' ').replace('\n ','').replace(' | ', '| ').split('| ')[1].split(" ")
    ip = message[2]
    key = "vod_" + ip
    if message[2] == 'PAUSE' or message[0] == 'PLAY' or message[1] == 'STOP':
        try:
            time_n = int(time())
            action = message[0].upper()
            url = message[3]
            filename = ''
            ref = ''
            if '?' in url:
                parsed_prepare = urlparse(url)
                parsed = parse_qs(parsed_prepare.query)
                filename = url.split('?')[0].split('/')[3]
                for key, value in parsed.iteritems():
                    for el in value:
                        parsed.update({key: el})
                for key, value in parsed.iteritems():
                    if key == 'ref':
                        ref = value
                for key, value in parsed.iteritems():
                    if key == 'titId':
                        c = value
                if c is not None:
                    cat = c[-2:]
                    catalog_id = cat.lstrip('0')
                    content_id = c[:-2]
                dict_test = {"user": {"guruq" : 'false'},
                "device" : {"type" : "siolbox",
                "ip": ip},
                "event" : {"origin" : "iptv",
                "timestamp": time_n,
                "product-type" : "video",
                "type": "vod",
                "channel": {},
                "content": {
                    "id" : content_id,
                    "catalog-id": catalog_id,
                    "action": action,
                    "url": url,
                    "ref" : ref,
                    "filename": filename}
                    }}
            else:
                filename = url.split('/')[2]
            outputStream.write(bytearray(json.dumps(dict_test, indent=4).encode('utf-8')))
        except Exception as e:
            print('%s vod %s 500...' % (log_date, message[2])

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

我需要将其处理到下一个NiFi流程putFile并将其写入文件中,但是我遇到了我根本无法理解的错误。这是我第一次使用NiFi。

1 个答案:

答案 0 :(得分:0)

第68行缺少右括号。尝试按如下所示添加它:

print('%s vod %s 500...' % (log_date, message[2]))