protocol buffer简介
一、protocol buffer简介
protocol buffer(简称PB)是google开源的一个数据序列化与反序列化工具,由于其支持多种语言、各种平台,多被用于对象的存储,远程调用等方向。
用户首先定义对象的结构,根据结构生成对应语言的源码,然后就可以在各种语言中使用PB将数据进行序列化和反序列化。
二、protocol buffer初步使用
下面是一个简单的使用的例子:
首先需要定义一个.proto文件,其中需要定义你希望操作的对象的结构。
message Person {
required string name = 1;
required int32 id = 2;
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;
}
保存为person.proto文件,之后下载protoc编译工具,并解压,使用PB将proto文件生成java类。
protoc.exe --java_out=. person.proto
在指定的java_out目录下就可以生成java对应的类,这里生成了PersonOuterClass.java。将文件放入项目,并引入PB的jar包。
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.1.0'
接下来就可以使用PB进行对象的操作了。
public static void main(String[] args) throws Exception{
//创建对象
PersonOuterClass.Person p= PersonOuterClass.Person.newBuilder().setName("my_name").setId(2).build();
System.out.print(p.toString());
//序列化对象
FileOutputStream fos=new FileOutputStream("D://person");
p.writeTo(fos);
fos.close();
//反序列化对象
FileInputStream fis=new FileInputStream("D://person");
PersonOuterClass.Person pread=PersonOuterClass.Person.parseFrom(fis);
System.out.println(pread.toString());
fis.close();
}
三、与java原生的serializable接口进行比较
public class JavaPerson implements Serializable{
public String name;
public Integer id;
public String email;
public enum PhoneType{
MOBILE,HOME,WORK
}
public class PhoneNumber{
public String number;
public PhoneType type;
}
List<PhoneNumber> phone;
public static void main(String[] args) throws Exception{
JavaPerson jp=new JavaPerson();
jp.name="my_name";
jp.id=2;
FileOutputStream fileOut = new FileOutputStream("d://person.ser");
ObjectOutputStream outStream = new ObjectOutputStream(fileOut);
outStream.writeObject(jp);
outStream.close();
fileOut.close();
FileInputStream fileIn =new FileInputStream("d://person.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
jp = (JavaPerson) in.readObject();
in.close();
fileIn.close();
}
}
运行比较结果:
- PB较java默认形式代码更简洁。
- 循环运行10000次序列化和反序列化后PB比java快约30%。
- 生成的文件PB有11字节而java有215字节。
- PB提供额外的字段校验支持。
protocol buffer简介的更多相关文章
- google protocol buffer 简介 版本 安装 使用 实例
一.简介 protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.google 提供了三种语言的实现:java.c++ 和 python,每一种实现 ...
- protocol buffer使用简介
之前在工作中用到了protocol buffer(此处简称PB)(主要对数据进行序列化与反序列化,方便网络传输中的编解码),之后发现这是一个好东西,在此稍微记录下该工具如何使用,方便以后查阅 官网地址 ...
- Google Protocol Buffer 的使用和原理[转]
本文转自: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构 ...
- Google Protocol Buffer 的使用
简介 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 ...
- Google Protocol Buffer 的使用和原理
Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...
- Google Protocol Buffer 协议
1. Protocol Buffers 简介 Protocol Buffers (ProtocolBuffer/ protobuf )是Google公司开发的一种数据描述语言,类似于XML能够将结构化 ...
- Protocol Buffer使用
Protocol Buffer使用简介 字数2630 阅读5067 评论1 喜欢12 我们项目中使用protocol buffer来进行服务器和客户端的消息交互,服务器使用C++,所以本文主要描述pr ...
- 【Google Protocol Buffer】Google Protocol Buffer
http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Google Protocol Buffer 的使用和原理 Protocol Buffers ...
- iOS 开发之 protocol Buffer 数据交换
前言: 从 14 年公司做项目时开始接触 Google 的 protocol Buffer,用了一段时间,后来到新公司就没有机会再使用了,趁着还没完全忘记,记录下. 简介:protocolbuffer ...
随机推荐
- hdoj:2048
#include <iostream> using namespace std; ]; ]; int main() { int C; a[] = ; a[] = ; b[] = ; b[] ...
- Tomcat中的backlog参数
在linux 2.2以前,backlog大小包括了半连接状态和全连接状态两种队列大小.linux 2.2以后,分离为两个backlog来分别限制半连接SYN_RCVD状态的未完成连接队列大小跟全连接E ...
- [原创]Fashion汽车定位器拆解
随着共享单车的爆发增长,定位方案被彻底激活.当下主流的共享单车都采用了MTK2503的方案(后续再详细分解),本文针对某商城热卖的汽车定位器进行拆解分析. 第一部分,定位器外观. 第二部分,拆解开壳, ...
- featureCounts 软件说明
featuresCounts 软件用于定量,不仅可以支持gene的定量,也支持exon, gene bodies, genomic bins, chromsomal locations的定量: 官网 ...
- python机器学习包 Windows下 pip安装 scikit-learn numpy scipy
1.到PIP的目录中C:\Python34\Scripts;2. 2.1 pip安装numpy pip install numpy 2.2 pip安装sklearn pip install -U ...
- css布局 - 工作中常见的两栏布局案例及分析
突然想到要整理这么一篇平时工作中相当常见但是我们又很忽视的布局的多种处理方法.临时就在我经常浏览的网站上抓的相对应的截图.(以后看到其他类型的我再补充) 既然截了图,咱们就直接看人家使用的布局方式,毕 ...
- C# windows GDI+仿画图 绘图程序设计
C# windows GDI+仿画图 绘图程序设计 1.介绍 这里分享一个简单的画图程序 原作者:author: ping3108@163.com 2.程序主窗体设计 3.程序设计 本程序工程使用VS ...
- cf 938E
哇自闭了. 一样个毛啊. 和之前见过的几道感觉很类似啊. 首先一个数如果有贡献那么在他后面一定有一个大于它的数,并且前面的全比他小,然后我就跑偏了... 于是我们先排个序,显然无影响,我们可以考虑从 ...
- [Codeforces Round #507][Codeforces 1039C/1040E. Network Safety]
题目链接:1039C - Network Safety/1040E - Network Safety 题目大意:不得不说这场比赛的题面真的是又臭又长...... 有n个点,m条边,每个点有对应的权值c ...
- mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
错误情况 解决办法: 首先查看mysql的命令 其次修改root用户的密码 set password for 'root'@'localhost' = password('123456'); 最后退出 ...