gpt4 book ai didi

java - Avro 逻辑类型 'date',默认为空值

转载 作者:行者123 更新时间:2023-12-02 03:27:58 25 4
gpt4 key购买 nike

我需要在 Avro 架构中向逻辑类型:“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)
...

我尝试了多种方法将此“逻辑类型”配置为可为空,但无法使其正常工作。如何将此字段配置为可为空?

最佳答案

这段代码对我有用:

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

关于java - Avro 逻辑类型 'date',默认为空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49371083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com