43

Is it possible to have an optional field in an Avro schema (i.e. the field does not appear at all in the .JSON file)?

In my Avro schema, I have two fields:

{"name": "author", "type": ["null", "string"], "default": null},
{"name": "importance", "type": ["null", "string"], "default": null},

And in my JSON files those two fields can exist or not.

However, when they do not exist, I receive an error (e.g. when I test such a JSON file using avro-tools command line client):

Expected field name not found: author

I understand that as long as the field name exists in a JSON, it can be null, or a string value, but what I'm trying to express is something like "this JSON is valid if the those field names do not exist, OR if they exist and they are null or string".

Is this possible to express in an Avro schema? If so, how?

Emre Sevinç
  • 8,211
  • 14
  • 64
  • 105

2 Answers2

47

you can define the default attribute as undefined example. so the field can be skipped.

{ 
   "name": "first_name",
   "type": "string",
   "default": "undefined"
},

Also all field are manadatory in avro. if you want it to be optional, then union its type with null. example:

{
    "name": "username",
    "type": [
      "null",
      "string"
    ],
    "default": null
},
Banana
  • 2,435
  • 7
  • 34
  • 60
arvin_v_s
  • 1,036
  • 1
  • 12
  • 18
15

According to avro specification this is possible, using the default attribute.

See https://avro.apache.org/docs/1.8.2/spec.html

default: A default value for this field, used when reading instances that lack this field (optional). Permitted values depend on the field's schema type, according to the table below. Default values for union fields correspond to the first schema in the union.

At the example you gave, you do add the default attribute with value "null", so this should work. However, supporting this depends also on the library you use for reading the avro message (there are libraries at c,c++,python,java,c#,ruby etc.). Maybe (probably) the library you use lack this feature.

Eliyahu Machluf
  • 1,251
  • 8
  • 17