The exception means that you have inserted a value that has invalid type
according to the schema. The schema says that the value in that field
must be union of null and `some record` (the record name is removed from
your stacktrace so I don't know what record that is). It means that the
you can only insert values of type `null` and `some record`

For example:

The code below throws "org.apache.avro.UnresolvedUnionException: Not in
union ["null","string"] true" because I have inserted a value of type
`boolean` to a field that only accepts `null` or `string`

Schema schema = SchemaBuilder.record("test")
    .namespace("foo")
    .fields()
        .name("bar").type()
            .unionOf()
                .nullType()
                .and()
                .stringType()
            .endUnion()
            .noDefault()
    .endRecord();

GenericRecord record = new GenericData.Record(schema);
record.put("bar", true);

GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
ByteArrayOutputStream clonedRecordOutputStream = new ByteArrayOutputStream();
Encoder binaryEncoder =
        EncoderFactory.get().binaryEncoder(clonedRecordOutputStream, null);
writer.write(record, binaryEncoder);

but 

GenericRecord record = new GenericData.Record(schema);
record.put("bar", "baz");

or

GenericRecord record = new GenericData.Record(schema);
record.put("bar", null);

would work fine.

-Mika
 
On Sep 22 2020, at 7:26 pm, Colin Williams
<[email protected]> wrote:

> I am still curious regarding
> 
> Throws UnresolvedUnionException ```Caused by:
> org.apache.avro.UnresolvedUnionException: Not in union
> ["null",{"type":"record","name"...
> at org.apache.avro.generic.GenericData.resolveUnion(GenericData.java:853)
> at 
> org.apache.avro.generic.GenericDatumWriter.resolveUnion(GenericDatumWriter.java:249)
> at 
> org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:142)
> at 
> org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:82)
> at 
> org.apache.avro.generic.GenericDatumWriter.writeField(GenericDatumWriter.java:206)
> at 
> org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:195)
> at 
> org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:130)
> at 
> org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:82)
> at 
> org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:72)
> ```
> 
> the copy exeption since I have since resolved creating the record with
> a Schema.
> 
> 
> Thanks,
> 
> Colin

Reply via email to