Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty list as default value in AVRO array

Tags:

avro

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.

like image 999
Xitrum Avatar asked Jan 10 '17 09:01

Xitrum


People also ask

What is default in Avro schema?

Default Values and Logical Types Default Values is one of the use case of Union where we can have multiple field value to take different types. And in default every field in avro schema are not nullable. Example : Making middle_name as nullable { "name": "middle_name", "type": ["null", "string"], "default": null }

Does Avro support date data type?

The date logical type annotates the Avro integer primitive type. The integer type stores the number of days since midnight January 1, 1970 UTC. Load values using the date logical type into target columns using the following Vertica data types: DATE.

What is Avro schema?

Avro schema definitions are JSON records. Because it is a record, it can define multiple fields which are organized in a JSON array. Each such field identifies the field's name as well as its type. The type can be something simple, like an integer, or something complex, like another record.


2 Answers

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.

like image 193
Constantin Gaul Avatar answered Sep 28 '22 02:09

Constantin Gaul


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": []
}
like image 34
blachniet Avatar answered Sep 28 '22 00:09

blachniet