具有默认空值的Avro logicalType“date”

时间:2018-03-19 19:38:27

标签: java apache-kafka schema avro

我需要在我的Avro架构中为logicalType:'date'添加一个'null'默认值。我现在的定义是这样的:

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

当我使用org.joda.time.LocalDate填充'birthday'字段时,它确实有效,但当我离开时null我得到以下异常:

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)
...

我尝试了很多方法将这个'logicalType'配置为可空,但无法使其正常工作。如何将此字段配置为可为空?

3 个答案:

答案 0 :(得分:3)

Avro尚不支持逻辑类型的联合。这是一个众所周知的突出问题:https://issues.apache.org/jira/browse/AVRO-1891

虽然不是很优雅,但我处理此问题的方法是使用诸如1900-01-01之类的标记值来表示null。

答案 1 :(得分:0)

此代码对我有用:

{
    "name" : "birthday",
    "type" : ["null",{
      "type" : "int",
      "logicalType": "date"
    }]
}

答案 2 :(得分:-1)

声明为:

{
  "name": "myOptionalDate",
  "type": ["null","int"],
  "logicalType": "date",
  "default" : "null" 
}

这应该有效