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 ...
随机推荐
- WordPress 3.8.1 /xmlrpc.php拒绝服务漏洞
漏洞版本: WordPress 3.8.1 漏洞描述: WordPress是一款内容管理系统. WordPress 3.8.1 /xmlrpc.php 文件有ping其他主机的功能,通过这个功能可以请 ...
- linux watchdog demo hacking
/********************************************************************** * linux watchdog demo hackin ...
- 阿里云至 Windows Azure 的 Linux 虚拟机迁移
在Windows Azure中,用户可以对部署在Azure中的虚拟机的映像.磁盘以及快照进行生成和下载.用户可以方便地将Azure中的虚拟机实例迁移到本地.私有云甚至其他公有云平台进行测试.扩展或者再 ...
- 剑指Offer:打印从1到最大的n位数
题目:输入数值n,按顺序打印从1到最大的n位数,例如输入n=3,则从1,2,3,一直打印到999 陷阱:若使用循环遍历 1- 999...9 并依次输出,当位数n过大时,无论将其存入int或long或 ...
- Owasp Top 10 Security Risks for 2014
A1-互联网泄密事件/撞库攻击 以大量的用户数据为基础,利用用户相同的注册习惯(相同的用户名和密码),尝试登陆其它的网站.2011年,互联网泄密事件引爆了整个信息安全 界,导致传统的用户+密码认证的方 ...
- SQL Server 2005如何远程连接数据库?
SQL Server 2005如何远程连接数据库? 方法/步骤 1 在配置工具中的服务和远程连接的外围应用配置器 --->远程连接-->本地连接和远程连接-->同时使用TCP/I ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- JavaScript基本概念(二)
JavaScript 基本概念(二) 操作符和语句 目录 操作符 一元操作符 位操作符 布尔操作符 乘性操作符 其他操作符 语句部分 说起操作符,回忆下上一篇文章末尾说的话. 操作符 一元操作符 ++ ...
- vs2013下git的使用
一.将VS2013项目托管到Git 现在的开源项目越来越多,我们会注意到这些开源项目大多共享在GitHub上面,包括微软开源的项目也在Github上,当然,如果你用过Git后,会发现Git确实不错,其 ...
- ACM2040
关于亲和数的详细解释如下: http://www.kepu.net.cn/gb/basic/szsx/8/8_83/8_83_1004.htm /* 亲和数 时间限制:2000/1000 MS(JAV ...