Empty list as default value in AVRO array
Asked Answered
H

3

8

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.

Hacksaw answered 10/1, 2017 at 9:52 Comment(0)
A
11

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.

Anglim answered 9/2, 2017 at 15:7 Comment(0)
A
11

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": []
}
Athalia answered 5/3, 2018 at 19:6 Comment(0)
S
2

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": []
    }
  ]
}
Selfdelusion answered 25/4, 2022 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.