<?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 Execute Script ProcessSession.read has not been closed in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Execute-Script-ProcessSession-read-has-not-been-closed/m-p/371812#M241094</link>
    <description>&lt;P&gt;Hi, I am trying ro read from a json file and create flowfiles with the kews as attributes and one flowfile per value in the list.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The json file looks something like this:&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;"key1": null,&lt;BR /&gt;"key2": null,&lt;BR /&gt;"list": ["foo", "bar"],&lt;BR /&gt;"key3": 1&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my execute script:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&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
from org.apache.nifi.processor.io import InputStreamCallback

flowFile = session.get() 
if (flowFile != None):
    
    stream_content = session.read(flowFile)
    text_content = IOUtils.toString(stream_content, StandardCharsets.UTF_8)
    json_data = json.loads(text_content)
	
    
    for i in json_data["list"]:
        
        flowFile2 = session.create()
        flowFile2 = session.putAttribute(flowFile2, "key3", str(json_data["key3"]))
        flowFile2 = session.putAttribute(flowFile2, "key1", json_data["key1"])
        flowFile2 = session.putAttribute(flowFile2, "list_value", i)
    
        session.transfer(flowFile2, REL_SUCCESS)

    session.remove(flowFile)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I keep getting an error that I need to close the process session first because my original flowfile is still in use. How can I iterate over the list to create multiple flowfiles properly? I couldn't find any similar case or example of this sort. Thanks for tips and help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: So I changed my code to create a dictionnary in order to use a new variable name for each flowfile created, but now I keep getting a key error when I want to transfer them to the success relationship.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&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
from org.apache.nifi.processor.io import InputStreamCallback

flowFile = session.get() 
if (flowFile != None):

	stream_content = session.read(flowFile)
	text_content = IOUtils.toString(stream_content, StandardCharsets.UTF_8)
	json_data = json.loads(text_content)
	
	d = {}
	for x in range(len(json_data["list"])):
		d["flowfile{0}".format(x)] = json_data['list'][x]
	
	for file in d:
		file = session.create()
		file = session.putAttribute(file, "key3", str(json_data["key3"]))
		file = session.putAttribute(file, "key2", json_data["key2"])
		file = session.putAttribute(file, "list_value", d[file])
		session.transfer(file, REL_SUCCESS)
	
session.remove(flowFile)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 31 May 2023 10:16:38 GMT</pubDate>
    <dc:creator>Fredi</dc:creator>
    <dc:date>2023-05-31T10:16:38Z</dc:date>
    <item>
      <title>Execute Script ProcessSession.read has not been closed</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Execute-Script-ProcessSession-read-has-not-been-closed/m-p/371812#M241094</link>
      <description>&lt;P&gt;Hi, I am trying ro read from a json file and create flowfiles with the kews as attributes and one flowfile per value in the list.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The json file looks something like this:&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;"key1": null,&lt;BR /&gt;"key2": null,&lt;BR /&gt;"list": ["foo", "bar"],&lt;BR /&gt;"key3": 1&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my execute script:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&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
from org.apache.nifi.processor.io import InputStreamCallback

flowFile = session.get() 
if (flowFile != None):
    
    stream_content = session.read(flowFile)
    text_content = IOUtils.toString(stream_content, StandardCharsets.UTF_8)
    json_data = json.loads(text_content)
	
    
    for i in json_data["list"]:
        
        flowFile2 = session.create()
        flowFile2 = session.putAttribute(flowFile2, "key3", str(json_data["key3"]))
        flowFile2 = session.putAttribute(flowFile2, "key1", json_data["key1"])
        flowFile2 = session.putAttribute(flowFile2, "list_value", i)
    
        session.transfer(flowFile2, REL_SUCCESS)

    session.remove(flowFile)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I keep getting an error that I need to close the process session first because my original flowfile is still in use. How can I iterate over the list to create multiple flowfiles properly? I couldn't find any similar case or example of this sort. Thanks for tips and help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: So I changed my code to create a dictionnary in order to use a new variable name for each flowfile created, but now I keep getting a key error when I want to transfer them to the success relationship.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&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
from org.apache.nifi.processor.io import InputStreamCallback

flowFile = session.get() 
if (flowFile != None):

	stream_content = session.read(flowFile)
	text_content = IOUtils.toString(stream_content, StandardCharsets.UTF_8)
	json_data = json.loads(text_content)
	
	d = {}
	for x in range(len(json_data["list"])):
		d["flowfile{0}".format(x)] = json_data['list'][x]
	
	for file in d:
		file = session.create()
		file = session.putAttribute(file, "key3", str(json_data["key3"]))
		file = session.putAttribute(file, "key2", json_data["key2"])
		file = session.putAttribute(file, "list_value", d[file])
		session.transfer(file, REL_SUCCESS)
	
