avro logicaltype'date',默认值为空

f5emj3cl  于 2021-06-07  发布在  Kafka
关注(0)|答案(3)|浏览(551)

我需要向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 它确实有用,但当我离开它的时候 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”配置为可空,但无法使其工作。如何将此字段配置为可为空?

mv1qrgav

mv1qrgav1#

这个代码对我有用:

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

8ulbf1ek2#

声明如下:

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

这应该管用

cetgtptt

cetgtptt3#

avro还不支持逻辑类型的联合。这是一个已知的未决问题:https://issues.apache.org/jira/browse/avro-1891
虽然一点也不优雅,但我处理这个问题的方法是使用一个sentinel值,比如1900-01-01来表示null。
--更新--
从版本1.9.0开始,这个问题似乎已得到修复

相关问题