Google Protocol Buffers - 将消息存储到文件中 [英] Google Protocol Buffers - Storing messages into file

查看:33
本文介绍了Google Protocol Buffers - 将消息存储到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用谷歌协议缓冲区来序列化股票市场数据(即时间戳、出价、询价字段).我可以将一条消息存储到一个文件中并对其进行反序列化.

I'm using google protocol buffer to serialize equity market data (ie. timestamp, bid,ask fields). I can store one message into a file and deserialize it without issue.

如何将多条消息存储到一个文件中?不知道如何分离消息.我需要能够即时将新消息附加到文件中.

How can I store multiple messages into a single file? Not sure how I can separate the messages. I need to be able to append new messages to the file on the fly.

推荐答案

我建议在 上使用 writeDelimitedTo(OutputStream)parseDelimitedFrom(InputStream) 方法消息 对象.writeDelimitedTo 在消息本身之前写入消息的长度;parseDelimitedFrom 然后使用该长度仅读取一条消息,不再读取更多消息.这允许将多个消息写入单个 OutputStream 然后分别解析.有关详细信息,请参阅 https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/MessageLite#writeDelimitedTo(java.io.OutputStream)

I would recommend using the writeDelimitedTo(OutputStream) and parseDelimitedFrom(InputStream) methods on Message objects. writeDelimitedTo writes the length of the message before the message itself; parseDelimitedFrom then uses that length to read only one message and no farther. This allows multiple messages to be written to a single OutputStream to then be parsed separately. For more information, see https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/MessageLite#writeDelimitedTo(java.io.OutputStream)

这篇关于Google Protocol Buffers - 将消息存储到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