gpt4 book ai didi

java - 在java中序列化多个protobuf消息并在Python中反序列化

转载 作者:行者123 更新时间:2023-11-30 06:11:39 25 4
gpt4 key购买 nike

我想将一堆 protobuf 消息存储在一个文件中,并稍后读取它们。

在java中,我可以使用'writeDelimitedTo'和'parseDelimitedFrom'来读取和写入文件。然而,我想用Python阅读它,它似乎只有一个“ParseFromString”方法。

一些SO问题非常相似,例如Parsing Protocol Buffers, written in Java and read in Python ,但这仅适用于单条消息:不适用于多条消息。

最佳答案

根据原型(prototype)指南,您需要自行处理消息的大小:

Streaming Multiple Messages

If you want to write multiple messages to a single file or stream, it is up to you to keep track of where one message ends and the next begins. The Protocol Buffer wire format is not self-delimiting, so protocol buffer parsers cannot determine where a message ends on their own. The easiest way to solve this problem is to write the size of each message before you write the message itself. When you read the messages back in, you read the size, then read the bytes into a separate buffer, then parse from that buffer. (If you want to avoid copying bytes to a separate buffer, check out the CodedInputStream class (in both C++ and Java) which can be told to limit reads to a certain number of bytes.)

https://developers.google.com/protocol-buffers/docs/techniques

一个简单的解决方案可能是您在文件中的新行上以 Base64 序列化每个原型(prototype)。

这样做,在 python 上解析和使用它们将非常容易。

关于java - 在java中序列化多个protobuf消息并在Python中反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50108028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com