简体   繁体   English

如何将FlowFile的属性放入其JSON内容?

[英]How to put attributes of FlowFile into its JSON content?

I use ExecuteScript processor and Python language to write a script. 我使用ExecuteScript处理器和Python语言编写脚本。

I want to pass two attributes ( eventid and reason ) of FlowFile into its JSON content as the parameter:value pair. 我想将FlowFile的两个属性( eventidreason )作为参数:值对传递到其JSON内容中。 The value of eventid is string, while the value of reason is integer. eventid的值是字符串, reason的值是整数。 I tried to use flowFile.getAttribute('eventid') , but it fails. 我尝试使用flowFile.getAttribute('eventid') ,但失败。

What is the correct way? 正确的方法是什么?

def process(self, inputStream, outputStream):
        text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
        obj = json.loads(text)
        dt = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')

        newObj = {
            "EventId": str(parse(flowFile.getAttribute('eventid'))),
            "EventType": self.getEventType(dt,obj),
            "EventReason": flowFile.getAttribute('reason')
        }
        outputStream.write(bytearray(json.dumps(newObj, indent=4).encode('utf-8')))

flowFile = session.get()
if (flowFile != None):
    flowFile = session.write(flowFile, ModJSON())
session.transfer(flowFile, REL_SUCCESS)
session.commit()

You can use EvaluateJsonPath with Destination set to flow-file attributes and Return Type set to JSON. 您可以将EvaluateJsonPath与Destination设置为流文件属性,Return Type设置为JSON。 Then you can add properties for each JSON path to extract like: 然后,您可以为每个JSON路径添加属性以进行提取,例如:

eventid = $.eventid 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM