Avro ENUM field
Asked Answered
V

1

15

I am trying to create Union field in Avro schema and send corresponding JSON message with it but to have one of the fields - null.

https://avro.apache.org/docs/1.8.2/spec.html#Unions

What is example of simplest UNION type (avro schema) with corresponding JSON data? (trying to make example without null/empty data and one with null/empty data).

Vinous answered 11/5, 2018 at 1:32 Comment(0)
M
24

Here you have an example.

Null enum

{"name": "Stephanie", "age": 30, "sex": "female", "myenum": null}

Not null enum

{"name": "Stephanie", "age": 30, "sex": "female", "myenum": "HEARTS"}

Schema

{
    "type": "record",
    "name": "Test",
    "namespace": "com.acme",
    "fields": [{
            "name": "name",
            "type": "string"
        }, {
            "name": "age",
            "type": "int"
        }, {
            "name": "sex",
            "type": "string"
        }, {
            "name": "myenum",
            "type": ["null", {
                    "type": "enum",
                    "name": "Suit",
                    "symbols": ["SPADES", "HEARTS", "DIAMONDS", "CLUBS"]
                }
            ]
        }
    ]
}
Mary answered 11/5, 2018 at 14:44 Comment(4)
This doesn't work for me. I have this error: "Failed to convert JSON to Avro: Expected start-union. Got VALUE_STRING" Avro version 1.9.2Woolfell
@vanillaSugar, ask in a different question adding details and codeMary
I guess this example does not work because of the namespace that is defined in the Schema... The "not null" message should be something like : {"name": "Stephanie", "age": 30, "sex": "female", "myenum": {"com.acme.Suit": "HEARTS" }}Reni
Yeah, I got the same issue. if I put "com.acme.Suit" it will work, but doesn't work for "myenum": "HEARTS". Do you know why?Kreit

© 2022 - 2024 — McMap. All rights reserved.