google protocol buffer

https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html
Google Protocol Buffer 的使用和原理
Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 个 .proto 文件。他们用于 RPC 系统和持续数据存储系统。
Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。

简单说来 Protobuf 的主要优点就是:简单,快。具体数据可参看下面link:
https://github.com/eishay/jvm-serializers/wiki

简单例子:
1. 安装 Google Protocol Buffer
2. 书写 .proto 文件
首先我们需要编写一个 proto 文件,定义我们程序中需要处理的结构化数据,在 protobuf 的术语中,结构化数据被称为 Message。proto 文件非常类似 java 或者 C 语言的数据定义。代码清单 1 显示了例子应用中的 proto 文件内容。
清单 1. proto 文件
package lm;
message helloworld
{
required int32 id = 1; // ID
required string str = 2; // str
optional int32 opt = 3; //optional field
}
3.编译 .proto 文件
写好 proto 文件之后就可以用 Protobuf 编译器将该文件编译成目标语言了。本例中我们将使用 C++。
假设您的 proto 文件存放在 SRCDIR使protocI= S R C D I R 下 面 , 您 也 想 把 生 成 的 文 件 放 在 同 一 个 目 录 下 , 则 可 以 使 用 如 下 命 令 : p r o t o c − I = SRC_DIR --cpp_out= DSTDIR D S T D I R SRC_DIR/addressbook.proto
命令将生成两个文件:
lm.helloworld.pb.h , 定义了 C++ 类的头文件
lm.helloworld.pb.cc , C++ 类的实现文件
在生成的头文件中,定义了一个 C++ 类 helloworld,后面的 Writer 和 Reader 将使用这个类来对消息进行操作。诸如对消息的成员进行赋值,将消息序列化等等都有相应的方法。
4.编写 writer 和 Reader
Writer:

#include "lm.helloworld.pb.h"
…
 int main(void)
 {
  lm::helloworld msg1;
  msg1.set_id(101);
  msg1.set_str(“hello”);
  // Write the new address book back to disk.
  fstream output("./log", ios::out | ios::trunc | ios::binary);

  if (!msg1.SerializeToOstream(&output)) {
      cerr << "Failed to write msg." << endl;
      return -1;
  }
  return 0;
 }

Reader:

#include "lm.helloworld.pb.h"void ListMsg(const lm::helloworld & msg) {
  cout << msg.id() << endl;
  cout << msg.str() << endl;
 }
 int main(int argc, char* argv[]) {
  lm::helloworld msg1;
  {
    fstream input("./log", ios::in | ios::binary);
    if (!msg1.ParseFromIstream(&input)) {
      cerr << "Failed to parse address book." << endl;
      return -1;
    }
  }

  ListMsg(msg1);
  …
 }


  1. 运行结果
    运行 Writer 和 Reader 的结果如下:

writer
reader
101
Hello
Protobuf 的不足
Protbuf 与 XML 相比也有不足之处。它功能简单,无法用来表示复杂的概念。
XML 已经成为多种行业标准的编写工具,Protobuf 只是 Google 公司内部使用的工具,在通用性上还差很多。
由于文本并不适合用来描述数据结构,所以 Protobuf 也不适合用来对基于文本的标记文档(如 HTML)建模。另外,由于 XML 具有某种程度上的自解释性,它可以被人直接读取编辑,在这一点上 Protobuf 不行,它以二进制的方式存储,除非你有 .proto 定义,否则你没法直接读出 Protobuf 的任何内容。

https://www.cnblogs.com/yinheyi/p/6080244.html
Google Protocol Buffer的安装与.proto文件的定义

来源:https://github.com/google/protobuf/releases protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台。google 提供了多种语言的实现,如:java、c#、c++、javascript、go 、python、ruby和php等,每一种实现都包含了相应语言的编译器以及库文件。由于它是一种二进制的格式,比使用 xml 进行数据交换快许多。可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换。作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输、配置文件、数据存储等诸多领域。 本资源包含: protobuf-all-3.6.0.tar.gz 8.25 MB protobuf-all-3.6.0.zip 4.25 MB protobuf-cpp-3.6.0.tar.gz 5.18 MB protobuf-cpp-3.6.0.zip 4.57 MB protobuf-csharp-3.6.0.tar.gz 5.66 MB protobuf-csharp-3.6.0.zip 4.7 MB protobuf-java-3.6.0.tar.gz 5.86 MB protobuf-java-3.6.0.zip 4.4 MB protobuf-js-3.6.0.tar.gz 5.43 MB protobuf-js-3.6.0.zip 4.59 MB protobuf-objectivec-3.6.0.tar.gz 5.69 MB protobuf-objectivec-3.6.0.zip 4.6 MB protobuf-php-3.6.0.tar.gz 5.64 MB protobuf-php-3.6.0.zip 4.53 MB protobuf-python-3.6.0.tar.gz 5.57 MB protobuf-python-3.6.0.zip 4.52 MB protobuf-ruby-3.6.0.tar.gz 5.5 MB protobuf-ruby-3.6.0.zip 1.46 MB protoc-3.6.0-linux-aarch_64.zip 1.31 MB protoc-3.6.0-linux-x86_32.zip 1.36 MB protoc-3.6.0-linux-x86_64.zip 2.44 MB protoc-3.6.0-osx-x86_32.zip 2.39 MB protoc-3.6.0-osx-x86_64.zip 984 KB protoc-3.6.0-win32.zip Source code (zip) Source code (tar.gz)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值