4

This question/answer (How to nest records in an Avro schema?) clears things up on how to nest complex types (records in this case). However, I'm wondering if anyone knows how to have default values for record types. I'm getting errors when in the example shown in the above question, the address is missing and I'd prefer avro to default it to an empty dictionary or at the very least to an empty String - instead of me having to default it in advance.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Sokratis
  • 85
  • 2
  • 7

1 Answers1

4

You can only have null value as default value for nested objects in AVRO. This is how looks like the schema with default null value

{
    "name": "person",
    "type": "record",
    "fields": [
        {"name": "firstname", "type": "string"},
        {"name": "lastname", "type": "string"},
        {
            "name": "address",
            "type": ["null" , {
                        "type" : "record",
                        "name" : "AddressUSRecord",
                        "fields" : [
                            {"name": "streetaddress", "type": "string"},
                            {"name": "city", "type": "string"}
                        ]
                    }],
            "default": null
        }
    ]
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
hlagos
  • 7,690
  • 3
  • 23
  • 41
  • Thanks! So in the example you posted, serialization won't fail if for example, "address" is missing. Will it fail if "streetaddress" is present but "city" is missing? – Sokratis May 01 '18 at 14:52
  • yes it will fail, you need to add the union for null and default value if you want to make it work like that. At the end of the day, your schema should specify what could be missing and what not. – hlagos May 01 '18 at 15:50
  • Thanks a lot @hlagos !! Works as expected! – Sokratis May 01 '18 at 20:08