Forum Stats

  • 3,758,591 Users
  • 2,251,416 Discussions
  • 7,870,257 Comments

Discussions

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

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

Tagged:

Best Answer

Answers

This discussion has been closed.