Search code examples
pythonapache-nifi

Use ExecuteScript with Python to write JSON to file


I'm trying to access the GitHub API and write the contents to a FlowFile. I could either create the FlowFile in the Python script or use GenerateFlowFile and send it into the script, I'm not picky. The code I have so far for creating the JSON is as follows (using urllib to comply with Jython):

import urllib.request
import json

headers = {
    'Content-Type': 'application/json {}'.format('cat'),
    'Authorization': 'token TOKEN_TEXT'
}

page = 1
response_text = []

while page > 0:
    req = urllib.request.Request(url="GITHUB_API_URL".format(page),\
                                 data=None, headers=headers)
    
    with urllib.request.urlopen(req) as resp:
        data = json.loads(resp.read().decode("utf-8"))

    
        if len(data) == 0:
            break
        else:
            response_text.extend(data)
            page += 1

I have confirmed I get the JSON as desired, I really just need to write response_text to a FlowFile to pass onto the next processor in my flow. I've tried researching, but I can't seem to find any hard documentation on the ExecuteScript processor (if you know where I could find that, I'd appreciate that, as well).


Solution

  • I've written a series of articles called the ExecuteScript Cookbook (part 1, part 2 and part 3) that has (among other supported languages) Jython examples of common NiFi API calls. Take a look at Part 2 which deals with FlowFile I/O. I should mention that the Jython script engine used in NiFi is based on Python 2 not 3.