Google Protobuf Guide
来源:互联网 发布:蜜蜡怎么优化 编辑:程序博客网 时间:2024/06/06 04:27
首先,这只是一篇Protobuf的Guide,没有任何高级内容。
ProtoBuf是什么?
我只能说,你写过代码就会明白。它和序列化对象关联很紧密,类似功能的工具很多。如xml Json等都是用来序列化对象的。至于序列化对象是什么?还是那句话,你写过代码就会明白。我的语文不好,不能给大家描述清楚。我个人理解就是把对象存储在文件里。举个简单的例子,我们有一个Person类,表示一个人。如下:
class Person{ public: void setName(const string & na); string getName(); private: string name; int age; string sex;}
有一个很明显的问题。程序运行结束,对象就消失了。相应地,对象的数据也消失了。序列化对象就是解决这个问题的(我说的可能不准确,大家酌情参考),把对象的字段存储到一个长期存在的文件里。例如Json XML等。这样,下次运行程序我们就可以从文件里读取对象信息了。肯定地,我们需要实现读取的方法。也就是反序列化
简介
什么是 Google Protocol Buffer? 假如您在网上搜索,应该会得到类似这样的文字介绍: Google Protocol
Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162
种报文格式定义和超过 12,183 个 .proto 文件。他们用于 RPC 系统和持续数据存储系统。 Protocol Buffers
是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC
数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python
三种语言的 API。 或许您和我一样,在第一次看完这些介绍后还是不明白 Protobuf
究竟是什么,那么我想一个简单的例子应该比较有助于理解它。
传送门
下载,安装
下载:传送门
安装:
- 找到 protobuf-2.6.1.tar.gz
- 解压
- ./configure
- make
- make check
- make install
其实INSTALL.txt中已经告诉你怎么安装了,不过现在人比较急躁肯定没兴趣看。如果你想自己指定安装路径,可以在第三步./configure
的时候加上--prefix=PATH
前缀。可参考:
By default, make install’ will install the package’s files in
/usr/local/bin
/usr/local/man’, etc. You can specify an
installation prefix other than/usr/local' by giving
configure’ the
option `–prefix=PATH’.
到这里算是安装成功了
使用
Protobuf文件的后缀名是.proto,我们在这里写一个info.liangsc.proto文件,如下:
package Info;message Liangsc{ required string name=1; required int32 age = 21; required string email = 2;}
一个好的命名习惯是 :
packageName.MessageName.proto
遵循这样的风格,上面的liangsc message有三个字段string类型的name int32的age string类型的email
现在我们编译这个文件:
protoc --cpp_out=./ ./info.liangsc.proto
如果没有意外的话,会在当前目录下生成这两个文件:
info.liangsc.pb.h info.liangsc.pb.cc
OK,我们一个Writer和Reader类来操作一下。Writer负责修改对象的值,并把对象存储在一个二进制文件中。Reader负责从二进制文件中读取消息并打印到shell上
Writer.cc
#include "info.liangsc.pb.h"#include <iostream>#include <ios>#include <fstream>using std::cout;using std::cin;int main(int argc,char **argv){ info::liangsc msg; msg.set_name("mark liang"); msg.set_age(41); msg.set_email("liangshaocong1994@gmail.com"); //write the new info to disk std::fstream output("./info",std::ios::out | std::ios::trunc | std::ios::binary); if(!msg.SerializeToOstream(&output)){ std::cerr<<"Serialize object failure"<<std::endl; return -1; } return 0;}
Reader.cc
#include "info.liangsc.pb.h"#include <google/protobuf/message.h>#include <fstream>#include <ios>#include <iostream>using std::cout;using std::cin;using std::ios;void ListMsg(const info::liangsc &guy){ cout<<guy.name()<<std::endl; cout<<guy.age()<<std::endl; cout<<guy.email()<<std::endl;}int main(int argc,char **argv){ info::liangsc sc; { std::fstream input("./info",ios::in | ios::binary); if(!sc.ParseFromIstream(&input)){ std::cerr<<"Faild to parse data"<<std::endl; return -1; } } ListMsg(sc);}
编译这两个文件
++ -Wall -g ./info.liangsc.pb.cc ./Writer.cc -o Writer -I. -I/usr/local/google/protobuf -L/usr/local/lib -lprotobuf -pthread
运行Writer会把信息写进info文件里,可用hexdump查看info的内容
hexdump -C -n 1000 -s skip info
内容如下:
00000000 0a 0a 6d 61 72 6b 20 6c 69 61 6e 67 12 1b 6c 69 |..mark liang..li|00000010 61 6e 67 73 68 61 6f 63 6f 6e 67 31 39 39 34 40 |angshaocong1994@|00000020 67 6d 61 69 6c 2e 63 6f 6d a8 01 29 |gmail.com..)|0000002c
运行Reader
mark liang41liangshaocong1994@gmail.com
参考链接:
cnblog
ibm-mix
- Google Protobuf Guide
- Google Protobuf Primer (2) Language Guide
- google protobuf
- Google ProtoBuf
- protobuf (GOOGLE)
- google protobuf
- google protobuf
- 【google protobuf】
- google protobuf
- Google protobuf
- Google protoBuf
- Google Protobuf
- Google protobuf
- Google guide
- google protobuf 学习
- google ProtoBuf开发者指南
- google protobuf 源码解析
- google ProtoBuf开发者指南
- cmd中的call,start,以及直接调用bat文件的区别
- Android微信支付
- Linux-常用命令例子
- 【Windows】【Scala + Spark】【Eclipse】单机开发环境搭建 - 及示例程序
- vs2013 获取cpu温度
- Google Protobuf Guide
- JS判断手机类型,及是否在微信内打开
- App调取手机系统拨打电话
- eclipse快捷键
- python控制用户输入
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- css基础知识
- Eclipse中快捷键Ctrl + Alt + 向下箭头与Windows冲突解决
- 4家外国支持支付宝支付的域名注册商