【问题标题】:Transform protobuf to avro将 protobuf 转换为 avro
【发布时间】:2013-07-01 01:45:39
【问题描述】:

我有数据以 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 对象?

问候, 罗南。

【问题讨论】:

  • 嗨 Ronen,你找到解决这个问题的方法了吗?我也有同样的经历。

标签: protocol-buffers avro


【解决方案1】:

您应该使用 ProtobufDatumWriter 而不是 GenericDatumWriter。

http://avro.apache.org/docs/current/api/java/org/apache/avro/protobuf/ProtobufDatumWriter.html

另外,使用 ProtobufDatumReader 读取 protobuf 数据。

最后,Avro 的问题在 Avro 的邮件列表中得到更及时的回答。

http://avro.apache.org/mailing_lists.html

【讨论】:

    猜你喜欢
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 2019-12-17
    • 1970-01-01
    • 2014-06-22
    • 2020-07-13
    相关资源
    最近更新 更多