Forum Stats

  • 3,839,777 Users
  • 2,262,536 Discussions


Avro schema - optional fields - gives "Not in Union" exception

djansson_sf Member Posts: 2
edited Dec 9, 2013 9:35PM in NoSQL Database Discussions

Hi all,

I have a problem with using optional fields in an avro schema and using the JSONAvroBinding.

If I have a User schema that has a first name field. The User can opt to provide the first name , or not. The field is optional from a readers perspective.

(this is taken from which is nosql avro documentation)


  "type": "record",

  "namespace": "com.example",

  "name": "FullName",

  "fields": [

    { "name": "first", "type": ["string", "null"] },

    { "name": "last", "type": "string", "default" : "Doe" }



Here if I supply this example as the Avro schema, and then omitt the first name when I write, it will work without problem. But when I attempt to write "John" it will fail with:

Caused By: org.apache.avro.UnresolvedUnionException: Not in union ["string","null"]: "John"

         at oracle.kv.impl.api.avro.JsonBinding$JsonData.resolveUnion(

         at org.apache.avro.generic.GenericDatumWriter.resolveUnion(

         at oracle.kv.impl.api.avro.JsonBinding$JsonDatumWriter.write(

         at oracle.kv.impl.api.avro.JsonBinding$JsonDatumWriter.writeRecord(

         at oracle.kv.impl.api.avro.JsonBinding$JsonDatumWriter.write(

         Truncated. see log file for complete stacktrace

This fails:


   "first" : "John",

   "last" : "Smith"


This works:


   "last" : "Smith"


It seems someone else had this problem and posted on AVROs site, where a response said it might be NoSQLs libraries fault:

"The error is thrown from the Oracle's NoSQL library's use of Avro and is not clearly an Avro problem. You might be better off trying to get help for this from Oracle?"

Any thoughts? To me this seems like a bug in the JsonBinding class?


Best Answer


This discussion has been closed.