Google Protocol Buffer 学习

本文介绍了Protocol Buffer的基本概念,对比XML的优势,包括更简洁、体积更小、速度更快等特点,并详细讲解了安装配置过程及一个具体的C++示例。此外还讨论了消息字段的特性及其扩展性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 概念
      Protocol Buffer是一种类似于XML的灵活高效的结构化数据存储格式,和XML相比,Protocol具有以下优势:
           1) simpler
           2) smaller:3-10倍
           3) faster:20-100倍
           4) less ambiguous
           5) generate data access classes that are easier to use programmatically.
      由于protocol buffer简单、高效的特点,其非常适用于作为模块、系统及平台无关的结构化数据描述格式,目前其支持C++、JAVA、PYTHON三种语言。

2. 使用

      安装pb c++支持:
      1)去http://code.google.com/p/protobuf/downloads/list下载最新版本:http://protobuf.googlecode.com/files/protobuf-2.4.1.tar.gz
      2)解压,tar -zxvf protobuf-2.4.1.tar.gz
      3) 安装:

$ cd protobuf-2.4.1
$ ./configure; 
$ make
$ make check
$ make install

此处需要注意,在ubuntu下,需要添加共享库路径: 

export LD_LIBRARY_PATH=/usr/lib/local

目录./protobuf-2.4.1/examples下有一个例子,功能简单,一个程序使用使用pb定义的类型写数据到一个文件,另一个程序读取该文件。
目标语言为c++的编译方式如下:

$ make cpp
protoc --cpp_out=. --java_out=. --python_out=. addressbook.proto
pkg-config --cflags protobuf  # fails if protobuf is not installed
-pthread -I/usr/local/include  
c++ add_person.cc addressbook.pb.cc -o add_person_cpp `pkg-config --cflags --libs protobuf`
pkg-config --cflags protobuf  # fails if protobuf is not installed
-pthread -I/usr/local/include  
c++ list_people.cc addressbook.pb.cc -o list_people_cpp `pkg-config --cflags --libs protobuf`

运行:

$ ./add_person_cpp test.txt
test.txt: File not found.  Creating a new file.
Enter person ID number: 123456
Enter name: zxm
Enter email address (blank for none): zxmever@gmail.com
Enter a phone number (or leave blank to finish): 12300000000
Is this a mobile, home, or work phone? home
Enter a phone number (or leave blank to finish): 
$./list_people_cpp test.txt 
Person ID: 123456
  Name: zxm
  E-mail address: zxmever@gmail.com
  Home phone #: 12300000000


上述例子的分析可参考:http://code.google.com/apis/protocolbuffers/docs/cpptutorial.html

4. 分析
一个proto文件如下:

package tutorial; // 

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;
}

message AddressBook {
  repeated Person person = 1;
}


说明:

required: a value for the field must be provided, otherwise the message will be considered "uninitialized"
optional: the field may or may not be set. If an optional field value isn't set, a default value is used.
repeated: the field may be repeated any number of times (including zero).

Required Is ForeverYou should be very careful about marking fields asrequired. If at some point you wish to stop writing or sending a required field, it will be problematic to change the field to an optional field – old readers will consider messages without this field to be incomplete and may reject or drop them unintentionally. You should consider writing application-specific custom validation routines for your buffers instead. Some engineers at Google have come to the conclusion that usingrequired does more harm than good; they prefer to use onlyoptional andrepeated. However, this view is not universal.

5. 扩展性
protocol buffer具有很好的向后扩展性,前提是遵守如下规则:
1)you must not change the tag numbers of any existing fields.
2)you must not add or delete any required fields.
3)you may delete optional or repeated fields.
4)you may add new optional or repeated fields but you must use fresh tag numbers (i.e. tag numbers that were never used in this protocol buffer, not even by deleted fields).
6. 参考文献
http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/?ca=drs-tp4608
http://code.google.com/apis/protocolbuffers/docs/overview.html



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值