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)?

Emre Sevinç picture Emre Sevinç · Mar 27, 2015 · Viewed 16.4k times · Source

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?

Answer

arvin_v_s picture arvin_v_s · Aug 21, 2019

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
  },