Reputation: 500
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?
Upvotes: 5
Views: 15289
Reputation: 76
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
Upvotes: 6
Reputation: 181
This code worked for me:
{
"name" : "birthday",
"type" : ["null",{
"type" : "int",
"logicalType": "date"
}]
}
Upvotes: 17
Reputation: 1
declare it as:
{
"name": "myOptionalDate",
"type": ["null","int"],
"logicalType": "date",
"default" : "null"
}
this should work
Upvotes: -4