Java Avro:序列化/反序列化包含枚举值的文件时发生ClassCastException
尝试反序列化以前序列化的文件时,出现以下错误:Java Avro:序列化/反序列化包含枚举值的文件时发生ClassCastException,java,serialization,deserialization,avro,Java,Serialization,Deserialization,Avro,尝试反序列化以前序列化的文件时,出现以下错误: 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(Zygosity
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)
为了重现错误,主要类如下所示:
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();
}
}
我是Avro的新手,有人能帮我找到问题吗?
在我的项目中,我尝试序列化和反序列化一个更大的结构,但我在枚举方面有问题,所以我在这里隔离了一个较小的问题。
如果你需要更多的信息,我可以发布。
谢谢。我认为这里的主要问题是$在Java类中有特殊的含义,而不太重要的是包名通常是小写的
因此,您至少应该编辑名称空间以删除$包路径不应以大写字母开头。。。你真的应该使用Avro Maven插件,而不是手动进行此转换。你是对的,我更改了包路径,它工作起来很有魅力。谢谢!!!请随意在下面展示您的解决方案作为答案,因为我不确定您之前在我使用json的项目中更改了什么。我想使用avro,所以我使用一个工具将json模式文件转换为avro模式文件。该工具为带有大写字母的枚举创建了名称空间,即名称空间:com.ssgg.bioinfo.effect.bioinfoviant$。一旦我更改了namespace:com.ssgg.bioinfo.effect.avro的值,我就再也没有得到错误了。所以你给我指出了正确的方向。请详细说明答案,我将把它作为正式答案投票。再次感谢。
/**
* 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$; }