'How can ı convert timestamp to datetime in Apache Nifi
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 "-".
Solution 1:[1]
Use a ScriptedTransformRecord processor:
Record Reader:JsonTreeReaderRecord Writer:JsonRecordSetWriterScript Language:GroovyScript 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"
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tonykoval |
