protocol buffer的简单使用
protocol buffer是一个高效的结构化数据存储格式,用来结构化数据的序列化与反序列化。目前支持java、c++、Python
相对于json而言:
数据量跟小
其他的还没看出什么优势
下载地址:
protobuf-2.5,protoc-2.5.0-win32.zip
安装过程:
1、进入解压后的java目录,查看readme.txt
2、把protoc.exe放入到protobuf-2.5中的
3、运行mvn -test
编写protocol buffer需要以下三步
1、定义消息格式文件,以proto结尾
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "PersonProtos"; message Person{
required string name=1;
required int32 id=2;
optional string email=3; message PhoneNumber{
required string number = 1;
optional int32 type=2;
} repeated PhoneNumber phone=4; }
2、使用编译器生成java文件
protoc --java_out=. person.proto
3、使用protocol buffer提供的api编写应用程序
package com.example.tutorial; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import com.example.tutorial.PersonProtos.Person;
import com.example.tutorial.PersonProtos.Person.PhoneNumber; public class ProtocolBufferExample {
public static void main(String[] args) {
Person person = Person.newBuilder()
.setName("zhengqun")
.setEmail("717401115@qq.com")
.setId(111)
.addPhone(PhoneNumber.newBuilder().setNumber("15351506736").setType(1))
.addPhone(PhoneNumber.newBuilder().setNumber("17751544242").setType(2))
.build(); FileOutputStream out = null;
try {
out = new FileOutputStream("example.txt");
person.writeTo(out);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} FileInputStream in = null;
try {
in = new FileInputStream("example.txt");
Person p = Person.parseFrom(in);
System.out.println("person2:" + p);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}
protocol buffer的简单使用的更多相关文章
- Google Protocol Buffer 简单介绍
以下内容主要整理自官方文档. 为什么使用 Protocol Buffers .proto文件 Protocol Buffers 语法 编译.proto文件 Protocol Buffers API 枚 ...
- 从零开始山寨Caffe·伍:Protocol Buffer简易指南
你为Class外访问private对象而苦恼嘛?你为设计序列化格式而头疼嘛? ——欢迎体验Google Protocol Buffer 面向对象之封装性 历史遗留问题 面向对象中最矛盾的一个特性,就是 ...
- [原创翻译]Protocol Buffer Basics: C#
Protocol Buffer 基础知识:c# 原文地址:https://developers.google.com/protocol-buffers/docs/csharptutorial ...
- 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之概述
XML这种属于非常强大的一种格式,能存储任何你想存的数据,而且编辑起来还是比较方便的.致命的缺陷在于比较庞大,在某些情况下,序列化和解析都会成为瓶颈.这种对于实时性很强的应用来说,就不太适合了,想象下 ...
- Ggoogle Protocol Buffer的使用 (基于C++语言)
首先说明的是Protocol Buffle是灵活高效的.它的一个很好的优点(很重要的,我认为)就是后向兼容性--当我们扩展了了.proto文件后,我们照样可以用它来读取之前生成的文件. 之前已经写了关 ...
- Google Protocol Buffer的安装与.proto文件的定义
什么是protocol Buffer呢? Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准. 我理解的就是:它是一种轻便高效的结构 ...
- Protocol Buffer技术
转载自http://www.cnblogs.com/stephen-liu74/archive/2013/01/02/2841485.html 该系列Blog的内容主体主要源自于Protocol Bu ...
随机推荐
- OpenSSH 'child_set_env()'函数安全绕过漏洞
漏洞版本: OpenSSH 6.x 漏洞描述: Bugtraq ID:66355 CVE ID:CVE-2014-2532 OpenSSH是一种开放源码的SSH协议的实现. OpenSSH " ...
- mysql CMAKE 参数说明
MySQL自5.5版本以后,就开始使用CMake编译工具了,因此,你在安装源文件中找不到configure文件是正常的.很多人下到了新版的MySQL,因为找不到configure文件,不知道该怎么继续 ...
- NGINX(七)分段下载
前言 nginx分段下载通过ngx_http_range_filter_module模块进行处理,关于HTTP分段下载过程,可以参考HTTP分段下载一文,主要分为一次请求一段和一次请求多段 涉及数据结 ...
- HDU4289 Control 最大流
经典题,求去掉若干个点,使得两个点不在连通,总价值最少 所以拆点最小割,除了拆点边,流量都为无穷,拆点边是流量为价值 #include <iostream> #include <cs ...
- HDU 5637 Transform 搜索
题意:bc round 74 div1 1002 中文题 分析(官方题解):注意到答案实际上只和s⊕t有关, bfs预处理下从0到xx的最短步数, 然后查询O(1)回答即可. #include < ...
- (原创)win7自带IIS7.5+php7.0.10安装教程(图)
php在上周8月18日发布了PHP 7.0 (7.0.10)版本.详细下载页面http://windows.php.net/download/,根据自身电脑配置情况酌情下载版本.win7旗舰版,iis ...
- python网络编程(六)---web客户端访问
1.获取web页面 urllib2 支持任何协议的工作---不仅仅是http,还包括FTP,Gopher. import urllib2 req=urllib2.Request('http://www ...
- input子系统 KeyPad-Touch上报数据格式与机制
-----------------------------------------------------------------------本文系本站原创,欢迎转载!转载请注明出处:http://b ...
- EasyUI + Spring MVC + hibernate实现增删改查导入导出
(这是一个故事--) 前言 作为一个JAVA开发工程师,我觉得最基本是需要懂前端.后台以及数据库. 练习的内容很基础,包括:基本增删改查.模糊查询.分页查询.树菜单.上传下载.tab页 主管发我一个已 ...
- Redis+MongoDB 最佳实践 做到读写分离 -摘自网络
方案1. (被否定) 加上Redis,做到MongoDB的读写分离,单一进程从MongoDB及时把任务同步到Redis中. 看起来很完美,但是上线后出现了各种各样的问题,列举一下: 1.Redis队列 ...