<?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: Can anyone provide an example of a python script executed through ExecuteStreamCommand? in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192488#M76350</link>
    <description>&lt;A rel="user" href="https://community.cloudera.com/users/69253/vincentvanoudenhoven.html" nodeid="69253"&gt;@Vincent van Oudenhoven&lt;/A&gt;&lt;P&gt;Any specific reason for using ExecuteStream Command for this use case of yours? I will recommend using ExecuteScript or InvokeScript Processor and you can perform all the aforementioned operations from your question!&lt;/P&gt;&lt;P&gt;For example, a very beginners example can be the following script which reads a file and create an empty file with all its attributes using ExecuteScript Processor.&lt;/P&gt;&lt;PRE&gt;flowFile = session.get()
attrMap = flowFile.getAttributes()
session.remove(flowFile)
newflowFile = session.create()
newflowFile = session.putAllAttributes(newflowFile, attrMap)
session.transfer(newflowFile, REL_SUCCESS)&lt;/PRE&gt;&lt;P&gt;Or this groovy script in an ExecuteScript processor which can read the content of your flow files and accordingly redirect them to downstream connections.&lt;/P&gt;&lt;PRE&gt;import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
flowFile = session.get()
if(!flowFile)return
def text = ''
def storeID = 0
session.read(flowFile, {inputStream -&amp;gt;
  text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
  storeID = text.tokenize("|")[2]
} as InputStreamCallback)
if(storeID &amp;gt;=1 &amp;amp;&amp;amp; storeID &amp;lt;= 10)
	session.transfer(flowFile, REL_SUCCESS)
else (storeID &amp;gt;10 &amp;amp;&amp;amp; storeID &amp;lt;= 20)
	session.transfer(flowFile, REL_FAILURE)&lt;/PRE&gt;&lt;P&gt;You can have an external script executed using ExecuteStream command also but why maintain any code outside when the inbuilt flow file handling logic in a processor like ExecuteScript processor can help you achieve the functionality more easily.&lt;/P&gt;</description>
    <pubDate>Sun, 25 Mar 2018 05:03:05 GMT</pubDate>
    <dc:creator>RahulSoni</dc:creator>
    <dc:date>2018-03-25T05:03:05Z</dc:date>
    <item>
      <title>Can anyone provide an example of a python script executed through ExecuteStreamCommand?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192487#M76349</link>
      <description>&lt;P&gt;Albeit obvious, the processor should have the following properties:&lt;/P&gt;&lt;UL&gt;
&lt;LI&gt;Calls a python script&lt;/LI&gt;&lt;LI&gt;Able to supply the FlowFile in to the python script&lt;/LI&gt;&lt;LI&gt;Read the FlowFile from within the python script&lt;/LI&gt;&lt;LI&gt;Update either the original FlowFile or create a new FlowFile from within the python script&lt;/LI&gt;&lt;LI&gt;Output the updated/new FlowFile back in to Nifi&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;A href="https://community.hortonworks.com/questions/177640/python-script-using-executestreamcommand.html" target="_blank"&gt;Original question&lt;/A&gt; (without any responses)&lt;/P&gt;&lt;P&gt;Any pointers/advice/help is appreciated&lt;/P&gt;</description>
      <pubDate>Fri, 16 Sep 2022 13:01:36 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192487#M76349</guid>
      <dc:creator>vincentvanouden</dc:creator>
      <dc:date>2022-09-16T13:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: Can anyone provide an example of a python script executed through ExecuteStreamCommand?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192488#M76350</link>
      <description>&lt;A rel="user" href="https://community.cloudera.com/users/69253/vincentvanoudenhoven.html" nodeid="69253"&gt;@Vincent van Oudenhoven&lt;/A&gt;&lt;P&gt;Any specific reason for using ExecuteStream Command for this use case of yours? I will recommend using ExecuteScript or InvokeScript Processor and you can perform all the aforementioned operations from your question!&lt;/P&gt;&lt;P&gt;For example, a very beginners example can be the following script which reads a file and create an empty file with all its attributes using ExecuteScript Processor.&lt;/P&gt;&lt;PRE&gt;flowFile = session.get()
