我有一些以Protobuff格式序列化的数据,想要将其转换为Avro序列化。
读取Protobuff数据并不成问题,可以使用:
ProtoTest.Msg msg = buildMessage();
ProtobufData protobufData = ProtobufData.get();
Schema protoSchema = protobufData.getSchema(ProtoTest.Msg.class);
Object o = protobufData.newRecord(msg, protoSchema);
得到的o再次成为protobuf对象。现在我想用相同的模式将o写成avro。
GenericDatumWriter genericDatumWriter = new GenericDatumWriter(protoSchema);
OutputStream out = new ByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().binaryEncoder(out, null );
genericDatumWriter.write(o, encoder);
但是运行上面的代码会在write方法处抛出下一个异常。
java.lang.ClassCastException: example.avro.ProtoTest$Msg cannot be cast to org.apache.avro.generic.IndexedRecord
at org.apache.avro.generic.GenericData.getField(GenericData.java:526)
at org.apache.avro.generic.GenericData.getField(GenericData.java:541)
at org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:104)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:66)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:58)
at hermes.mediationOrchestrator.AvroFileWriteTest.testWriter3(AvroFileWriteTest.java:115)
如何将proto对象转换为avro对象?
谢谢, Ronen。