Google Protocol Buffer 学习
来源:互联网 发布:pdf阅读器推荐 知乎 编辑:程序博客网 时间:2024/05/17 07:22
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三种语言。
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) 安装:
一个proto文件如下:
repeated: the field may be repeated any number of times (including zero).
Required Is ForeverYou should be very careful about marking fields as
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
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. 使用
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 cppprotoc --cpp_out=. --java_out=. --python_out=. addressbook.protopkg-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.txttest.txt: File not found. Creating a new file.Enter person ID number: 123456Enter name: zxmEnter email address (blank for none): zxmever@gmail.comEnter a phone number (or leave blank to finish): 12300000000Is this a mobile, home, or work phone? homeEnter 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
一个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
. 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
- Google Protocol Buffer 学习
- Google Protocol Buffer 学习
- Google Protocol Buffer 学习
- google的protocol buffer学习
- Google Protocol Buffer学习笔记
- google Protocol buffer(JAVA接口) 学习
- google Protocol buffer(JAVA接口) 学习
- Google Protocol Buffer的学习精要
- Google Protocol Buffer
- Protocol Buffer(Google) 學習筆記一
- google protocol buffer
- Google Protocol Buffer
- Google Protocol Buffer
- Google Protocol Buffer
- Google Protocol Buffer 用法
- 【Google Protocol Buffer】概览
- Google Protocol Buffer
- Google Protocol Buffer 介绍
- ansys中KBC设置
- nsubst,deltim和autots
- 回调函数声明与调用
- XMLDocument转成字符串[javascript]
- iphone 模拟器 XCode gdb 查找无法定位的程序崩溃
- Google Protocol Buffer 学习
- 安装Fedora 15后需做的25件事情
- java容器
- Android游戏开发之onTouchEvent
- VC++如何声明定义全局函数与全局变量
- 用samba实现fedora 15与windows局域网文件共享
- nginx编译缺少pcre
- CRC的原理与实现
- S2Container + Cubby 工程的创建