Home > Blockchain >  how do you map java localdatetime to avro
how do you map java localdatetime to avro

Time:01-27

Im looking at understanding how we map localdatetime in java to avro.

is correct to use

   {
      "name": "example",
      "type": ["null", {"type": "int", "logicalType": "timestamp-millis"}],
      "default": null
    }

How do i set this on my avro object..

LocalDateTime localDateTime = LocalDateTime.now();
long epochMillis = localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli();

should avro not be defined as a Long?

CodePudding user response:

int can't store epoch millis; you need long.

Compare these values:

        1,674,123,456,789 // current epoch millis (approx)
            2,147,483,647 // largest int value
9,223,372,036,854,775,807 // largest long value

Define the avro field as long.

CodePudding user response:

As the spec says

A time-millis logical type annotates an Avro int, where the int stores the number of milliseconds after midnight, 00:00:00.000.

So, try casting

int epochMillis = (int) localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli();
avroFoo.setExample(epochMillis);
  • Related