[英]Transform protobuf to avro
I have data serialized in protobuff format and I want to transform it to an Avro serialization. 我已将数据序列化为probbuff格式,我想将其转换为Avro序列化。
I have no problem reading the proto data using 我没有问题使用读取原始数据
ProtoTest.Msg msg = buildMessage();
ProtobufData protobufData = ProtobufData.get();
Schema protoSchema = protobufData.getSchema(ProtoTest.Msg.class);
Object o = protobufData.newRecord(msg, protoSchema);
The resulting o is again a protobuf object. 所得的o还是protobuf对象。 Now I want to write o as avro with the same schema
现在我想用相同的模式将o编写为avro
GenericDatumWriter genericDatumWriter = new GenericDatumWriter(protoSchema);
OutputStream out = new ByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().binaryEncoder(out, null );
genericDatumWriter.write(o, encoder);
But running the code above throws next exception at the write method 但是运行上面的代码会在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)
How can I transform proto object into avro object? 如何将原型对象转换为Avro对象?
Regards, Ronen. 问候,罗嫩。
You should use a ProtobufDatumWriter instead of a GenericDatumWriter. 您应该使用ProtobufDatumWriter而不是GenericDatumWriter。
http://avro.apache.org/docs/current/api/java/org/apache/avro/protobuf/ProtobufDatumWriter.html http://avro.apache.org/docs/current/api/java/org/apache/avro/protobuf/ProtobufDatumWriter.html
Also, use a ProtobufDatumReader to read protobuf data. 另外,使用ProtobufDatumReader读取protobuf数据。
Lastly, Avro questions are more promptly answered on Avro's mailing lists. 最后,在Avro的邮件列表中,Avro的问题会得到更迅速的解答。
http://avro.apache.org/mailing_lists.html http://avro.apache.org/mailing_lists.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.