Created on 10-18-2017 11:18 AM - edited 08-17-2019 05:49 PM
Hi All,
I have json flow file with values in Mon Oct 09 23:38:55 IST 2017 format.
I'm trying to change format to '2017-09-11 19:56:13'.
to accomplish this I selected ExecuteScript Processor below is my groovy script which is not working.
Please give me any suggestion on this.
Thanks
Rakesh.
import org.apache.commons.io.IOUtils import java.nio.charset.* def flowFile = session.get() if(!flowFile) return flowFile = session.write(flowFile, {inputStream, outputStream -> def jsonSlurper = new JsonSlurper() def object = jsonSlurper.parseText(flowFile) assert object instanceof Map object.each { key,value -> String Regex = "([a-zA-Z]{3} [a-zA-Z]{3} \d{2} \d{2}\:\d{2}\:\d{2} [a-zA-Z]{3} \d{4})" if($value ==~ Regex){ $value = toDate("EEE MMM dd HH:mm:ss z yyyy"):toNumber():format("yyyy-MM-dd HH:mm:ss.S") } } outputStream.write(object) } as StreamCallback) flowFile = session.putAttribute(flowFile) session.transfer(flowFile, REL_SUCCESS)
Created on 10-18-2017 03:03 PM - edited 08-17-2019 05:49 PM
Hi @rakesh chow, as ExecuteScripts are experimental in NiFi but you can acheive same results by using another processors as follows.
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
EvaluateJsonPath configs:-
My input sample content is
{ "ID":"1", "CID":"1", "DiscoveredTime":"Mon Sep 11 19:56:13 IST 2017", "LastDiscoveredTime":"Mon Oct 09 23:38:55 IST 2017" }
in eval json processor extract all the contents of the flowfile to the attributes of flowfile by changing
Destination property to flowfile-attribute
and add new properties by clicking + sign at right corner.
ID
$.ID
DiscoveredTime
$.DiscoveredTime
CId
$.CId
LastDiscoveredTime
$.LastDiscoveredTime
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.
Config Screenshot:-
UpdateAttribute Processor Configs:-
This processor will helps to update the attributes on the fly we are changing our DiscoveredTime,LastDiscoveredTime attributes on the fly.
add new properties
1.DiscoveredTime
${DiscoveredTime:toDate('EEE MMM dd HH:mm:ss ZZZ yyyy'):toNumber():plus(21600000):format("yyyy-MM-dd HH:mm:ss.SSS")}
in this expression we are checking which timezone it is and converting that to number and adding 6 hrs(i think you don't need to do :plus(21600000)), after that i'm converting to yyyy-MM-dd HH:mm:ss.SSS format
Your expression probably will be
${DiscoveredTime:toDate('EEE MMM dd HH:mm:ss ZZZ yyyy'):toNumber():format("yyyy-MM-dd HH:mm:ss.SSS")}
2. add new property for
LastDiscoveredTime
${LastDiscoveredTime:toDate('EEE MMM dd HH:mm:ss ZZZ yyyy'):toNumber():plus(21600000):format("yyyy-MM-dd HH:mm:ss.SSS")}
Your expression:-
${LastDiscoveredTime:toDate('EEE MMM dd HH:mm:ss ZZZ yyyy'):toNumber():format("yyyy-MM-dd HH:mm:ss.SSS")}
Configs:-
AttributesToJson Processor:-
in Attributes list property add all the available attributes it will prepare a json messages with the attributes that you have mentioned here.
ID,CID,DiscoveredTime,LastDiscoveredTime
Change Destination property to flowfile-content
Configs:-
Input:-
{ "ID":"1", "CID":"1", "DiscoveredTime":"Mon Sep 11 19:56:13 IST 2017", "LastDiscoveredTime":"Mon Oct 09 23:38:55 IST 2017" }
Output:-
{ "ID" : "1", "LastDiscoveredTime" : "2017-10-09 23:38:55.000", "DiscoveredTime" : "2017-09-11 19:56:13.000", "CID" : "" }
In your case you need to mention all your attributes in attributes to json processor and it will prepare new json content with all your attributes, if you left this property as empty then all the attributes associated with the ff will be part of your json content.
Flow Screenshot:-