avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From andy36 <andyfi...@gmail.com>
Subject Apache Avro : UnresolvedUnionException when using union data type
Date Mon, 02 Dec 2013 20:00:13 GMT
I am using [Apache Avro][1] for object serialization.

I have one Avro schema for `School` object:

    {"namespace": "com.my.model",
     "type": "record",
     "name": "School",
     "fields": [
         {"name": "sid",  "type": "int"},
         {"name": "size",  "type": "long"},
         {"name": "other", "type": ["null", "Teacher", "Student"]}
       ]
    }

As you see above, the `"other"` field holds an **union data type**, it could
be either `null` or a `Teacher` instance or a `Student` instance.

The **Teacher** object schema:

    {"namespace": "com.my.model",
    "type": "record",
    "name": "Teacher",
     	"fields": [
    		{"name": "isMale", "type": "boolean"}
    	]
    }

The **Student** object schema:

    {"namespace": "com.my.model",
    "type": "record",
    "name": "Student",
     	"fields": [
    		{"name": "age", "type": "int"}
    	]
    }

I compiled above schemas with Avro tool & Avro generated all the Java
classes for me automatically.

Then, in my java program I am creating `School` instance in the following
way:

    School school = new School();
    school.setSid(3);
    school.setSize(2000);
    
    //create a student object
    Student student = new Student();
    student.setAge(18);
    
    //set student into school instance
    school.setOther(student);

As you see above, the school instance's `other` field holds a student
object. When I compile my code, however I got the
**UnresolvedUnionException** . It complains about the **union data type**
for the `other` field of `School` schema. Seems it can not resolve the
`student` I set to `school` in my Java code. Why is this exception ?
Stacktrace is:

    org.apache.avro.UnresolvedUnionException: Not in union
["null",{"type":"record","name":"Student","namespace":"com.my.model","fields":[{"name":"age","type":"long"}]},{"type":"record","name":"Teacher","namespace":"com.model","fields":[{"name":"isMale","type":"boolean"}]}]:
false
    	at
org.apache.avro.generic.GenericData.resolveUnion(GenericData.java:561)
    	at
org.apache.avro.generic.GenericDatumWriter.resolveUnion(GenericDatumWriter.java:144)
    	at
org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:71)
    	at
org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:106)
    	at
org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:66)
    	at
org.apache.avro.generic.GenericDatumWriter.writeArray(GenericDatumWriter.java:131)
    	at
org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:68)
    	at
org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:73)
    	at
org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:106)
    	at
org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:66)
    	at
org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:58)


  [1]: http://avro.apache.org/docs/1.7.4/gettingstartedjava.html



--
View this message in context: http://apache-avro.679487.n3.nabble.com/Apache-Avro-UnresolvedUnionException-when-using-union-data-type-tp4028641.html
Sent from the Avro - Users mailing list archive at Nabble.com.

Mime
View raw message