简体   繁体   English

Avro 逻辑类型“日期”,默认为空值

[英]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.

相关问题 Avro 消息 - InvalidNumberEncodingException 反序列化逻辑类型日期 - Avro Message - InvalidNumberEncodingException deserializing logicalType date Avro logicalType String 日期转换为 EPOCH 时间戳-mils - Avro logicalType String Date conversion to EPOCH timestamp-milis org.apache.avro.UnresolvedUnionException:不在联合中[{“type”:“bytes”,“logicalType”:“decimal”,“precision”:18,“scale”:4},“null”]:0.0000 - org.apache.avro.UnresolvedUnionException: Not in union [{“type”:“bytes”,“logicalType”:“decimal”,“precision”:18,“scale”:4},“null”]: 0.0000 在 Java Beam 管道中的日期/时间戳上使用 LogicalType 'timestamp-millis' 编写 avro 文件 - Write avro files with LogicalType 'timestamp-millis' on date/timestamps in Java Beam pipeline Avro - 如何为特定编译器注册自定义 LogicalType - Avro - how to register custom LogicalType for SpecificCompiler 如何在Avro中定义LogicalType。 (Java)的 - How to define a LogicalType in Avro. (java) 未应用布尔值的 avro 默认值 - avro default value for boolean not applied Avro 数组默认为 null 而不是空数组 - Avro array default is null and not an empty array 如何使用Java将timestamp-millis logicalType序列化为avro文件 - How to serialize timestamp-millis logicalType to avro file using java 无法将null枚举编码为avro枚举 - Trouble encoding avro enum with null default
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM