<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Python in ExecuteScript: Transform both flowfile attributes &amp; content in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177568#M139816</link>
    <description>&lt;P&gt;&lt;A href="https://www.welookups.com"&gt;https://www.welookups.com&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 23 Aug 2018 23:42:50 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2018-08-23T23:42:50Z</dc:date>
    <item>
      <title>Python in ExecuteScript: Transform both flowfile attributes &amp; content</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177563#M139811</link>
      <description>&lt;P&gt;I am trying to create a Python script in NiFi that: &lt;/P&gt;&lt;OL&gt;&lt;LI&gt; Reads some attributes from an incoming flowfile &lt;/LI&gt;&lt;LI&gt; Read the json content of the flowfile &amp;amp; extract specific fields &lt;/LI&gt;&lt;LI&gt;Write attributes to outgoing flowfile &lt;/LI&gt;&lt;LI&gt; Overwrite incoming flowfile with new content that is created in the script (e.g. API call that returns new json) and send it to SUCCESS relationship OR remove the old flowfile and create new with desired content &lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;What i ve done so far: &lt;/P&gt;&lt;PRE&gt;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,InputStreamCallback, OutputStreamCallback

class OutputWrite(OutputStreamCallback, obj):

def __init__(self):
    self.obj = obj

def process(self, outputStream):

    outputStream.write(bytearray(json.dumps(self.obj).encode('utf')))

###end class###

flowfile = session.get()

if flowfile != None:

