简体   繁体   中英

Avro: ClassCastException while serializing / deserializing a file that contains an Enum value

I'm getting the following error when I try to deserialize a previously serialized file:

Exception in thread "main" java.lang.ClassCastException: 
com.ssgg.bioinfo.effect.Sample$Zygosity cannot be cast to 
com.ssgg.bioinfo.effect.Sample$.Zygosity
at com.ssgg.ZygosityTest.deserializeZygosityToAvroStructure(ZygosityTest.java:45)
at com.ssgg.ZygosityTest.main(ZygosityTest.java:30)

In order to reproduce the error, the main class is as follows:

public class ZygosityTest {

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

    String filepath = "/home/XXXX/zygosity.avro";

    /* Populate Zygosity*/
    com.ssgg.bioinfo.effect.Sample$.Zygosity zygosity = com.ssgg.bioinfo.effect.Sample$.Zygosity.HET;

    /* Create file serialized */
    createZygositySerialized(zygosity, filepath);

    /* Deserializae file */
    com.ssgg.bioinfo.effect.Sample$.Zygosity avroZygosityOutput = deserializeZygosityToAvroStructure(filepath);

}

private static com.ssgg.bioinfo.effect.Sample$.Zygosity deserializeZygosityToAvroStructure(String filepath)
        throws IOException {
    com.ssgg.bioinfo.effect.Sample$.Zygosity zygosity = null;

    File myFile = new File(filepath);
    DatumReader<com.ssgg.bioinfo.effect.Sample$.Zygosity> reader = new SpecificDatumReader<com.ssgg.bioinfo.effect.Sample$.Zygosity>(
            com.ssgg.bioinfo.effect.Sample$.Zygosity.class);
    DataFileReader<com.ssgg.bioinfo.effect.Sample$.Zygosity> dataFileReader = new DataFileReader<com.ssgg.bioinfo.effect.Sample$.Zygosity>(
            myFile, reader);

    while (dataFileReader.hasNext()) {
        zygosity = dataFileReader.next(zygosity);
    }

    dataFileReader.close();
    return zygosity;
}

private static void createZygositySerialized(com.ssgg.bioinfo.effect.Sample$.Zygosity zygosity, String filepath)
        throws IOException {
    DatumWriter<com.ssgg.bioinfo.effect.Sample$.Zygosity> datumWriter = new SpecificDatumWriter<com.ssgg.bioinfo.effect.Sample$.Zygosity>(
            com.ssgg.bioinfo.effect.Sample$.Zygosity.class);
    DataFileWriter<com.ssgg.bioinfo.effect.Sample$.Zygosity> fileWriter = new DataFileWriter<com.ssgg.bioinfo.effect.Sample$.Zygosity>(
            datumWriter);

    Schema schema = com.ssgg.bioinfo.effect.Sample$.Zygosity.getClassSchema();

    fileWriter.create(schema, new File(filepath));
    fileWriter.append(zygosity);
    fileWriter.close();
}

}

The avro generated enum for Zygosity is as follows:

/**
* Autogenerated by Avro
*
* DO NOT EDIT DIRECTLY
*/
package com.ssgg.bioinfo.effect.Sample$;
@SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated
public enum Zygosity {
  HOM_REF, HET, HOM_VAR, HEMI, UNK  ;
  public static final org.apache.avro.Schema SCHEMA$ = new     org.apache.avro.Schema.Parser().parse("{\"type\":\"enum\",\"name\":\"Zygosity\",\"namespace\":\"com.ssgg.bioinfo.effect.Sample$\",\"symbols\":[\"HOM_REF\",\"HET\",\"HOM_VAR\",\"HEMI\",\"UNK\"]}");
  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }

}

I'm a newbie in Avro, can somebody plese help me to find the problem? In my project I try to serialize and deserialize a bigger structure, but I have problems with the enums, so I isolated a smaller problem here. If you need more info I can post it. Thanks.

I believe the main issue here is that $ has a special meaning in Java classes, and less important is that package names are typically lowercased.

So, you should at least edit the namespaces to remove the $

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM