I want to convert timestamp to datetime.
Here is my JSON input :
{
"preMarket_timestamp": 1646830062,
"regularMarket_timestamp": 1646773204,
"earningsTimestamp": 1643301000,
"earningsTimestampStart": 1651003200,
"earningsTimestampEnd": 1651521600
}
The JSON result i want :
{
"preMarket_timestamp": "2022/03/09 16:09:26",
"regularMarket_timestamp": "2022/03/09 00:00:04",
"earningsTimestamp": "2022/01/27 19:30:00",
"earningsTimestampStart": "2022/04/26 23:00:00",
"earningsTimestampEnd": "2022/05/02 23:00:00"
}
Is there operation to do this convertion or can I do it with Nifi Expression Language?.Im stuck here.
It does not have to be separated with the "/" operator, it can also be "-".
CodePudding user response:
Use a ScriptedTransformRecord processor:
Record Reader
:JsonTreeReader
Record Writer
:JsonRecordSetWriter
Script Language
:Groovy
Script Body
:
import org.apache.nifi.serialization.record.RecordField;
import org.apache.nifi.serialization.record.RecordFieldType;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
def formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
formatter.setTimeZone(TimeZone.getTimeZone("Europe/Istanbul"))
record.setValue("preMarket_timestamp_formatted", formatter.format(new Date(record.getAsLong("preMarket_timestamp")*1000)))
record.setValue("regularMarket_timestamp_formatted", formatter.format(new Date(record.getAsLong("regularMarket_timestamp")*1000)))
record.setValue("earningsTimestamp_formatted", formatter.format(new Date(record.getAsLong("earningsTimestamp")*1000)))
record.setValue("earningsTimestampStart_formatted", formatter.format(new Date(record.getAsLong("earningsTimestampStart")*1000)))
record.setValue("earningsTimestampEnd_formatted", formatter.format(new Date(record.getAsLong("earningsTimestampEnd")*1000)))
return record
Output json:
{
"preMarket_timestamp" : 1646830062,
"regularMarket_timestamp" : 1646773204,
"earningsTimestamp" : 1643301000,
"earningsTimestampStart" : 1651003200,
"earningsTimestampEnd" : 1651521600,
"preMarket_timestamp_formatted" : "2022/03/09 15:47:42",
"regularMarket_timestamp_formatted" : "2022/03/09 00:00:04",
"earningsTimestamp_formatted" : "2022/01/27 19:30:00",
"earningsTimestampStart_formatted" : "2022/04/26 23:00:00",
"earningsTimestampEnd_formatted" : "2022/05/02 23:00:00"
}
CodePudding user response:
This conversion can be done by Nifi Expression Language
but it may or may not be useful for you because you must be constructing new json at the end of this and must be careful about all other properties and their types.
I am going to show for one property. You can add others easily and should consider other non-conversion properties (if exists)
EvaluateJsonPath
UpdateAttribute
add other properties here like below
${preMarket_timestamp:padRight(13, '0'):toNumber():toDate():format("yyyy-MM-dd'T'HH:mm:ss.SSS")}
AttributesToJSON
You can set comma-separated property list to Attributes List
to construct new json form new attributes