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 ...
随机推荐
- Logstash安装和使用
Logstash 是开源的服务器端数据处理管道,能够同时 从多个来源采集数据.转换数据,然后将数据发送到您最喜欢的 “存储库” 中.(我们的存储库当然是 Elasticsearch.) 作用:集中.转 ...
- 某公司面试java试题之【二】,看看吧,说不定就是你将要做的题
这次做的题是在是太多了,五页呢,吓死宝宝了!
- WPS 关闭 wpscenter.exe 服务
1.Ctrl + Shift + Esc 打开任务管理,结束wps相关的进程 2.新建文本文件,并命名为:wpscenter.exe 3.重命名 C:\Program Files1\WPS Offic ...
- ubuntu14.04 terminator字体挤在一起问题
字体挤在一起:在ubuntu下请选择mono后缀的字体就可以了 右键—>首选项—>profile—>general—>字体设置成ubuntu mono 或Free mono
- CF 1131C Birthday
C. Birthday time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- ASP.NET MVC+HighCharts开发统计图表
HighCharts是开源的Web图表js组件,与D3.js一样,经常用于数据可视化.HighCharts图表类型丰富,功能非常强大,是很好的数据可视化解决方案,其官方网站为:http://www.h ...
- A - 不要62
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍 ...
- ubuntu安装sublime-text
按照网上的教程, wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add - sudo apt ...
- easyui equals验证代码
在使用easyui textbox进行相等验证时却没有效果,经查询原来官方代码中没有提供equals验证的方法,搜了一个加上去就OK了: // extend the 'equals' rule $.e ...
- js方法的积累
对字符串编码解码编码 encodeURIComponet(str); 解码 decodeURIComponet(str); 及时反应的方法,监听input值是否发生改变$('#username') ...