Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avro logicalType 'date' with default null value

I need to add a 'null' default value to a logicalType: 'date' in my Avro schema. The current definition I have is like this:

{
 "type": "record",
 "namespace": "my.model.package",
 "name": "Person",
 "version": "1",
 "fields": [
    {"name":"birthday","type": { "type": "int", "logicalType": "date"}}
 ]
}

When I populate 'birthday' field with a org.joda.time.LocalDate it does work but when I do leave it null I get the following exception:

org.apache.kafka.common.errors.SerializationException: Error serializing Avro message
Caused by: java.lang.NullPointerException: null of int of my.model.package.Person
at org.apache.avro.generic.GenericDatumWriter.npe(GenericDatumWriter.java:145)
at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:139)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:75)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:62)
at io.confluent.kafka.serializers.AbstractKafkaAvroSerializer.serializeImpl(AbstractKafkaAvroSerializer.java:92)
at io.confluent.kafka.serializers.KafkaAvroSerializer.serialize(KafkaAvroSerializer.java:53)
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:459)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:436)
...

I tried many ways to configure this 'logicalType' as nullable but could not get it working. How can I configure this field to be nullable?

like image 884
djeison Avatar asked Mar 19 '18 19:03

djeison


2 Answers

This code worked for me:

{
    "name" : "birthday",
    "type" : ["null",{
      "type" : "int",
      "logicalType": "date"
    }]
}
like image 171
Moqichenle Avatar answered Nov 03 '22 00:11

Moqichenle


Avro does not yet support unions of logical types. This is a known outstanding issue: https://issues.apache.org/jira/browse/AVRO-1891

While not at all elegant, the way I have handled this is to use a sentinel value such as 1900-01-01 to represent null.

-- Update --

This issue seems to be fixed as of version 1.9.0

like image 25
erik Avatar answered Nov 02 '22 23:11

erik