寻找一种快速、紧凑、可流式、多语言、强类型的序列化格式
我目前在 Java 项目中使用 JSON(通过 gzip 压缩),其中需要在磁盘上存储大量对象(数亿个)。 我每行有一个 JSON 对象,并且不允许在 JSON 对象内出现换行符。 这样我就可以逐行从磁盘流式传输数据,而不必立即读取整个文件。
事实证明,解析 JSON 代码(使用 http://www.json.org/java/)比将原始数据从磁盘中取出或解压缩(我即时执行)的开销更大。
理想情况下,我想要的是强类型序列化格式,我可以在其中指定“此对象字段是字符串列表”(例如),并且因为系统知道会发生什么,所以它可以快速反序列化它。 我还可以通过向其他人提供其“类型”来指定格式。
它还需要是跨平台的。 我使用 Java,但与使用 PHP、Python 和其他语言的人一起工作。
所以,回顾一下,它应该是:
- 强类型
- Streamable(即一点一点地读取文件,而不必一次将其全部加载到 RAM 中)
- 跨平台(包括 Java 和 PHP)
- 快速
- 免费(如演讲中)
有任何指针吗?
I'm currently using JSON (compressed via gzip) in my Java project, in which I need to store a large number of objects (hundreds of millions) on disk. I have one JSON object per line, and disallow linebreaks within the JSON object. This way I can stream the data off disk line-by-line without having to read the entire file at once.
It turns out that parsing the JSON code (using http://www.json.org/java/) is a bigger overhead than either pulling the raw data off disk, or decompressing it (which I do on the fly).
Ideally what I'd like is a strongly-typed serialization format, where I can specify "this object field is a list of strings" (for example), and because the system knows what to expect, it can deserialize it quickly. I can also specify the format just by giving someone else its "type".
It would also need to be cross-platform. I use Java, but work with people using PHP, Python, and other languages.
So, to recap, it should be:
- Strongly typed
- Streamable (ie. read a file bit by bit without having to load it all into RAM at once)
- Cross platform (including Java and PHP)
- Fast
- Free (as in speech)
Any pointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您查看过 Google Protocol buffers 吗?:
http://code.google.com/apis/protocolbuffers/
它们是跨平台的(C++、Java、Python),还具有 PHP 的第三方绑定。 它速度快、相当紧凑并且类型强。
这里还对各种格式进行了有用的比较:
http://code。 google.com/p/thrift-protobuf-compare/wiki/Benchmarking
您可能需要考虑 Thrift 或此处提到的其他工具之一。
Have you looked at Google Protocol buffers?:
http://code.google.com/apis/protocolbuffers/
They're cross platform (C++, Java, Python) with third party bindings for PHP also. It's fast, fairly compact and strongly typed.
There's also a useful comparison between various formats here:
http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking
You might want to consider Thrift or one of the others mentioned here as well.
我用 Jackson 解析 JSON 得到了非常好的结果
Jackson is a:
JSON 处理器(JSON 解析器 + JSON 生成器)。 除了基本的 JSON 读/写(解析、生成)之外,它还提供完整的基于节点的树模型,以及完整的 OJM(对象/Json 映射器)数据绑定功能。
与许多其他序列化选项相比,其性能非常好。
I've had very good results parsing JSON with Jackson
Jackson is a:
JSON processor (JSON parser + JSON generator) written in Java. Beyond basic JSON reading/writing (parsing, generating), it also offers full node-based Tree Model, as well as full OJM (Object/Json Mapper) data binding functionality.
Its performance is very good when compared to many other serialisation options.
你可以看一下 YAML- http://www.yaml.org/
它是 JSON 的超集,所以您将会熟悉数据文件结构。 它支持一些附加数据类型以及使用将一种数据结构的一部分包含到另一种数据结构中的引用的能力。
我不知道它是否“足够快”——但 libyaml 解析器(用 C 语言编写)看起来相当敏捷。
You could take a look at YAML- http://www.yaml.org/
It's a superset of JSON so the data file structure will be familiar to you. It supports some additional data types as well as the ability to use references that include a part of one data structure into another.
I don't have any idea if it will be "fast enough"- but the libyaml parser (written in C) seems pretty snappy.