avro default value for boolean not applied
Asked Answered
L

1

7

I have this avro schema in which I have capture declared as boolean and I wanted to give default value as 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. What do I need to do to default it to true?

Locket answered 19/3, 2020 at 6:57 Comment(0)
O
5

This is not just related to Java. In general ( as per documentation available at https://avro.apache.org/docs/current/spec.html ) we have:

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 ). So the record you get reading contains the default value of schema A2 for that variable.


To test I've produced two Java classes from a schema respectively with a boolean field with default value as "true" and "false". Generated classes are identical except for Avro schema definition. This confirm that default value is not reflected on source code.

I've also tried with Golang and the result is the same.

Organelle answered 20/3, 2020 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.