简体   繁体   中英

How to put attributes of FlowFile into its JSON content?

I use ExecuteScript processor and Python language to write a script.

I want to pass two attributes ( eventid and reason ) of FlowFile into its JSON content as the parameter:value pair. The value of eventid is string, while the value of reason is integer. I tried to use flowFile.getAttribute('eventid') , but it fails.

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. Then you can add properties for each JSON path to extract like:

eventid = $.eventid 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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