Google Protocol Buffer ѧϰ

匿名 (未验证) 提交于 2019-12-03 00:05:01
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 as required 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



转载于:https://my.oschina.net/zipu888/blog/549658

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!