<?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 Script in ExecuteScript Processor To Format Date value from flow file in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Groovy-Script-in-ExecuteScript-Processor-To-Format-Date/m-p/230208#M192058</link>
    <description>&lt;P&gt;Hi &lt;A rel="user" href="https://community.cloudera.com/users/45268/rakesh999alla.html" nodeid="45268" target="_blank"&gt;@rakesh chow&lt;/A&gt;, as ExecuteScripts are experimental in NiFi but you can &lt;STRONG&gt;acheive same&lt;/STRONG&gt; &lt;STRONG&gt;results&lt;/STRONG&gt; by using another processors as follows.&lt;/P&gt;&lt;PRE&gt;1.EvaluateJsonPath //Extract all the contents to attributes
2.UpdateAttribute //Update time stamps attributes with new format(yyyy-mm-dd hh...etc)
3.AttributesToJson //Recreate Json content by specifying all the attributes &lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;EvaluateJsonPath configs:-&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;My input sample content is&lt;/P&gt;&lt;PRE&gt;{
"ID":"1",
"CID":"1",
"DiscoveredTime":"Mon Sep 11 19:56:13 IST 2017",
"LastDiscoveredTime":"Mon Oct 09 23:38:55 IST 2017"
}&lt;/PRE&gt;&lt;P&gt;in&lt;STRONG&gt; eval json processor extract all the contents&lt;/STRONG&gt; of the flowfile to the&lt;STRONG&gt; attributes &lt;/STRONG&gt;of flowfile by changing&lt;/P&gt;&lt;P&gt;Destination property to&lt;STRONG&gt; flowfile-attribute&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;and add new properties by clicking + sign at right corner.&lt;/P&gt;&lt;P&gt;ID &lt;/P&gt;&lt;PRE&gt;$.ID&lt;/PRE&gt;&lt;P&gt;DiscoveredTime &lt;/P&gt;&lt;PRE&gt;$.DiscoveredTime&lt;/PRE&gt;&lt;P&gt;CId
&lt;/P&gt;&lt;PRE&gt;$.CId&lt;/PRE&gt;&lt;P&gt;LastDiscoveredTime
&lt;/P&gt;&lt;PRE&gt;$.LastDiscoveredTime&lt;/PRE&gt;&lt;P&gt;so i have extracted all the content of ff as attributes of ff, in your case you need to add all your json content keys of contents here and  this processor keeps all the values of content to attributes.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Config Screenshot:-&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="39854-evaluatejson.png" style="width: 581px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/15036i46BEDEA94DB6C5FE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="39854-evaluatejson.png" alt="39854-evaluatejson.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;UpdateAttribute Processor Configs:-&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;This processor will helps to update the attributes on the fly we are changing our DiscoveredTime,LastDiscoveredTime attributes on the fly.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;add new properties &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;1.DiscoveredTime&lt;/P&gt;&lt;PRE&gt;${DiscoveredTime:toDate('EEE MMM dd HH:mm:ss ZZZ yyyy'):toNumber():plus(21600000):format("yyyy-MM-dd HH:mm:ss.SSS")}&lt;/PRE&gt;&lt;P&gt;in this expression we are checking which timezone it is and converting that to number and adding 6 hrs(&lt;STRONG&gt;i think you don't need to do :plus(21600000&lt;/STRONG&gt;)), after that i'm converting to yyyy-MM-dd HH:mm:ss.SSS format&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Your expression probably will be&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;${DiscoveredTime:toDate('EEE MMM dd HH:mm:ss ZZZ yyyy'):toNumber():format("yyyy-MM-dd HH:mm:ss.SSS")}&lt;/PRE&gt;&lt;P&gt;2. add new property for&lt;STRONG&gt; &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;LastDiscoveredTime&lt;/P&gt;&lt;PRE&gt;${LastDiscoveredTime:toDate('EEE MMM dd HH:mm:ss ZZZ yyyy'):toNumber():plus(21600000):format("yyyy-MM-dd HH:mm:ss.SSS")}&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Your expression:-&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;${LastDiscoveredTime:toDate('EEE MMM dd HH:mm:ss ZZZ yyyy'):toNumber():format("yyyy-MM-dd HH:mm:ss.SSS")}&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Configs:-&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="39858-update-attr.png" style="width: 600px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/15037iDAA2E9439A8258C5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="39858-update-attr.png" alt="39858-update-attr.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;AttributesToJson Processor:-&lt;/P&gt;&lt;P&gt;in Attributes list property add all the available attributes it will prepare a json messages with the attributes that you have mentioned here.&lt;/P&gt;&lt;PRE&gt;ID,CID,DiscoveredTime,LastDiscoveredTime&lt;/PRE&gt;&lt;P&gt;Change &lt;STRONG&gt;Destination property&lt;/STRONG&gt; to&lt;STRONG&gt;&lt;U&gt; &lt;/U&gt;flowfile-content&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Configs:-&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="39859-attributestojson.png" style="width: 616px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/15038iFF6AF954FCBC7A3F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="39859-attributestojson.png" alt="39859-attributestojson.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Input:-&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;{
"ID":"1",
"CID":"1",
"DiscoveredTime":"Mon Sep 11 19:56:13 IST 2017",
"LastDiscoveredTime":"Mon Oct 09 23:38:55 IST 2017"
}&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Output:-&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;{
  "ID" : "1",
  "LastDiscoveredTime" : "2017-10-09 23:38:55.000",
  "DiscoveredTime" : "2017-09-11 19:56:13.000",
  "CID" : ""
}&lt;/PRE&gt;&lt;P&gt;In your case you need to &lt;STRONG&gt;mention all your attributes&lt;/STRONG&gt; in attributes to json processor and it will prepare new json content with all your attributes, if you &lt;STRONG&gt;left this property as empty&lt;/STRONG&gt; then all the attributes associated with the ff will be part of your json content.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Flow Screenshot:-&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="39860-flow.png" style="width: 473px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/15039iF2D28BE6EB9DE685/image-size/medium?v=v2&amp;amp;px=400" role="button" title="39860-flow.png" alt="39860-flow.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 18 Aug 2019 00:49:27 GMT</pubDate>
    <dc:creator>Shu_ashu</dc:creator>
    <dc:date>2019-08-18T00:49:27Z</dc:date>
  </channel>
</rss>