**#1) Get flowfile attributes**

    headers = {
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept': 'application/json, text/plain, */*',
        'Cache-Control': 'no-cache',
        'Ocp-Apim-Trace': 'true',
        'Authorization': flowfile.getAttribute('Authorization')
    }

    collection = flowfile.getAttribute('collection')
    dataset = flowfile.getAttribute('dataset')

    **#2)Get flowfile content**

    stream_content = session.read(flowfile)
    text_content = IOUtils.toString(stream_content, StandardCharsets.UTF_8)
    json_content = json.loads(text_content)

    records = json_content['result']['count']
    pages = records/10000

    **#3) Write flowfile attributes**

    flowfile = session.putAttribute(flowfile, 'collection', collection)
    flowfile = session.putAttribute(flowfile, 'dataset', dataset)

    **#API operations: output_json with desired data**

    output_json = {some data}

    **#4) Write final JSON data to output flowfile**

    flowfile = session.write(flowfile, OutputWrite(output_json))

    session.transfer(flowfile, REL_SUCCESS)
    session.commit()&lt;/PRE&gt;&lt;P&gt;My problem is that i can't find a way to pass a reference to the desired output_json object as an argument in the OutputStreamCallback class. Any ideas on how to resolve this or maybe a better approach? 
Is it maybe easier to perform all API operations in this case within the process function of the class, but then how do i get access to the incoming flowfile attributes within the process function (requires a session or a flowfile object) ? 
Any help much appreciated!&lt;/P&gt;</description>
      <pubDate>Mon, 12 Mar 2018 19:21:36 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177563#M139811</guid>
      <dc:creator>foivos</dc:creator>
      <dc:date>2018-03-12T19:21:36Z</dc:date>
    </item>
    <item>
      <title>Re: Python in ExecuteScript: Transform both flowfile attributes &amp; content</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177564#M139812</link>
      <description>&lt;P&gt;&lt;A rel="user" href="https://community.cloudera.com/users/641/mburgess.html" nodeid="641"&gt;@Matt Burgess&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Seems i could do all API operations within the process function of the CallBack class, but in this case i need access to the flowfile attributes besides the content within the class scope. Tried to pass a flowfile reference to the class definition, but failed. Any ideas?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Mar 2018 18:09:01 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177564#M139812</guid>
      <dc:creator>foivos</dc:creator>
      <dc:date>2018-03-13T18:09:01Z</dc:date>
    </item>
    <item>
      <title>Re: Python in ExecuteScript: Transform both flowfile attributes &amp; content</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177565#M139813</link>
      <description>&lt;P&gt;Solved, much simpler than i thought. You just need to pass the flowfile to the initialization function of the CallBack class.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Mar 2018 19:04:23 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177565#M139813</guid>
      <dc:creator>foivos</dc:creator>
      <dc:date>2018-03-13T19:04:23Z</dc:date>
    </item>
    <item>
      <title>Re: Python in ExecuteScript: Transform both flowfile attributes &amp; content</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177566#M139814</link>
      <description>&lt;P&gt;Hi Balalaika, I dont understand the answer you have posted. Can you please explain a bit more? Also from where does the obj comes in the OutputWrite class?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 08:53:53 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177566#M139814</guid>
      <dc:creator>bharat_sudharsa</dc:creator>
      <dc:date>2018-08-21T08:53:53Z</dc:date>
    </item>
    <item>
      <title>Re: Python in ExecuteScript: Transform both flowfile attributes &amp; content</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177567#M139815</link>
      <description>&lt;A rel="user" href="https://community.cloudera.com/users/89301/bharatsudharsanam.html" nodeid="89301"&gt;@Bharath
 Sudharsanam
&lt;/A&gt;&lt;P&gt;If you have not already, please review this 3 part series on ExecuteScript.  It was very helpful for me when I was working with my custom script and ExecuteScript Processor.&lt;/P&gt;&lt;P style="margin-left: 20px;"&gt;&lt;A href="https://community.hortonworks.com/content/kbentry/75032/executescript-cookbook-part-1.html" target="_blank"&gt;https://community.hortonworks.com/content/kbentry/75032/executescript-cookbook-part-1.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 18:31:14 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177567#M139815</guid>
      <dc:creator>stevenmatison</dc:creator>
      <dc:date>2018-08-21T18:31:14Z</dc:date>
    </item>
    <item>
      <title>Re: Python in ExecuteScript: Transform both flowfile attributes &amp; content</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177568#M139816</link>
      <description>&lt;P&gt;&lt;A href="https://www.welookups.com"&gt;https://www.welookups.com&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 23:42:50 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/177568#M139816</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2018-08-23T23:42:50Z</dc:date>
    </item>
    <item>
      <title>Re: Python in ExecuteScript: Transform both flowfile attributes &amp; content</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/375748#M242633</link>
      <description>&lt;P&gt;Can you help me with a similar issue?&lt;BR /&gt;&lt;BR /&gt;My code:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;import&lt;/SPAN&gt; &lt;SPAN&gt;json&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;import&lt;/SPAN&gt; &lt;SPAN&gt;collections&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;import&lt;/SPAN&gt; &lt;SPAN&gt;java&lt;/SPAN&gt;&lt;SPAN&gt;.io&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;import&lt;/SPAN&gt; &lt;SPAN&gt;ast&lt;/SPAN&gt;&lt;SPAN&gt; &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;from&lt;/SPAN&gt;&lt;SPAN&gt; org.apache.commons.io &lt;/SPAN&gt;&lt;SPAN&gt;import&lt;/SPAN&gt;&lt;SPAN&gt; IOUtils&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;from&lt;/SPAN&gt;&lt;SPAN&gt; java.nio.charset &lt;/SPAN&gt;&lt;SPAN&gt;import&lt;/SPAN&gt;&lt;SPAN&gt; StandardCharsets&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;from&lt;/SPAN&gt;&lt;SPAN&gt; org.apache.nifi.processor.io &lt;/SPAN&gt;&lt;SPAN&gt;import&lt;/SPAN&gt;&lt;SPAN&gt; StreamCallback&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;class&lt;/SPAN&gt; &lt;SPAN&gt;ModJSON&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;StreamCallback) :&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;def&lt;/SPAN&gt; &lt;SPAN&gt;__init__&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;self):&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;pass&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;def&lt;/SPAN&gt; &lt;SPAN&gt;process&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;self&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;inputStream&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;outputStream):&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;text&lt;/SPAN&gt; &lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt; IOUtils.toString(&lt;/SPAN&gt;&lt;SPAN&gt;inputStream&lt;/SPAN&gt;&lt;SPAN&gt;, StandardCharsets.UTF_8)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;obj&lt;/SPAN&gt; &lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;ast&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;literal_eval&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;text&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;mail&lt;/SPAN&gt; &lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;obj&lt;/SPAN&gt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;SPAN&gt;flowFile&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"filename"&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;flowFile&lt;/SPAN&gt;&lt;SPAN&gt;.getAttribute(&lt;/SPAN&gt;&lt;SPAN&gt;'filename'&lt;/SPAN&gt;&lt;SPAN&gt;).split(&lt;/SPAN&gt;&lt;SPAN&gt;'.'&lt;/SPAN&gt;&lt;SPAN&gt;)[&lt;/SPAN&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;SPAN&gt;]&lt;/SPAN&gt;&lt;SPAN&gt;+&lt;/SPAN&gt;&lt;SPAN&gt;'_translated.json'&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;flowFile&lt;/SPAN&gt; &lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt; session.putAttribute(&lt;/SPAN&gt;&lt;SPAN&gt;flowFile&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;"mail"&lt;/SPAN&gt;&lt;SPAN&gt;, mail)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;session.transfer(&lt;/SPAN&gt;&lt;SPAN&gt;flowFile&lt;/SPAN&gt;&lt;SPAN&gt;, REL_SUCCESS)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;session.commit()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I want to get "mail" value and create a new attribute for it. However, mail variable is not accessible when I insert it into the putattribute command. Any feedback?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 29 Aug 2023 15:39:58 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Python-in-ExecuteScript-Transform-both-flowfile-attributes/m-p/375748#M242633</guid>
      <dc:creator>Kiranq</dc:creator>
      <dc:date>2023-08-29T15:39:58Z</dc:date>
    </item>
  </channel>
</rss>

