Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 701
Emre Sevinç Avatar asked Mar 27 '15 11:03

Emre Sevinç


1 Answers

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   }, 
like image 71
arvin_v_s Avatar answered Sep 23 '22 05:09

arvin_v_s