google的protocol buffers 对象的序列化 for java
来源:互联网 发布:淘宝找回手机真的假的 编辑:程序博客网 时间:2024/05/23 12:32
前言:
protobuf确实比JSON快很多倍,看下面的图就知道了。
环境:
win7 x64
eclipse 4.3
protoc-2.5.0
安装包下载:
https://code.google.com/p/protobuf/downloads/list
2.6+的版本已转到GitHub上,下载地址是:
https://github.com/google/protobuf/releases
1.生成jar包
下载ProtoBuf包和生成protobuf-2.5.0.jar文件。下载protoc-2.5.0-win32.zip和protobuf-2.5.0.zip,两个文件都解压缩,将protoc-2.5.0-win32中的protoc.exe文件拷贝到protobuf-2.5.0\src目录下,然后进入protobuf-2.5.0\java,执行 mvn install,编译完成后可以在protobuf-2.5.0\java\target目录中找到protobuf-2.5.0.jar文件。
2.建立eclipse项目
a. protoc.exe 放在工程的根目录下面
b. protobuf-java-2.5.0.jar 放在lib下面
c. 新建一个文件夹proto存放proto文件
d. 在proto文件夹下新建一个文件msg.proto,写入以下内容:
option java_package = "com.jamesfen.protobuf"; option java_outer_classname = "PersonProbuf"; 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; message CountryInfo { required string name = 1; required string code = 2; optional int32 number = 3; }} message AddressBook { repeated Person person = 1; }
3.产生protobuf JAVA类
进入 cd E:/Git/myhadoop2.x/myhadoop2.x文件夹
执行:
protoc --java_out=./src/main/java ./proto/msg.proto
在包package com.jamesfen.protobuf;下面会找到类PersonProbuf
4.序列化反序列化测试:
package com.jamesfen.protobuf;import java.util.List;import com.google.protobuf.InvalidProtocolBufferException;import com.jamesfen.protobuf.PersonProbuf;import com.jamesfen.protobuf.PersonProbuf.Person;import com.jamesfen.protobuf.PersonProbuf.Person.PhoneNumber;public class TestProtobuf {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubPersonProbuf.Person.Builder builder = PersonProbuf.Person.newBuilder();builder.setEmail("kkk@email.com");builder.setId(1);builder.setName("TestName");builder.addPhone(PersonProbuf.Person.PhoneNumber.newBuilder().setNumber("131111111").setType(PersonProbuf.Person.PhoneType.MOBILE));builder.addPhone(PersonProbuf.Person.PhoneNumber.newBuilder().setNumber("011111").setType(PersonProbuf.Person.PhoneType.HOME));Person person = builder.build();byte[] buf = person.toByteArray();try {Person person2 = PersonProbuf.Person.parseFrom(buf);System.out.println(person2.getName() + ", " + person2.getEmail());List<PhoneNumber> lstPhones = person2.getPhoneList();for (PhoneNumber phoneNumber : lstPhones) {System.out.println(phoneNumber.getNumber());}} catch (InvalidProtocolBufferException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(buf);}}
5. 输出:
TestName, kkk@email.com
131111111
011111
[B@472a2a50
6.demo源码下载
https://github.com/Bellonor/myhadoop2.x
- google的protocol buffers 对象的序列化 for java
- Protocol Buffers for Java
- Google Protocol Buffers 就是我想要的
- Protocol Buffers:Google 的数据交换格式
- Protocol Buffers (Google 专用的通讯协议)
- [Hadoop]Google 的Protocol Buffers 入门
- thrift, Protocol Buffers and MsgPack 的序列化对比
- thrift, Protocol Buffers and MsgPack 的序列化对比
- Google Protocol Buffers 2.3.0 for java 快速开始
- Google Protocol Buffers 2.3.0 for java 快速开始
- Google Protocol Buffers Java实例
- Google Protocol Buffers Java实例
- protocol buffers 序列化实例
- Useful links for mastering Google Protocol Buffers
- 在C++中使用Google的“Protocol Buffers”
- Google Protocol Buffers在linux环境下的安装
- Google Protocol Buffers在linux环境下的安装
- Google protocol-buffers java版--简单使用
- secure CRT上传下载文件
- 写爬虫程序
- NVelocity使用在.net(c#)
- 锁屏上显示Activity
- 时间单位换算表
- google的protocol buffers 对象的序列化 for java
- nyoj 236 心急的c小加 【LIS】
- VisualSVN Server仓库迁移到Linux(包含所有版本, 权限,用户信息)
- Jenkins——简介
- 恶补web之六:javascript知识(2)
- 死亡十字寓意:全球经济与股市背离
- oracle表重命名
- linux 切换用户之后变成-bash-3.2$ :
- arm linux 启动流程