<?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: putAttribute which is generated from Nifi ExecuteScript process method in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/putAttribute-which-is-generated-from-Nifi-ExecuteScript/m-p/177070#M82865</link>
    <description>&lt;P&gt;I believe your "total" variable is local to the method, so you won't be able to refer to it later. Try changing the references to "self.total", that will put the variable as a member of the class instance, so you can get at it later with ConvertFilesData.total. Also note that putAttribute() expects a String for the value of the attribute, so you'll need str(ConvertFilesData.total) there.&lt;/P&gt;</description>
    <pubDate>Thu, 30 Aug 2018 00:59:35 GMT</pubDate>
    <dc:creator>mburgess</dc:creator>
    <dc:date>2018-08-30T00:59:35Z</dc:date>
    <item>
      <title>putAttribute which is generated from Nifi ExecuteScript process method</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/putAttribute-which-is-generated-from-Nifi-ExecuteScript/m-p/177069#M82864</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am having a requirement where user need to use execute script processor with python language to get the summation of all values from content file and summation result should add as new attribute to flow file, I am able to get the sum, But i am not getting how to pass newly created attribute to session for posting the attribute. Below is my code snippet.&lt;/P&gt;&lt;PRE&gt;import traceback
from org.apache.nifi.processors.script import ExecuteScript
from org.apache.nifi.processor.io import StreamCallback
from java.io import BufferedReader, InputStreamReader, OutputStreamWriter
class ConvertFiles(StreamCallback) :
    def __init__(self) :
        pass
    def process(self, inputStream, outputStream) :
        try :
            reader = InputStreamReader(inputStream,"UTF-8")
            bufferedReader = BufferedReader(reader)
            writer = OutputStreamWriter(outputStream,"UTF-8")
            line = bufferedReader.readLine()
            while line != None:
                total = 0
                ChangedRec = line.upper()
                writer.write(ChangedRec)
                writer.write('\n')
                a=line.split(",")
                for valu in a:
                    b=valu.strip()
                    total += int(b)
                line = bufferedReader.readLine()
            print("Summation of Records are %s ",total)
            writer.flush()
            writer.close()
            reader.close()
            bufferedReader.close()
        except :
            print "Exception in Reader:"
            print '-' * 60
            traceback.print_exc(file=sys.stdout)
            print '-' * 60
            raise
            session.transfer(flowFile, ExecuteScript.REL_FAILURE)
        finally :
            if bufferedReader is not None :
                bufferedReader.close()
            if reader is not None :
                reader.close()
flowFile = session.get()
if flowFile is not None :
    ConvertFilesData = ConvertFiles()
    session.write(flowFile, ConvertFilesData)
    flowFile = session.putAttribute(flowFile, "FileSum",ConvertFilesData.total)
    session.transfer(flowFile, ExecuteScript.REL_SUCCESS)
&lt;/PRE&gt;&lt;P&gt;Now I should get Total as value and attribute name as SumResult, Could you please help me to get the issue resolved.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 19:57:21 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/putAttribute-which-is-generated-from-Nifi-ExecuteScript/m-p/177069#M82864</guid>
      <dc:creator>mvenkat1727</dc:creator>
      <dc:date>2018-08-29T19:57:21Z</dc:date>
    </item>
    <item>
      <title>Re: putAttribute which is generated from Nifi ExecuteScript process method</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/putAttribute-which-is-generated-from-Nifi-ExecuteScript/m-p/177070#M82865</link>
      <description>&lt;P&gt;I believe your "total" variable is local to the method, so you won't be able to refer to it later. Try changing the references to "self.total", that will put the variable as a member of the class instance, so you can get at it later with ConvertFilesData.total. Also note that putAttribute() expects a String for the value of the attribute, so you'll need str(ConvertFilesData.total) there.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2018 00:59:35 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/putAttribute-which-is-generated-from-Nifi-ExecuteScript/m-p/177070#M82865</guid>
      <dc:creator>mburgess</dc:creator>
      <dc:date>2018-08-30T00:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: putAttribute which is generated from Nifi ExecuteScript process method</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/putAttribute-which-is-generated-from-Nifi-ExecuteScript/m-p/177071#M82866</link>
      <description>&lt;P&gt;Thanks a lot &lt;A rel="user" href="https://community.cloudera.com/users/641/mburgess.html" nodeid="641" target="_blank"&gt;@Matt Burgess&lt;/A&gt; , By adding reference self to the variable as 'self.total', It did miracle. code snippet is working absolutely fine as expected. It really helps me a lot.&lt;/P&gt;&lt;PRE&gt;import traceback
from org.apache.nifi.processors.script import ExecuteScript
from org.apache.nifi.processor.io import StreamCallback
from java.io import BufferedReader, InputStreamReader, OutputStreamWriter
class ConvertFiles(StreamCallback) :
    def __init__(self) :
        pass
    def process(self, inputStream, outputStream) :
        try :
            self.total = 0
            reader = InputStreamReader(inputStream,"UTF-8")
            bufferedReader = BufferedReader(reader)
            writer = OutputStreamWriter(outputStream,"UTF-8")
            line = bufferedReader.readLine()
            while line != None:
                ChangedRec = line.upper()
                writer.write(ChangedRec)
                writer.write('\n')
                a=line.split(",")
                for valu in a:
                    b=valu.strip()
                    self.total += int(b)
                line = bufferedReader.readLine()
            print("Summation of Records are %s ",self.total)
            writer.flush()
            writer.close()
            reader.close()
            bufferedReader.close()
        except :
            print "Exception in Reader:"
            print '-' * 60
            traceback.print_exc(file=sys.stdout)
            print '-' * 60
            raise
            session.transfer(flowFile, ExecuteScript.REL_FAILURE)
        finally :
            if bufferedReader is not None :
                bufferedReader.close()
            if reader is not None :
                reader.close()
flowFile = session.get()
if flowFile is not None :
    ConvertFilesData = ConvertFiles()
    session.write(flowFile, ConvertFilesData)
    flowFile = session.putAttribute(flowFile, "FileSum",str(ConvertFilesData.total))
    session.transfer(flowFile, ExecuteScript.REL_SUCCESS)&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;Snapshot Result:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="88399-flowfile.png" style="width: 327px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/19609iC5219929126D88BD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="88399-flowfile.png" alt="88399-flowfile.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Aug 2019 09:47:31 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/putAttribute-which-is-generated-from-Nifi-ExecuteScript/m-p/177071#M82866</guid>
      <dc:creator>mvenkat1727</dc:creator>
      <dc:date>2019-08-18T09:47:31Z</dc:date>
    </item>
  </channel>
</rss>

