2

I am trying to serialize an enum value into an Avro message and send it. The schema is built with:

Schema myschema = SchemaBuilder.record("com.testing.schemas").fields()
.name("enumTest").type().nullable().enumeration("aname")
.symbols("a","b","c","d","e").noDefault();

and looks like this

{"name":"enumTest","type":
[{"type":"enum","name":"aname",
"symbols":"a","b","c","d","e"]}
,"null"]}

My code:

GenericRecord record = new GenericData.Record(myschema);
GenericData.EnumSymbol symbol = new GenericData.EnumSymbol(
myschema.getField("enumTest").schema(), "a");
record.put("enumTest", symbol);

When trying to send the message I get the error :

Error serializing Avro message
org.apache.avro.UnresolvedUnionException: Not in union [{"type":"enum","name":"aname","namespace":"com.testing.schemas","symbols":["a","b","c","d","e"]},"null"]: a

What am I missing? Thanks.

1 Answer 1

5

The type of field aname is a Union, not an Enum:

[ {
  "type" : "enum",
  "name" : "aname",
  "namespace" : "com.testing",
  "symbols" : [ "a", "b", "c", "d", "e" ]
}, "null" ]

You could do something like this:

    String namespace = "com.testing";

    Schema enumTest =
        SchemaBuilder.enumeration("aname")
            .namespace(namespace)
            .symbols("a", "b", "c", "d", "e");

    Schema myschema =
        SchemaBuilder.record("schemas")
            .namespace(namespace)
            .fields()
            .name("enumTest")
            .type()
            .unionOf()
            .nullType()
            .and()
            .type(enumTest)
            .endUnion()
            .noDefault()
            .endRecord();

    GenericRecord record = new GenericData.Record(myschema);
    GenericData.EnumSymbol symbol = new GenericData.EnumSymbol(enumTest, "a");
    record.put("enumTest", symbol);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.