How to serialize a date using AVRO in Java

I'm actually trying to serialize date objects with Avro, and the deserialized date does not match the expected value (checked with avro 1.7.2 and 1.7.1). Here is the class I'm serializing:

import java.text.SimpleDateFormat; import java.util.Date; public class Dummy { private Date date; private SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS"); public Dummy() { } public void setDate(Date date) { this.date = date; } public Date getDate() { return date; } @Override public String toString() { return df.format(date); } } 

Code used for serialization / deserialization:

 import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Date; import org.apache.avro.Schema; import org.apache.avro.io.DatumReader; import org.apache.avro.io.DatumWriter; import org.apache.avro.io.Decoder; import org.apache.avro.io.DecoderFactory; import org.apache.avro.io.Encoder; import org.apache.avro.io.EncoderFactory; import org.apache.avro.reflect.ReflectData; import org.apache.avro.reflect.ReflectDatumReader; import org.apache.avro.reflect.ReflectDatumWriter; public class AvroSerialization { public static void main(String[] args) { Dummy expected = new Dummy(); expected.setDate(new Date()); System.out.println("EXPECTED: " + expected); Schema schema = ReflectData.get().getSchema(Dummy.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Encoder encoder = EncoderFactory.get().binaryEncoder(baos, null); DatumWriter<Dummy> writer = new ReflectDatumWriter<Dummy>(schema); try { writer.write(expected, encoder); encoder.flush(); Decoder decoder = DecoderFactory.get().binaryDecoder(baos.toByteArray(), null); DatumReader<Dummy> reader = new ReflectDatumReader<Dummy>(schema); Dummy actual = reader.read(null, decoder); System.out.println("ACTUAL: " + actual); } catch (IOException e) { System.err.println("IOException: " + e.getMessage()); } } } 

And the conclusion:

 EXPECTED: 06/11/2012 05:43:29.188 ACTUAL: 06/11/2012 05:43:29.387 

Is this due to a known bug, or is it related to how I serialize the object?

+8
java datetime serialization avro
source share
2 answers

I think AVRO is not serializing the date at the moment. I would do this to wrap it in another class and save it as long (date.gettime ()), while the Auro people add this function . And the reason you see different Date values ​​is because every time you (and avro) create a Date object, it initializes the date with the current system time.

+6
source share

Avro 1.8 now has a "logicalType" date that annotates an int. For example:

{"name": "date", "type": "int", "logicalType": "date"}

Specification citation: β€œThe logical date type annotates the value of Avro int, where int stores the number of days since the unix era, January 1, 1970 (ISO calendar).

+7
source share

All Articles