Protocol Buffers介绍及例子
Protocol Buffers介绍及例子
Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。
protobuf协议是以一个 .proto 后缀的文件为基础,这个文件通过结构化数据类型的形式描述了一个数据信息。
protobuf的序列化底层原理:
you can see, each field in the message definition has a unique numbered tag. These tags are used to identify your fields in the message binary format, and should not be changed once your message type is in use. Note that tags with values in the range 1 through 15 take one byte to encode. Tags in the range 16 through 2047 take two bytes. So you should reserve the tags 1 through 15 for very frequently occurring message elements. Remember to leave some room for frequently occurring elements that might be added in the future.
翻译为:
protobuf协议使用二进制格式表示Key字段;对value而言,不同的类型采用的编码方式也不同,如果是整型,采用二进制表示;如果是字符,会直接原样写入文件或者字符串(不编码)。
公式 field_number << 3)| wire_type,如果域号大于等于16,两个字节共16位,去掉移位的3位,去掉两个字节中第一个比特位, 总共16个比特位只有16-511个比特位用来表示Key,所以Key的域号要小于2^11 2048。
T - L - V 的数据存储方式
- Tag - Length - Value,标识 - 长度 - 字段值 存储方式
- 以 标识 - 长度 - 字段值 表示单个数据,最终将所有数据拼接成一个 字节流,从而 实现 数据存储 的功能
正式因为采用PB自身框架代码和编译器完成和独特的编码方式,才会使protobuf的序列化紧凑,效率这么高。
最初的目录结构:

各文件内容:
cat helloworld.proto
syntax = "proto2";
package lm;
message helloworld
{
required int32 id = 1;
required string str = 2;
optional int32 opt=3;
}
cat main.cpp
#include <iostream>
#include <fstream>
#include "protoc_dir/helloworld.pb.h"
using namespace std;
void ListMsg(const lm::helloworld & msg){
cout << msg.id()<<endl;
cout << msg.str()<<endl;
}
int main(void)
{
lm::helloworld msg1;
lm::helloworld msg2;
msg1.set_id(101); //设置id
msg1.set_str("helloworld"); //设置字符串
{
fstream output("./tst.log", ios::out |ios::trunc |ios::binary);
if (!msg1.SerializeToOstream(&output)){
cerr << "failed to serialize msg1" <<endl;
return -1;
}
}
{
fstream input("./tst.log",ios::in|ios::binary);
if (!msg2.ParseFromIstream(&input)){
cerr << "failed to parse" <<endl;
return -1;
}
ListMsg(msg2);
}
return 0;
}
1.根据.proto文件生成.cc .h文件
protoc --cpp_out=./protoc_dir/ helloworld.proto

2.编译源代码
g++ -std=c++11 main.cpp protoc_dir/helloworld.pb.cc -lprotobuf -lpthread -o obj_run

注意: protobuf用到了多线程, 所以一定要加上 lpthread
3.运行可执行文件

Protocol Buffers介绍及例子的更多相关文章
- Protocol buffers 介绍
Protocol buffers和mxl一样在序列化数据结构时很灵活.高效和智能,但是它的优势在于定义文件更小,读取速度更快,使用更加简单.目前protocol buffers支持C++.java和p ...
- 开源点评:Protocol Buffers介绍
今天来介绍一下“Protocol Buffers”(下面简称protobuf)这个玩意儿.本来俺在构思“生产者/消费者模式 ”系列的下一个帖子:关于生产者和消费者之间的传输数据格式.因为里面扯到了pr ...
- Google Protocol Buffers介绍
简要介绍和总结protobuf的一些关键点,从我之前做的ppt里摘录而成,希望能节省protobuf初学者的入门时间.这是一个简单的Demo. Protobuf 简介 Protobuf全称Google ...
- Protocol Buffers介绍
基本概念 Protocol Buffers(以下简称PB)是一种独立于语言.独立于开发平台.可扩展的序列化数据结构框架,它常常被用在通信.数据序列化保存等方面. PB是一种敏捷.高效.自动化的用于对数 ...
- Protocol Buffers编码详解,例子,图解
Protocol Buffers编码详解,例子,图解 本文不是让你掌握protobuf的使用,而是以超级细致的例子的方式分析protobuf的编码设计.通过此文你可以了解protobuf的数据压缩能力 ...
- Protocol Buffers(Protobuf) 官方文档--Protobuf语言指南
Protocol Buffers(Protobuf) 官方文档--Protobuf语言指南 约定:为方便书写,ProtocolBuffers在下文中将已Protobuf代替. 本指南将向您描述如何使用 ...
- Protocol Buffers(Protobuf)开发者指南---概览
Protocol Buffers(Protobuf)开发者指南---概览 欢迎来到protocol buffers的开发者指南文档,protocol buffers是一个与编程语言无关‘.系统平台无关 ...
- Protocol Buffers
今天来介绍一下"Protocol Buffers"(以下简称protobuf)这个玩意儿.本来俺在构思"生产者/消费者模式"系列的下一个帖子:关于生产者和消费者 ...
- Protocol Buffers(2):编码与解码
目录 Message Structure 解码代码一窥 varint Protobuf中的整数和浮点数 Length-delimited相关类型 小结 参考 博客:blog.shinelee.me | ...
随机推荐
- ubuntu 12.04安装jdk 8
转载:http://www.itnose.net/detail/6196130.html Ubuntu12.4安装jdk1.8 1.要安装的jdk,我把它拷在了共享文件夹里面. (用优盘拷也可以 ...
- SDL编程
一.简介 SDL是一个用C编写的跨平台的多媒体库,它通过OpenGL和Direct3D,提供了针对音频.视频.键盘.鼠标.控制杆及3D硬件的低级别的访问接口.它在MPEG播放软件.模拟器以及许多游戏中 ...
- Debian Mount nfs 出错的解决
系统未安装 nfs 客户端 #aptitude install nfs-common 解决!
- WEB开发常见错误-php无法操作数据库
Ubuntu 安装phpmyadmin 和配置 ubuntu 安装 phpmyadmin 两种 : 1: apt-get 安装 然后使用 已有的虚拟主机目录建立软连接 sudo apt-g ...
- CMD 与 ENTRYPOINT 的区别
Dockerfile里有 CMD 与 ENTRYPOINT 两个功能咋看起来很相似的指令,开始的时候觉得两个互用没什么所谓,但其实并非如此: CMD指令: The main purpose of a ...
- java中double和float精度丢失问题
为什么会出现这个问题呢,就这是java和其它计算机语言都会出现的问题,下面我们分析一下为什么会出现这个问题:float和double类型主要是为了科学计算和工程计算而设计的.他们执行二进制浮点运算,这 ...
- Work at a KFC fast food restaurant
During the summer holiday of 2005,I thought I should do some meaningful instead of at home and watch ...
- koa学习笔记
卸载node http://www.it165.net/os/html/201504/12427.html 安装 sudo npm install -g n sudo n stable 装个稳定版试试 ...
- hdu2364之BFS
Escape Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- mysql数据表简单拷贝及重命名
CREATE TABLE to LIKE from;//拷贝结构 RENAME TABLE from TO to;//重命名