Search code examples
apache-nifi

Convert unix time to datetime in NiFi


I have a JSON like:

{
  "id": 1
  "create_time" : 1692617669
}

How can i convert this to yyyy-MM-dd HH:mm:ss with GMT+3 timezone with UpdateRecord?

I tried with ${field.value:multiply(1000):format("yyyy-MM-dd HH:mm:ss", "GMT+3")}

enter image description here

Bot got an error:

java.lang.NumberFormatException: For input string: "2023-07-20 16:28:28"

So i need to change data type to string - for this i should create new field. I tried with Record Path Value as Replacement Value Strategy

But got an error + invalid date: enter image description here

Error:

java.lang.NumberFormatException: For input string: "1970-01-20 13:24:19"

How to deal with it?


Solution

  • I tried to replicate the same flow on my end

    Generate Flowfile

    UpdateRecord

    ${field.value:multiply(1000):formatInstant("yyyy/MM/dd HH:mm:ss", "GMT+3")}

    Final Flow

    Final Output

    If you really need the file to be in Avro format then the following properties needs to be set.

    AvroRecordWriter

    {
      "type": "record",
      "name": "CustomRecord",
      "fields": [
        {
          "name": "id",
          "type": "int"
        },
        {
          "name": "create_time",
          "type": {
            "type": "string",
            "logicalType": "timestamp-millis"
          }
        }
      ]
    }
    

    The issue seems to be with the AvroRecordSetWriter probally with the Schema Inheritance.