Skip to Main Content

NoSQL Database

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

djansson_sfDec 5 2013 — edited Dec 9 2013

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  http://docs.oracle.com/cd/NOSQL/html/GettingStartedGuide/avroschemas.html 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(JsonBinding.java:403)

         at org.apache.avro.generic.GenericDatumWriter.resolveUnion(GenericDatumWriter.java:144)

         at oracle.kv.impl.api.avro.JsonBinding$JsonDatumWriter.write(JsonBinding.java:599)

         at oracle.kv.impl.api.avro.JsonBinding$JsonDatumWriter.writeRecord(JsonBinding.java:695)

         at oracle.kv.impl.api.avro.JsonBinding$JsonDatumWriter.write(JsonBinding.java:565)

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

https://issues.apache.org/jira/browse/AVRO-1376

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

This post has been answered by djansson_sf on Dec 9 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 6 2014
Added on Dec 5 2013
1 comment
8,790 views