我有以 protobuff 格式序列化的数据,我想将其转换为 Avro 序列化。
我使用读取原始数据没有问题
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 对象?
问候,罗南。