Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Duplicate
-
1.8.1
-
None
-
None
Description
Avro compiler generate an enum JAVA class from an enum Avro type
and this class is used in the Java class of the records pointing to the enum type. However, org.apache.avro.generic.GenericDatumWriter.writeEnum expects a GenericEnumSymbol rather than Java Enum!
This is problem because the class interfaces for Avro messages expect Java Enum and not GenericEnumSymbol!
Consider the following schema:
TrafficLights.avsc:
{"namespace": "com.comp.message",
"type": "record",
"name": "TrafficLights",
"fields": [
,
{"name": "signal", "type": "Signals" } ]
}
Signals.avsc:
{
"namespace": "com.comp.message",
"type": "enum",
"name":"Signals",
"symbols" : ["GREEN", "YELLOW", "RED"]
}
Using maven-avro-plugin to import "Signals" as an external schema, Avro compiler creates the following constructor for TrafficLights class:
public TrafficLights(java.lang.CharSequence id, com.comp.message.Signals signal)
and an Enum class fot the Signals:
package com.comp.message;
@SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated
public enum Signals {
GREEN, YELLOW, RED ;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("
");
public static org.apache.avro.Schema getClassSchema()
}
However, when I try to serialize a TrafficLights message, GenericDatumWriter.writeEnum throws an exeception:
TrafficLights mainTrafficLight = new TrafficLights ("123", Signals.GREEN);
byte[] msg = new AvroMsgManager( mainTrafficLight .getSchema().toString() ).serialize (mainTrafficLight))
org.apache.avro.AvroTypeException: Not an enum: GREEN
at org.apache.avro.generic.GenericDatumWriter.writeEnum(GenericDatumWriter.java:164)
at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:106)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:73)
at org.apache.avro.generic.GenericDatumWriter.writeField(GenericDatumWriter.java:153)
at org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:143)
at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:105)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:73)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:60)
Attachments
Issue Links
- duplicates
-
AVRO-1870 Avro compiler generate JAVA enum interfaces but writeEnum expects GenericEnumSymbol
- Open