I am trying to deserialize/read an Avro file, the avro data file doesn't have the new field. Even though the new field is declared as null in schema, it is expected to be optional. But it still gives me error as mandatory.
Exception in thread "main" org.apache.avro.AvroTypeException: Found com.kiran.avro.User, expecting com.kiran.avro.User, missing required field loc
The AVRO schema declaration:
{"name": "loc", "type": ["string", "null"]}
Reading file using code:
DatumReader<User> userDatumReader = new SpecificDatumReader<User>(User.class);
DataFileReader<User> dataFileReader = new DataFileReader<User>(file, userDatumReader);
Is there any other way to declare an optional field?
Thanks for hints/suggestions !!
Apache Avro To model the set of fields within a schema, Avro supports the following primitive types: null: No value. boolean: Binary value.
Logical types specify a way of representing a high-level type as a base Avro type. For example, a date is specified as the number of days after the unix epoch (or before using a negative value). This enables extensions to Avro's type system without breaking binary compatibility.
@johndcal A namespace is not required within the avro schema source in Schema Registry.
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 }
What is the content of "file"?
I might be wrong, but if you define a field in schema as {"name": "loc", "type": ["string", "null"]}
, you still need to define a loc
field, even for null
. It should be something like "loc": null
in the file.
Try adding "default"
to this field declaration:
{"name" : "loc",
"type" : ["null","string"] ,
"default" : null}
Then it should be possible to omit this field in file.
You can also see this question Avro: deserialize json - schema with optional fields for some additional info and examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With