session.remove(flowFile)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 10:16:38 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Execute-Script-ProcessSession-read-has-not-been-closed/m-p/371812#M241094</guid>
      <dc:creator>Fredi</dc:creator>
      <dc:date>2023-05-31T10:16:38Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Script ProcessSession.read has not been closed</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Execute-Script-ProcessSession-read-has-not-been-closed/m-p/371834#M241098</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/100919"&gt;@Fredi&lt;/a&gt;,&lt;BR /&gt;&lt;BR /&gt;I am not an expert when it comes to using ExecuteScript in NiFi, as I mostly go for ExecuteStreamCommand, but I really recommend your to have a look on the following two links (until somebody with far more experience provides you with an answer), as they explain everything you need to know when it comes to executing a Jython script in NiFi:&lt;BR /&gt;&lt;BR /&gt;See Part 2 for I/O on FlowFiles: &lt;A href="https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018" target="_blank"&gt;https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;See Part 1 for FlowFile Creation: &lt;A href="https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-1/ta-p/248922" target="_blank"&gt;https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-1/ta-p/248922&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;If I were to look at your code and the examples provided by &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/38301"&gt;@mburgess&lt;/a&gt;, you are missing some important steps and that might be the cause of your error.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 10:12:07 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Execute-Script-ProcessSession-read-has-not-been-closed/m-p/371834#M241098</guid>
      <dc:creator>cotopaul</dc:creator>
      <dc:date>2023-05-31T10:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Script ProcessSession.read has not been closed</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Execute-Script-ProcessSession-read-has-not-been-closed/m-p/371835#M241099</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/103151"&gt;@cotopaul&lt;/a&gt;, thanks for your remark. I have seen the cookbooks, but there don't seem to be examples for creating multiple flowfiles from one incoming flowfile. As you can see from my edit I have advanced a bit, Also, I don't even want to generate content (I do that later with other processors). I just want to create flowfiles with some custom attributes with different values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 10:26:30 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Execute-Script-ProcessSession-read-has-not-been-closed/m-p/371835#M241099</guid>
      <dc:creator>Fredi</dc:creator>
      <dc:date>2023-05-31T10:26:30Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Script ProcessSession.read has not been closed</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Execute-Script-ProcessSession-read-has-not-been-closed/m-p/371851#M241101</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/100919"&gt;@Fredi&lt;/a&gt;&amp;nbsp;The solution you are looking for, is to use this line of code in your script during a loop whenever you want to send the current flowfile content.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; session.commit()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;This will send a flowfile out.&amp;nbsp; &amp;nbsp;This is little known, as this command is inferred at the end of the script, assuming 1 execution to one flowfile.&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example from my fraud detection demo, a while statement that does some counts, sends multiple flowfiles of good transactions, then in random iterations during 20 loops, sends some fraud transaction flowfiles.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;# All processing code starts at this indent
while ticks &amp;lt; 20:
 ticks += 1
 fintran = create_fintran()   
 fintransaction =  json.dumps(fintran)
 #send_fintran(out_socket, json.dumps(fintran))
 #print(fintransaction)
 flowFile = session.create()
 flowFile = session.write(flowFile, WriteContentCallback(fintransaction))
 session.transfer(flowFile, REL_SUCCESS)
 session.commit()

 sleep(DELAY)
 if ticks &amp;gt; fraud_tick:
    fraudtran = create_fraudtran(fintran)
    fraudfintransaction=json.dumps(fraudtran)
    #send_fintran(out_socket, json.dumps(fraudtran))
    #print(fraudfintransaction)
    flowFile2 = session.create()
    flowFile2 = session.write(flowFile2, WriteContentCallback(fraudfintransaction))
    session.transfer(flowFile2, REL_SUCCESS)
    session.commit()
    fraud_tick = random.randint(FRAUD_TICK_MIN, FRAUD_TICK_MAX)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 12:35:31 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Execute-Script-ProcessSession-read-has-not-been-closed/m-p/371851#M241101</guid>
      <dc:creator>steven-matison</dc:creator>
      <dc:date>2023-05-31T12:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Script ProcessSession.read has not been closed</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Execute-Script-ProcessSession-read-has-not-been-closed/m-p/371856#M241103</link>
      <description>&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/95503"&gt;@steven-matison&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Thanks man. It is true, the session.commit() method can be found in the abstract processor class, which is why I did not think of adding it. This helped me a lot! Also I needed to close the Inputstream with&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;IOUtils.closeQuietly(stream_content)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thirdly I had to use the enumerate function for the dictionnary, because it couldn't read the line&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;file = session.putAttribute(file, "list_value", d[file])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I just filled the dict with empty values and used session.putAttribute(file, "list_value", json_data['list'][i])&lt;/P&gt;&lt;P&gt;It is ugly, but works at least.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2023 13:20:50 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Execute-Script-ProcessSession-read-has-not-been-closed/m-p/371856#M241103</guid>
      <dc:creator>Fredi</dc:creator>
      <dc:date>2023-05-31T13:20:50Z</dc:date>
    </item>
  </channel>
</rss>

