protobuf的简单使用
操作系统 : CentOS7.3.1611_x64
gcc版本 :4.8.5
go 版本 : go1.8.3 linux/amd64
Python 版本 : 2.7.5
libprotoc : 2.5.0
Protobuf是Google开发一种数据描述语言,能够将结构化数据序列化,可用于数据存储、通信协议等方面。
首页: https://developers.google.com/protocol-buffers/
文档: https://developers.google.com/protocol-buffers/docs/overview
github地址: https://github.com/google/protobuf
这里记录了C++、Go和Python语言的简单使用示例,更多内容请参考官网手册。
准备工作
安装protobuf:
yum install protobuf protobuf-compiler protobuf-c-devel protobuf-devel
写proto文件(addr.book.proto):
package addr;
message book
{
required int32 id = ;
required string str = ;
optional int32 opt = ;
}
C++示例
写文件操作(writer.cpp):
https://github.com/mike-zhang/cppExamples/blob/master/protobufOpt/example1/writer.cpp
读文件操作(reader.cpp):
https://github.com/mike-zhang/cppExamples/blob/master/protobufOpt/example1/reader.cpp
Makefile文件:
CC = g++
CFLAGS = -g -O2 -Wall
SRC_DIR=.
DST_DIR=libs all:
make genproto
make writer
make reader genproto:
mkdir -p $(DST_DIR)
protoc -I=$(SRC_DIR) --cpp_out=$(DST_DIR) addr.book.proto writer:
$(CC) -o writer writer.cpp $(DST_DIR)/addr.book.pb.cc -I$(DST_DIR) -lprotobuf reader:
$(CC) -o reader reader.cpp $(DST_DIR)/addr.book.pb.cc -I$(DST_DIR) -lprotobuf clean:
rm -rf $(DST_DIR)
rm -f writer reader log
rm -f *.o
运行效果:
[root@localhost proto1]# ./writer
[root@localhost proto1]# ./reader book1
[root@localhost proto1]#
Go示例代码
安装goprotobuf:
yum install golang-googlecode-goprotobuf.x86_64
将proto文件转换为go代码(genPbCode.sh):
#! /bin/sh mkdir -p src/addr
protoc -I=. --go_out=src/addr addr.book.proto
修改protobuf路径:
vi src/addr/addr.book.pb.go import proto "code.google.com/p/goprotobuf/proto" 修改为: import proto "github.com/golang/protobuf/proto"
添加PATH(临时使用时可以这么操作):
export GOPATH=$GOPATH:$PWD
写文件操作(write.go):
https://github.com/mike-zhang/goExamples/blob/master/protobufOpt/protoTest1/write.go
读文件操作(reader.go):
https://github.com/mike-zhang/goExamples/blob/master/protobufOpt/protoTest1/read.go
运行效果:
[root@localhost proto3]# go run write.go
[root@localhost proto3]# go run read.go
id: str:"testMsg11"
[root@localhost proto3]#
Python示例
需要安装protobuf包:
pip install protobuf
将proto文件转换为python代码(genPbCode.sh):
#! /bin/sh protoc -I=. --python_out=. addr.book.proto
touch addr/__init__.py
写文件操作(write.py):
https://github.com/mike-zhang/pyExamples/blob/master/protobufOpt/example1/write.py
读文件操作(reader.py):
https://github.com/mike-zhang/pyExamples/blob/master/protobufOpt/example1/reader.py
运行效果:
(py27env) [root@localhost proto2]# ./genPbCode.sh
(py27env) [root@localhost proto2]# python write.py
(py27env) [root@localhost proto2]# python reader.py
id:
str: "testMsg1" (py27env) [root@localhost proto2]#
好,就这些了,希望对你有帮助。
本文github地址:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2018/20180201_protobuf的简单使用.rst
欢迎补充
protobuf的简单使用的更多相关文章
- Protobuf的简单介绍、使用和分析
Protobuf的简单介绍.使用和分析 一.protobuf是什么? protobuf(Google Protocol Buffers)是Google提供一个具有高效的协议数据交换格式工具库( ...
- Centos6.5下安装protobuf及简单使用
1.protobuf是google公司提出的数据存储格式,详细介绍可以参考:https://code.google.com/p/protobuf/ 2.下载最新的protobuf,下载地址:https ...
- Centos6.4下安装protobuf及简单使用
1.protobuf是google公司提出的数据存储格式,详细介绍可以参考:https://code.google.com/p/protobuf/ 2.下载最新的protobuf,下载地址:https ...
- .NET序列化工具Jil、Json.NET和Protobuf的简单测评
前一段时间逛园子的时候发现有人比较了Jil.Json.NET和Protobuf的性能,一时好奇,也做了个测试,这里记录下来,以供查阅. 前期准备 依赖类库的话,可以通过Nuget在公共组件库总下载,这 ...
- Unity手游之路<一>C#版本Protobuf
http://blog.csdn.net/janeky/article/details/17104877 个游戏包含了各种数据,包括本地数据和与服务端通信的数据.今天我们来谈谈如何存储数据,以及客户端 ...
- (转)RabbitMQ消息队列的小伙伴: ProtoBuf(Google Protocol Buffer)
什么是ProtoBuf? 一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩 ...
- protobuf 中的嵌套消息的使用
protobuf的简单的使用,不过还留下了一个问题,那就是之前主要介绍的都是对简单数据的赋值,简单数据直接采用set_xx()即可,但是如果不是简单变量而是自定义的复合类型变量,就没有简单的set函数 ...
- RabbitMQ消息队列的小伙伴: ProtoBuf(Google Protocol Buffer)
什么是ProtoBuf? 一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩 ...
- SpringBoot整合Netty并使用Protobuf进行数据传输(附工程)
前言 本篇文章主要介绍的是SpringBoot整合Netty以及使用Protobuf进行数据传输的相关内容.Protobuf会简单的介绍下用法,至于Netty在之前的文章中已经简单的介绍过了,这里就不 ...
随机推荐
- centos7下docker启动失败解决
docker启动失败解决 could not change group /var/run/docker.sock to docker: gr... not found 如果出现:Job for doc ...
- 解决UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position
最近用Python写了些爬虫,在爬取一个gb2312的页面时,抛出异常: UnicodeEncodeError: 'ascii' codec can't encode characters in po ...
- Java实现Windows、Mouse监听器
1.通过实现WindowListener接口来实现Windows监听器: import java.awt.event.WindowEvent; import java.awt.event.Window ...
- photoshop实现倾斜图片的修正
第一天学习Photoshop,了解到了Photoshop对图片的处理,下面是实现一个倾斜图片修正的两种方法: 举例图片: 第一种方法:1.利用吸管中的标尺工具量倾斜度数: 2.利用旋转图像,旋转对应的 ...
- Java -- 内部类(一)
什么是内部类 将一个类的定义放在另一个类的定义内部,这就是内部类.在Java中内部类主要分为成员内部类.局部内部类.匿名内部类.静态内部类.举个栗子: public class A { public ...
- Spring使用笔记(三) 高级装配
高级装配 一.环境与Profile 一)配置profile bean 环境的改变导致配置改变(需求:通过环境决定使用哪个bean),可以通过Spring的Profile解决. Profile可以在程序 ...
- 区间dp板子题:[noi1995]石子合并
非常经典的区间dp模板 对于每一个大于二的区间 我们显然都可以将它拆分成两个子序列 那么分别计算对于每个取最优值即可 #pragma GCC optimize("O2") #inc ...
- YIi url美化
一.Yii Url美化,配置urlManager组件 'urlManager' => [ 'enablePrettyUrl' => true, // 开启URL美化,可以去掉 index. ...
- day6常用模块,数据库操作
一.循环调用函数 map() 二.列表推导式和生成器 三.filter过滤器 四.os模块 五.datetime模块 六.random模块 七.写日志,导入nnlog模块 八,发邮件 九,操作数 ...
- JAVA自学笔记24
JAVA自学笔记24 1.能使用同步代码块就使用同步代码块,除非锁对象是this,就可以考虑使用同步方法.静态方法的锁是类的字节码对象. 2.JDK5新特性 1)接口Lock void Lock()/ ...