[英]Avro logicalType 'date' with default null value
I need to add a 'null' default value to a logicalType: 'date' in my Avro schema.我需要向我的 Avro 架构中的 logicalType: 'date' 添加一个“空”默认值。 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.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)
...
I tried many ways to configure this 'logicalType' as nullable but could not get it working.我尝试了很多方法来将此“logicalType”配置为可为空,但无法使其正常工作。 How can I configure this field to be nullable?
如何将此字段配置为可为空?
This code worked for me:这段代码对我有用:
{
"name" : "birthday",
"type" : ["null",{
"type" : "int",
"logicalType": "date"
}]
}
Avro does not yet support unions of logical types. Avro 尚不支持逻辑类型的联合。 This is a known outstanding issue: https://issues.apache.org/jira/browse/AVRO-1891
这是一个已知的未决问题: 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.虽然一点也不优雅,但我处理这个问题的方法是使用一个标记值,例如 1900-01-01 来表示 null。
-- Update -- -- 更新 --
This issue seems to be fixed as of version 1.9.0这个问题似乎从 1.9.0 版开始修复
declare it as:将其声明为:
{
"name": "myOptionalDate",
"type": ["null","int"],
"logicalType": "date",
"default" : "null"
}
this should work这应该有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.