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介绍及例子的更多相关文章

  1. Protocol buffers 介绍

    Protocol buffers和mxl一样在序列化数据结构时很灵活.高效和智能,但是它的优势在于定义文件更小,读取速度更快,使用更加简单.目前protocol buffers支持C++.java和p ...

  2. 开源点评:Protocol Buffers介绍

    今天来介绍一下“Protocol Buffers”(下面简称protobuf)这个玩意儿.本来俺在构思“生产者/消费者模式 ”系列的下一个帖子:关于生产者和消费者之间的传输数据格式.因为里面扯到了pr ...

  3. Google Protocol Buffers介绍

    简要介绍和总结protobuf的一些关键点,从我之前做的ppt里摘录而成,希望能节省protobuf初学者的入门时间.这是一个简单的Demo. Protobuf 简介 Protobuf全称Google ...

  4. Protocol Buffers介绍

    基本概念 Protocol Buffers(以下简称PB)是一种独立于语言.独立于开发平台.可扩展的序列化数据结构框架,它常常被用在通信.数据序列化保存等方面. PB是一种敏捷.高效.自动化的用于对数 ...

  5. Protocol Buffers编码详解,例子,图解

    Protocol Buffers编码详解,例子,图解 本文不是让你掌握protobuf的使用,而是以超级细致的例子的方式分析protobuf的编码设计.通过此文你可以了解protobuf的数据压缩能力 ...

  6. Protocol Buffers(Protobuf) 官方文档--Protobuf语言指南

    Protocol Buffers(Protobuf) 官方文档--Protobuf语言指南 约定:为方便书写,ProtocolBuffers在下文中将已Protobuf代替. 本指南将向您描述如何使用 ...

  7. Protocol Buffers(Protobuf)开发者指南---概览

    Protocol Buffers(Protobuf)开发者指南---概览 欢迎来到protocol buffers的开发者指南文档,protocol buffers是一个与编程语言无关‘.系统平台无关 ...

  8. Protocol Buffers

    今天来介绍一下"Protocol Buffers"(以下简称protobuf)这个玩意儿.本来俺在构思"生产者/消费者模式"系列的下一个帖子:关于生产者和消费者 ...

  9. Protocol Buffers(2):编码与解码

    目录 Message Structure 解码代码一窥 varint Protobuf中的整数和浮点数 Length-delimited相关类型 小结 参考 博客:blog.shinelee.me | ...

随机推荐

  1. 38-解决Fiddler查看Post参数中文乱码的问题

    转载自:https://blog.csdn.net/JusterDu/article/details/50888617 解决Fiddler查看Post参数中文乱码的问题 2016年03月14日 18: ...

  2. ubuntu 12.04安装jdk 8

    转载:http://www.itnose.net/detail/6196130.html Ubuntu12.4安装jdk1.8 1.要安装的jdk,我把它拷在了共享文件夹里面.    (用优盘拷也可以 ...

  3. Windows c++程序的基本结构

    Windows c++程序的基本结构 1.一个完整的Windows应用程序通常由五种类型的文件组成 C语言源程序文件 头文件 模块定义文件 资源描述文件 项目文件 2.Windows应用程序构成基本框 ...

  4. 在sublime text中添加JavaScript的build-system

    -step 1: 下载安装node.js, 并添加到path变量中. -step 2: 在sublime text中新建一个build-system. tools --> build-syste ...

  5. Web图片编辑控件开发文档-Xproer.ImageEditor

    版权所有 2009-2014 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com 产品首页:http://www.ncmem.com/webplug/image-e ...

  6. RocketMQ 运维指令

    1.1. 控制台使用 RocketMQ 提供有控制台及一系列控制台命令,用于管理员对主题,集群,broker 等信息的管理 登录控制台 首先进入RocketMQ 工程,进入/RocketMQ/bin ...

  7. 机器学习—K近邻

    一.算法原理 还是图片格式~ 二.sklearn实现 import pandas as pd import numpy as np import matplotlib.pyplot as plt im ...

  8. Concurrent Request:Inactive phase,No Manager status

    Symptom 随便submit一个request,发现几乎所有的Concurrent Manager都为No Manager状态,Phase为Inactive. Solution 一个Concurr ...

  9. Android-HttpsURLConnectionHelp-工具类

    HttpsURLConnectionHelp-工具类 是专门把javax.net.ssl.HttpsURLConnection类的使用,进行了封装,提供了常用的公共方法: package common ...

  10. Android-Camera+SurfaceView

    Camera相机是属于硬件,每台设备的Camera硬件配置的参数都是不一样的,Camera通常是用来拍照,扫描二维码等等 AndroidManifest.xml配置Camera需要的权限: <! ...