SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
ProtoBuf



       Dongdong Deng <LibFetion@gmail.com>
OverView

� 1、简介
� 2、示例
� 3、.proto语法
1、ProtoBuf
  ProtoBuf
Google开源的,用于序列结构化数据的协议

http://code.google.com/apis/protocolbu
  ffers
� 跨编程语言
� 跨系统平台
� 可扩展
1、ProtoBuf vs XML
  ProtoBuf
� 简单
� 比XML小3-10倍
� 比XML快20-100倍
� 避免歧义
� 自动产生数据操作类,让开发者更容易使
  用
1、ProtoBuf 缺点
  ProtoBuf
� 目前使用不够广泛
ProtoBuf目前没有广泛使用,如果系统需要提供若干对外
   的接口给第三方系统调用,XML目前是更好的选择


� 二进制格式导致可读性差
二进制格式进行编码,通信协议出现问题不好跟踪

� 缺乏自描述
如果不配合相应的proto文件,ProtoBuf基本是看不懂,
在配置文件方面,XML还是很有优势的
2、ProtoBuf 示例
  ProtoBuf
1: Write .proto 文件

2:protoc --cpp_out --java_out --python_out

3: *.pb.h *.pb.cc

4: using above *.pb.h in your program
2、.proto
  .proto

message Test
{
  required int32 id = 1;
  required string pwd = 2;
  optional string others = 3;
}
2、protoc
  protoc


protoc --cpp_out=./test test.proto

$ls ./test
test.pb.h test.pb.cc
2、progam-产生方
  progam-
产生方:

Test test;
test.set_id(344300);
test.set_pwd(123);
test.set_others("othersothers");

string sTest;
test.SerailzeToString(&sTest);
2、progam-读取方
  progam-
读取方:
string sTest; //得到协议数据,存放到sTest
Test test;
if(test.ParseFromString(sTest))
{ cout << "ID:" << test.id() << endl
       << "PWD:" << test.pwd() << endl
       << "others:" << test.others() << endl;
} else {
  cerr << "parse error!" << endl;
}
3、proto 语法
  proto
package tutorial;

message Person {
 required string name = 1;
 required int32 id = 2;     // Unique ID number for this person.
 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;
} Make Presentation much more fun

// Our address book file is just one of these.                     @WPS官方微博
                                                                   @WPS官方微博
message AddressBook {                                              @kingsoftwps
  repeated Person person = 1;
}
Protobuf

Contenu connexe

En vedette

Apresentacao sessoes mz
Apresentacao sessoes mzApresentacao sessoes mz
Apresentacao sessoes mz
Diogo Gomes
 
Four Tips to improve & analyse your Sales Performance Scorecard Effectively
Four Tips to improve & analyse your Sales Performance Scorecard EffectivelyFour Tips to improve & analyse your Sales Performance Scorecard Effectively
Four Tips to improve & analyse your Sales Performance Scorecard Effectively
Andre Vonk
 
How to become cloud backup provider
How to become cloud backup providerHow to become cloud backup provider
How to become cloud backup provider
CLOUDIAN KK
 

En vedette (16)

Krylov
KrylovKrylov
Krylov
 
Communicating student learning, november 2015
Communicating student learning, november 2015Communicating student learning, november 2015
Communicating student learning, november 2015
 
Rileev
RileevRileev
Rileev
 
Cloudian meets CloudStack
Cloudian meets CloudStackCloudian meets CloudStack
Cloudian meets CloudStack
 
クラウドストレージとは? (Cloudian Summit 2012)
クラウドストレージとは? (Cloudian Summit 2012)クラウドストレージとは? (Cloudian Summit 2012)
クラウドストレージとは? (Cloudian Summit 2012)
 
Babel
BabelBabel
Babel
 
Apresentacao sessoes mz
Apresentacao sessoes mzApresentacao sessoes mz
Apresentacao sessoes mz
 
Four Tips to improve & analyse your Sales Performance Scorecard Effectively
Four Tips to improve & analyse your Sales Performance Scorecard EffectivelyFour Tips to improve & analyse your Sales Performance Scorecard Effectively
Four Tips to improve & analyse your Sales Performance Scorecard Effectively
 
8 Things You Need To Know About the Shanghai World Expo, 2010
8 Things You Need To Know About the Shanghai World Expo, 20108 Things You Need To Know About the Shanghai World Expo, 2010
8 Things You Need To Know About the Shanghai World Expo, 2010
 
Prodovolstvie
ProdovolstvieProdovolstvie
Prodovolstvie
 
Solgenicin
SolgenicinSolgenicin
Solgenicin
 
How to become cloud backup provider
How to become cloud backup providerHow to become cloud backup provider
How to become cloud backup provider
 
Eleven tips to build your succesfull body language
Eleven tips to build your succesfull body languageEleven tips to build your succesfull body language
Eleven tips to build your succesfull body language
 
Iskusstvo feod
Iskusstvo feodIskusstvo feod
Iskusstvo feod
 
Nabokov
NabokovNabokov
Nabokov
 
Dostoevskiy unizennie
Dostoevskiy unizennieDostoevskiy unizennie
Dostoevskiy unizennie
 

Protobuf

  • 1. ProtoBuf Dongdong Deng <LibFetion@gmail.com>
  • 4. 1、ProtoBuf vs XML ProtoBuf � 简单 � 比XML小3-10倍 � 比XML快20-100倍 � 避免歧义 � 自动产生数据操作类,让开发者更容易使 用
  • 5. 1、ProtoBuf 缺点 ProtoBuf � 目前使用不够广泛 ProtoBuf目前没有广泛使用,如果系统需要提供若干对外 的接口给第三方系统调用,XML目前是更好的选择 � 二进制格式导致可读性差 二进制格式进行编码,通信协议出现问题不好跟踪 � 缺乏自描述 如果不配合相应的proto文件,ProtoBuf基本是看不懂, 在配置文件方面,XML还是很有优势的
  • 6. 2、ProtoBuf 示例 ProtoBuf 1: Write .proto 文件 2:protoc --cpp_out --java_out --python_out 3: *.pb.h *.pb.cc 4: using above *.pb.h in your program
  • 7. 2、.proto .proto message Test { required int32 id = 1; required string pwd = 2; optional string others = 3; }
  • 8. 2、protoc protoc protoc --cpp_out=./test test.proto $ls ./test test.pb.h test.pb.cc
  • 9. 2、progam-产生方 progam- 产生方: Test test; test.set_id(344300); test.set_pwd(123); test.set_others("othersothers"); string sTest; test.SerailzeToString(&sTest);
  • 10. 2、progam-读取方 progam- 读取方: string sTest; //得到协议数据,存放到sTest Test test; if(test.ParseFromString(sTest)) { cout << "ID:" << test.id() << endl << "PWD:" << test.pwd() << endl << "others:" << test.others() << endl; } else { cerr << "parse error!" << endl; }
  • 12. package tutorial; message Person { required string name = 1; required int32 id = 2; // Unique ID number for this person. 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; } Make Presentation much more fun // Our address book file is just one of these. @WPS官方微博 @WPS官方微博 message AddressBook { @kingsoftwps repeated Person person = 1; }