'Empty list as default value in AVRO array
What I have in my Schema.avdl:
array<string> names = null;
Now instead of null, I want to have the default value as an empty array, so i tried and failed:
array<string> names = []; and array<string> names = {};
Can someone show me how to put an empty list as the default value ?
Thank you.
Solution 1:[1]
I'm not sure if this directly helps you, but in an avro schema declaration (.avsc), you could write the following:
{
     "type": "record",
     "namespace": "my.avro.schemas",
     "name": "Schema",
     "fields": [ {
          "name": "string_arr",
          "type": {
              "type": "array", 
              "items": "string"
          },
          "default": []
     }]
}
Note that the "default" field definition is an empty json array. Using the Builder of the parsed avro Schema class would fill the "string_arr" field with an empty array per default.
Solution 2:[2]
array<string> names = []; works in Avro 1.8.2. This will generate the following field in JSON:
{
  "name": "names",
  "type": {
    "type": "array",
    "items": "string"
  },
  "default": []
}
Solution 3:[3]
Does it work for the array of records? As I have the following schema and it doesn't initialize the array, it gets null
{
  "namespace": "com.turkogluc",
  "type": "record",
  "name": "OverSpeedingAgg",
  "fields": [
    {
      "name": "driverId",
      "type": "long"
    },
    {
      "name": "incidents",
      "type": {
        "type": "array",
        "items": "com.turkogluc.VehicleLocationUpdate"
      },
      "default": []
    }
  ]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Jahir | 
| Solution 2 | blachniet | 
| Solution 3 | turkogluc | 
