简体   繁体   English

未应用布尔值的 avro 默认值

[英]avro default value for boolean not applied

I have this avro schema in which I have capture declared as boolean and I wanted to give default value as true .我有这个 avro 模式,其中我将捕获声明为 boolean ,我想将默认值设置为true

{
    "namespace": "abc",
    "type": "record",
    "name": "Request",
    "fields": [
        {
            "name": "amount",
            "type": "long",
        },
        {
            "name": "currency",
            "type": ["string", "null"],
        },
        {
            "name": "capture",
            "type": "boolean",
            "default": true
        } 
}

When this is used in Java, it gets default value null .当它在 Java 中使用时,它获得默认值null What do I need to do to default it to true ?我需要做什么才能将其默认为true

This is not just related to Java.这不仅仅与Java有关。 In general ( as per documentation available at https://avro.apache.org/docs/current/spec.html ) we have:一般来说(根据https://avro.apache.org/docs/current/spec.html提供的文档)我们有:

default: A default value for this field, used when reading instances that lack this field (optional)

So the default value is used only on schema evolution and so when you read/write records using different schemas.因此默认值仅用于模式演变,因此当您使用不同的模式读/写记录时。 For example suppose you want to read a record written with schema A1 using instead the evolved schema A2 and that the capture field and has been introduced with schema A2 ( so it didn't exist at schema A1 ).例如,假设您想读取使用模式 A1 编写的记录,而不是使用进化模式 A2 和捕获字段,并且已通过模式 A2 引入(因此它不存在于模式 A1 中)。 So the record you get reading contains the default value of schema A2 for that variable.因此,您读取的记录包含该变量的模式 A2 的默认值。


To test I've produced two Java classes from a schema respectively with a boolean field with default value as "true" and "false".为了进行测试,我分别从一个模式中生成了两个Java类,其中一个布尔字段的默认值为“true”和“false”。 Generated classes are identical except for Avro schema definition.除了 Avro 模式定义之外,生成的类是相同的 This confirm that default value is not reflected on source code.这确认默认值未反映在源代码中。

I've also tried with Golang and the result is the same.我也试过Golang ,结果是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM