<?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: Groovy ExcuteScript example ? in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Groovy-ExcuteScript-example/m-p/123998#M47232</link>
    <description>&lt;P&gt;Heres an example of groovy class in ExecuteScript processor, treat it like Java and use Matt's nifi-script-tester utility to debug your code &lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/dbist/workshops/blob/master/nifi/templates/zabbix/ZabbixSender.groovy#L36-L56" target="_blank"&gt;https://github.com/dbist/workshops/blob/master/nifi/templates/zabbix/ZabbixSender.groovy#L36-L56&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 28 Nov 2016 02:38:58 GMT</pubDate>
    <dc:creator>aervits</dc:creator>
    <dc:date>2016-11-28T02:38:58Z</dc:date>
    <item>
      <title>Groovy ExcuteScript example ?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Groovy-ExcuteScript-example/m-p/123997#M47231</link>
      <description>&lt;P&gt;Hello forum,&lt;/P&gt;&lt;P&gt;I got the below example from funnifi blog, thanks to Matt &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I have 2 questions on this ?&lt;/P&gt;&lt;P&gt;1- Groovy supposed to be Java like language, classes and methods... but I can't see this in the below example ? no class or method declaration ?&lt;/P&gt;&lt;P&gt;2- If I want to add conditions to the code and modify the output based on this.&lt;/P&gt;&lt;P&gt;e.g. &lt;/P&gt;&lt;P&gt;if field 3 (column C) starts with 3300, replace 33 with 22&lt;/P&gt;&lt;P&gt;if field 3 (column C) starts with 0, replace this 0 with 00212&lt;/P&gt;&lt;P&gt;how to achieve this ?&lt;/P&gt;&lt;P&gt;Input file:&lt;/P&gt;&lt;PRE&gt;a1|b1|c1|d1
a2|b2|c2|d2
a3|b3|c3|d3&lt;/PRE&gt;&lt;P&gt;Desired output:&lt;/P&gt;&lt;PRE&gt;b1 c1
b2 c2
b3 c3&lt;/PRE&gt;&lt;P&gt;script:&lt;/P&gt;&lt;PRE&gt;import java.nio.charset.StandardCharsets

def flowFile = session.get()
if(!flowFile) return

flowFile = session.write(flowFile, {inputStream, outputStream -&amp;gt;
   inputStream.eachLine { line -&amp;gt;
   a = line.tokenize('|')
   outputStream.write("${a[1]} ${a[2]}\n".toString().getBytes(StandardCharsets.UTF_8))
   }
} as StreamCallback)

session.transfer(flowFile, REL_SUCCESS)&lt;/PRE&gt;</description>
      <pubDate>Sun, 27 Nov 2016 16:56:18 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Groovy-ExcuteScript-example/m-p/123997#M47231</guid>
      <dc:creator>yahya_najjar</dc:creator>
      <dc:date>2016-11-27T16:56:18Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy ExcuteScript example ?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Groovy-ExcuteScript-example/m-p/123998#M47232</link>
      <description>&lt;P&gt;Heres an example of groovy class in ExecuteScript processor, treat it like Java and use Matt's nifi-script-tester utility to debug your code &lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/dbist/workshops/blob/master/nifi/templates/zabbix/ZabbixSender.groovy#L36-L56" target="_blank"&gt;https://github.com/dbist/workshops/blob/master/nifi/templates/zabbix/ZabbixSender.groovy#L36-L56&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Nov 2016 02:38:58 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Groovy-ExcuteScript-example/m-p/123998#M47232</guid>
      <dc:creator>aervits</dc:creator>
      <dc:date>2016-11-28T02:38:58Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy ExcuteScript example ?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Groovy-ExcuteScript-example/m-p/123999#M47233</link>
      <description>&lt;P&gt;thank you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Nov 2016 03:29:09 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Groovy-ExcuteScript-example/m-p/123999#M47233</guid>
      <dc:creator>yahya_najjar</dc:creator>
      <dc:date>2016-11-28T03:29:09Z</dc:date>
    </item>
    <item>
      <title>Re: Groovy ExcuteScript example ?</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Groovy-ExcuteScript-example/m-p/124000#M47234</link>
      <description>&lt;P&gt;To follow up on your first question above, Groovy has many features and supports multiple paradigms such as OO (like Java), functional programming, etc. Also it can be used as a scripting language, which is why you don't often see explicitly-defined classes and methods in the example scripts (both in &lt;A rel="user" href="https://community.cloudera.com/users/393/aervits.html" nodeid="393"&gt;@Artem Ervits&lt;/A&gt; answer and the examples on &lt;A target="_blank" href="http://funnifi.blogspot.com/"&gt;my blog&lt;/A&gt;).&lt;/P&gt;&lt;P&gt;For ExecuteScript, the Script Body is treated like a script, so it can be evaluated without needing to define a top-level class. Under the hood, Groovy wraps the script in a &lt;A target="_blank" href="http://docs.groovy-lang.org/latest/html/gapi/groovy/lang/Script.html"&gt;Script&lt;/A&gt; object (such that it obeys the JVM rules).&lt;/P&gt;&lt;P&gt;For your second question, you can add additional logic inside the inputStream.eachLine closure to do any transformations. So instead of just writing out a[1] a[2] after you've tokenized the input stream, you can do additional things such as:&lt;/P&gt;&lt;PRE&gt;if(a[2].startsWith('3300')) a[2] = a[2].replaceFirst('33','22')
if(a[2].startsWith('0')) a[2] = a[2].replaceFirst('0','00212')&lt;/PRE&gt;&lt;P&gt;Then you can output the space-separated columns.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Nov 2016 21:54:19 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Groovy-ExcuteScript-example/m-p/124000#M47234</guid>
      <dc:creator>mburgess</dc:creator>
      <dc:date>2016-11-28T21:54:19Z</dc:date>
    </item>
  </channel>
</rss>

