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的简单使用的更多相关文章

  1. Google Protocol Buffer 简单介绍

    以下内容主要整理自官方文档. 为什么使用 Protocol Buffers .proto文件 Protocol Buffers 语法 编译.proto文件 Protocol Buffers API 枚 ...

  2. 从零开始山寨Caffe·伍:Protocol Buffer简易指南

    你为Class外访问private对象而苦恼嘛?你为设计序列化格式而头疼嘛? ——欢迎体验Google Protocol Buffer 面向对象之封装性 历史遗留问题 面向对象中最矛盾的一个特性,就是 ...

  3. [原创翻译]Protocol Buffer Basics: C#

    Protocol Buffer 基础知识:c#    原文地址:https://developers.google.com/protocol-buffers/docs/csharptutorial   ...

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

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

  5. Google Protocol Buffer 的使用

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

  6. 学习Google Protocol buffer之概述

    XML这种属于非常强大的一种格式,能存储任何你想存的数据,而且编辑起来还是比较方便的.致命的缺陷在于比较庞大,在某些情况下,序列化和解析都会成为瓶颈.这种对于实时性很强的应用来说,就不太适合了,想象下 ...

  7. Ggoogle Protocol Buffer的使用 (基于C++语言)

    首先说明的是Protocol Buffle是灵活高效的.它的一个很好的优点(很重要的,我认为)就是后向兼容性--当我们扩展了了.proto文件后,我们照样可以用它来读取之前生成的文件. 之前已经写了关 ...

  8. Google Protocol Buffer的安装与.proto文件的定义

    什么是protocol Buffer呢? Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准. 我理解的就是:它是一种轻便高效的结构 ...

  9. Protocol Buffer技术

    转载自http://www.cnblogs.com/stephen-liu74/archive/2013/01/02/2841485.html 该系列Blog的内容主体主要源自于Protocol Bu ...

随机推荐

  1. 结构体 typedef struct hash_cell_struct hash_cell_t;

    typedef struct hash_cell_struct hash_cell_t; struct hash_cell_struct{ void* node; /*!< hash chain ...

  2. RazorEngine(未解决,留底)

    TemplateServiceConfiguration templateConfig = new TemplateServiceConfiguration { BaseTemplateType = ...

  3. bzoj2324

    出题人真 口袋迷 很容易发现这是一道费用流的题目 很显然这个问题有两个难点: 保证走到某个点时之前序号的点都被走过 保证每个点都走 对于1,我们换个说法,一个人走到该点时经过的点的序号都小于该点--- ...

  4. 使用Unity3d的Physics.Raycast()的用法做子弹射击

    Class Functions 1)static function Raycast (origin : Vector3, direction : Vector3, distance : float = ...

  5. C语言块内变量回收问题

    之前有一个错误认识,错误的认为局部变量的回收是发生在函数返回时.其实在块结束时块内使用的内容就会被回收了. 以下的实例说明了问题 ]; ; i < ; ++i) { int item = i; ...

  6. About Wisdom

    All human wisdom is summed up in two words --- wait and hope.人类所有的智慧可以归结为两个词---等待和希望. —— Alexandre D ...

  7. This Android SDK requires Android Developer Toolkit version 23.0.0 or above

    2014-07-05 12:58 6445人阅读 评论(1) 收藏 举报 This Android SDK requires Android Developer Toolkit version 23. ...

  8. e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (一) 京东 商品搜索

    之前有发布一篇文章“e2e 自动化集成测试 环境搭建 Node.js Selenium WebDriverIO Mocha Node-Inspector”, 主要是讲了,如何搭建环境, 其中开发环境使 ...

  9. codeforce 600A - Extract Numbers

    学习string #include <bits/stdc++.h> #define eps 1e-8 #define M_PI 3.141592653589793 ; using name ...

  10. Windows下ffmpeg的完美编译

    纠结了好几天,终于搞定了,小结一下. 1.下载ffmpeg源码,官网 2.编译环境Msys的安装配置,http://blog.csdn.net/jszj/article/details/4028716 ...