Search code examples
nullavro

Avro ENUM field


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).


Solution

  • 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"]
                    }
                ]
            }
        ]
    }