attrMap = flowFile.getAttributes()
session.remove(flowFile)
newflowFile = session.create()
newflowFile = session.putAllAttributes(newflowFile, attrMap)
session.transfer(newflowFile, REL_SUCCESS)&lt;/PRE&gt;&lt;P&gt;Or this groovy script in an ExecuteScript processor which can read the content of your flow files and accordingly redirect them to downstream connections.&lt;/P&gt;&lt;PRE&gt;import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
flowFile = session.get()
if(!flowFile)return
def text = ''
def storeID = 0
session.read(flowFile, {inputStream -&amp;gt;
  text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
  storeID = text.tokenize("|")[2]
} as InputStreamCallback)
if(storeID &amp;gt;=1 &amp;amp;&amp;amp; storeID &amp;lt;= 10)
	session.transfer(flowFile, REL_SUCCESS)
else (storeID &amp;gt;10 &amp;amp;&amp;amp; storeID &amp;lt;= 20)
	session.transfer(flowFile, REL_FAILURE)&lt;/PRE&gt;&lt;P&gt;You can have an external script executed using ExecuteStream command also but why maintain any code outside when the inbuilt flow file handling logic in a processor like ExecuteScript processor can help you achieve the functionality more easily.&lt;/P&gt;</description>
      <pubDate>Sun, 25 Mar 2018 05:03:05 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192488#M76350</guid>
      <dc:creator>RahulSoni</dc:creator>
      <dc:date>2018-03-25T05:03:05Z</dc:date>
    </item>
    <item>
      <title>Re: Can anyone provide an example of a python script executed through ExecuteStreamCommand?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192489#M76351</link>
      <description>&lt;P&gt;&lt;A rel="user" href="https://community.cloudera.com/users/66220/rsoni.html" nodeid="66220"&gt;@Rahul Soni&lt;/A&gt; &lt;/P&gt;&lt;P&gt;Thank you for your reply. The reason that I want to use ExecuteStreamCommand instead of ExecuteScript is because Jython is not an option for me. I am running a wide array of python commands and they need to be executed under a particular Anaconda environment. &lt;/P&gt;&lt;P&gt;I can't seem to find any solid examples of ExecuteStreamCommand, would you mind providing an example or pointing me in the right direction? &lt;/P&gt;&lt;P&gt;Edit: just to add, ExecuteProcess is also not an option as it does not allow for an incoming FlowFile.&lt;/P&gt;</description>
      <pubDate>Sun, 25 Mar 2018 05:42:49 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192489#M76351</guid>
      <dc:creator>vincentvanouden</dc:creator>
      <dc:date>2018-03-25T05:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: Can anyone provide an example of a python script executed through ExecuteStreamCommand?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192490#M76352</link>
      <description>&lt;P&gt;&lt;A rel="user" href="https://community.cloudera.com/users/69253/vincentvanoudenhoven.html" nodeid="69253" target="_blank"&gt;@Vincent van Oudenhoven&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Here is a very elementary flow to depict it using ExecuteStreamCommand processor.&lt;/P&gt;&lt;P&gt;The flow looks like&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="64862-screen-shot-2018-03-27-at-15009-am.png" style="width: 624px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/18300i1F0DD6895F8EDF54/image-size/medium?v=v2&amp;amp;px=400" role="button" title="64862-screen-shot-2018-03-27-at-15009-am.png" alt="64862-screen-shot-2018-03-27-at-15009-am.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;In the GenerateFlowFile processor, I am generating a flow file with sample text "foobar"&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="64863-screen-shot-2018-03-27-at-15034-am.png" style="width: 1354px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/18301i736058F898E333B4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="64863-screen-shot-2018-03-27-at-15034-am.png" alt="64863-screen-shot-2018-03-27-at-15034-am.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;In the ExecuteStreamCommand, I am referring to my python code as&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="64864-screen-shot-2018-03-27-at-15105-am.png" style="width: 1352px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/18302i89BBDA5609EDA6FA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="64864-screen-shot-2018-03-27-at-15105-am.png" alt="64864-screen-shot-2018-03-27-at-15105-am.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;The sample.py looks like as silly as &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="64865-screen-shot-2018-03-27-at-15143-am.png" style="width: 338px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/18303i88CE8B7D01AFC1A7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="64865-screen-shot-2018-03-27-at-15143-am.png" alt="64865-screen-shot-2018-03-27-at-15143-am.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;And now the content of the flow file looks like&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="64866-screen-shot-2018-03-27-at-15128-am.png" style="width: 840px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/18304i9F8F7B28F460584E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="64866-screen-shot-2018-03-27-at-15128-am.png" alt="64866-screen-shot-2018-03-27-at-15128-am.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;However, if you want to access the content of the existing flow file, I guess the only way you can do it is by converting the content to attribute and this can have consequences since attributes are kept in memory and a very large value for an attribute or a lot of attributes can adversely affect the performance.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;BR /&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Let know if that helps!&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Aug 2019 07:12:16 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192490#M76352</guid>
      <dc:creator>RahulSoni</dc:creator>
      <dc:date>2019-08-18T07:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: Can anyone provide an example of a python script executed through ExecuteStreamCommand?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192491#M76353</link>
      <description>&lt;P&gt;&lt;A rel="user" href="https://community.cloudera.com/users/69253/vincentvanoudenhoven.html" nodeid="69253"&gt;@Vincent van Oudenhoven&lt;/A&gt; Does that help?&lt;/P&gt;</description>
      <pubDate>Wed, 28 Mar 2018 04:27:55 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192491#M76353</guid>
      <dc:creator>RahulSoni</dc:creator>
      <dc:date>2018-03-28T04:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Can anyone provide an example of a python script executed through ExecuteStreamCommand?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192492#M76354</link>
      <description>&lt;A rel="user" href="https://community.cloudera.com/users/69253/vincentvanoudenhoven.html" nodeid="69253"&gt;@Vincent van Oudenhoven&lt;/A&gt;&lt;P&gt;Did the answer help in the resolution of your query? Please close the thread by marking the answer as Accepted!&lt;/P&gt;</description>
      <pubDate>Sun, 01 Apr 2018 23:10:51 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192492#M76354</guid>
      <dc:creator>RahulSoni</dc:creator>
      <dc:date>2018-04-01T23:10:51Z</dc:date>
    </item>
    <item>
      <title>Re: Can anyone provide an example of a python script executed through ExecuteStreamCommand?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192493#M76355</link>
      <description>&lt;P&gt;It did indeed help. I found the following StackOverflow answer to help too: &lt;A href="https://stackoverflow.com/questions/49467969/python-script-using-executestreamcommand" target="_blank"&gt;https://stackoverflow.com/questions/49467969/python-script-using-executestreamcommand&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Especially: &lt;/P&gt;&lt;PRE&gt;Command Arguments: any flags or args, delimited by ; (i.e. /path/to/my_script.py)

Command Path: /path/to/python3&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;Note the Command Path that you did not specify in the processor. This also allows the use of for example a predefined Anaconda environment. &lt;/P&gt;&lt;P&gt;Anyhow, thank you for the help!&lt;/P&gt;</description>
      <pubDate>Mon, 02 Apr 2018 01:57:54 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Can-anyone-provide-an-example-of-a-python-script-executed/m-p/192493#M76355</guid>
      <dc:creator>vincentvanouden</dc:creator>
      <dc:date>2018-04-02T01:57:54Z</dc:date>
    </item>
  </channel>
</rss>

