一、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简介的更多相关文章

  1. google protocol buffer 简介 版本 安装 使用 实例

    一.简介 protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.google 提供了三种语言的实现:java.c++ 和 python,每一种实现 ...

  2. protocol buffer使用简介

    之前在工作中用到了protocol buffer(此处简称PB)(主要对数据进行序列化与反序列化,方便网络传输中的编解码),之后发现这是一个好东西,在此稍微记录下该工具如何使用,方便以后查阅 官网地址 ...

  3. Google Protocol Buffer 的使用和原理[转]

    本文转自: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构 ...

  4. Google Protocol Buffer 的使用

    简介 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 ...

  5. Google Protocol Buffer 的使用和原理

    Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...

  6. Google Protocol Buffer 协议

    1. Protocol Buffers 简介 Protocol Buffers (ProtocolBuffer/ protobuf )是Google公司开发的一种数据描述语言,类似于XML能够将结构化 ...

  7. Protocol Buffer使用

    Protocol Buffer使用简介 字数2630 阅读5067 评论1 喜欢12 我们项目中使用protocol buffer来进行服务器和客户端的消息交互,服务器使用C++,所以本文主要描述pr ...

  8. 【Google Protocol Buffer】Google Protocol Buffer

    http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Google Protocol Buffer 的使用和原理 Protocol Buffers ...

  9. iOS 开发之 protocol Buffer 数据交换

    前言: 从 14 年公司做项目时开始接触 Google 的 protocol Buffer,用了一段时间,后来到新公司就没有机会再使用了,趁着还没完全忘记,记录下. 简介:protocolbuffer ...

随机推荐

  1. 蜕变成蝶~Linux设备驱动之字符设备驱动

    一.linux系统将设备分为3类:字符设备.块设备.网络设备.使用驱动程序: 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面向流 ...

  2. 手写一个selenium浏览器池

    维护一组浏览器,实现每分钟1000次查询.DriverPool使用变幻版只初始化一次的单例模式.维护每个浏览器的当前是否使用的状态. 不需要等待请求来了,临时开浏览器,开一个浏览器会耽误6秒钟. 可以 ...

  3. Linux下常见命令

    =============挂载和登陆命令======================================== Mount:挂载命令. 比方挂载光驱mount /dev/cdrom /mnt ...

  4. 《转载》强大全面的C++框架和库推荐!

    C++ 资源大全 关于 C++ 框架.库和资源的一些汇总列表,内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了STL容器,算法和 ...

  5. http://www.rehack.cn/techshare/webbe/php/3391.html

    首先配置好本地PHPstudy环境: 默认在D:\phpStudy\php\php-7.0.12-nts\ext目录下有php_pdo_sqlsrv_7_nts_x86.dll.php_sqlsrv_ ...

  6. scala 隐式详解(implicit关键字)

    掌握implicit的用法是阅读spark源码的基础,也是学习scala其它的开源框架的关键,implicit 可分为: 隐式参数 隐式转换类型 隐式调用函数 1.隐式参数 当我们在定义方法时,可以把 ...

  7. layui---form表单模块

    虽然对layui比较熟悉了,但是今天有时间还是将layui的form表单模块重新看一下. https://www.layui.com/doc/modules/form.html 一.更新渲染 layu ...

  8. HttpSession的认识

    package javax.servlet.http; import java.util.Enumeration; import javax.servlet.ServletContext; publi ...

  9. Mac os的使用

    来北京入职java开发实习,公司标配macook.一开始不会使用macos系统,用起来很不适应,我是拒绝的.但是leader说mac是开发效率最高的工具了·.一开我很怀疑,后来觉得mac系统用起来还真 ...

  10. php代码画足球场

    用代码画了个足球场 原图: 代码画出的效果图: 代码如下: // 创建一个 200X200 的图像 $img = imagecreate(800, 500); // 分配颜色 $bg = imagec ...