Google protobuf
个人理解:
定义.proto文件就是指明消息里包含的成员和类型,protoc会compile成相应的java文件包含interface和implementation class,然后在构建message的时候要使用builder,然后写到outputstream里。
应用实例:
ByteArrayOutputStream out = new ByteArrayOutputStream(BYTE_ARRAY_SIZE);
CodedOutputStream codedOut = CodedOutputStream.newInstance(out);
GothamAuthProto.ExclusiveGroupChoices.newBuilder().setUsername(getActor().getUserId()).addChoices(GothamAuthProto.NameValuePair.newBuilder().setName(GROUP_KEY).setValue(role)).build().writeTo(codedOut);
codedOut.flush();
String uri = solaceRoleSetupUri + getActor().getUserId();
LOGGER.info("Select role: " + out.toString() + " -> " + uri);
batman.createProducerTemplate().sendBody(uri, out.toByteArray());
From Google.protobuf tutorial:https://developers.google.com/protocol-buffers/docs/javatutorial
1, With protocol buffers, you write a .proto description of the data structure you wish to store.
2, From that, the protocol buffer compiler creates a class that implements automatic encoding and parsing of the protocol buffer data with an efficient binary format.
3, The generated class provides getters and setters for the fields that make up a protocol buffer and takes care of the details of reading and writing the protocol buffer as a unit.
a. START with a .proto file:
=====================================
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos"; 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 AddressBook {
repeated Person person = 1;
} ==================================================== b. Run the protocol buffer compilerprotocon your.proto:(generate the classes you'll need to read and writeAddressBook)
Here are some of the accessors for thePersonclass:
// required string name = 1;
public boolean hasName();
public String getName(); // required int32 id = 2;
public boolean hasId();
public int getId(); // optional string email = 3;
public boolean hasEmail();
public String getEmail();
c, To construct a message, you must first construct a builder, set any fields you want to set to your chosen values, then call the builder's build() method.
Person john =
Person.newBuilder()
.setId(1234)
.setName("John Doe")
.setEmail("jdoe@example.com")
.addPhone(
Person.PhoneNumber.newBuilder()
.setNumber("555-4321")
.setType(Person.PhoneType.HOME))
.build();
Google protobuf的更多相关文章
- google protobuf安装与使用
google protobuf是一个灵活的.高效的用于序列化数据的协议.相比较XML和JSON格式,protobuf更小.更快.更便捷.google protobuf是跨语言的,并且自带了一个编译器( ...
- VS2013编译google protobuf 出现问题error C3861: “min”:
问题描述: 今天用vs2013编译protobuf 2.4.1 报错: 错误 3 error C3861: "max": 找不到标识符 f:\google\protobuf\pro ...
- google protobuf初体验
最近在读别人代码的时候发现一个的东西,名字叫protobuf, 感觉挺好用的,写在这里,留个记录.那么什么是protobuf 呢?假如您在网上搜索,应该会得到类似这样的文字介绍: Google Pro ...
- Google protobuf proto文件编写规则
转载自: http://blog.csdn.net/yi_ya/article/details/40404231 1. 简单介绍 protobuf文件:就是定义你要的消息(类似java中的类)和消息中 ...
- window下编译并使用google protobuf
参考网址: http://my.oschina.net/chenleijava/blog/261263 http://www.ibm.com/developerworks/cn/linux/l-cn- ...
- GOOGLE PROTOBUF开发者指南
原文地址:http://www.cppblog.com/liquidx/archive/2009/06/23/88366.html 译者: gashero 目录 1 概览 1.1 什么是pro ...
- google protobuf ios开发使用
简介: protobuf 即 google protocol buffer 是一种数据封装格式协议: 比如其他经常用的xml,json等格式:protobuf的优势是效率高,同样的一份数据使用prot ...
- google protobuf 简单实例
1.定义proto文件: User.proto package netty; option java_package="myprotobuf"; option java_outer ...
- google protobuf使用
下载的是github上的:https://github.com/google/protobuf If you get the source from github, you need to gener ...
随机推荐
- 《Java程序设计》实验五 实验报告
实验五 java网络编程 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2. ...
- python读取指定内存的内容
import ctypes as ct t = ct.string_at(0x211000, 20) # (addr, size) print t 最好不要用解释性语言来开发底层,一般用C.
- uva--1339 - Ancient Cipher(模拟水体系列)
1339 - Ancient Cipher Ancient Roman empire had a strong government system with various departments, ...
- hdu---(1280)前m大的数(计数排序)
前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 《Play for Java》学习笔记(六)文件上传file upload
一. Play中标准方法 使用表单form和multipart/form-data的content-type类型. 1.Form @form(action = routes.Application.u ...
- [转]Web基础架构:负载均衡和LVS
以下内容转载自:http://www.importnew.com/11229.html 在大规模互联网应用中,负载均衡设备是必不可少的一个节点,源于互联网应用的高并发和大流量的冲击压力,我们通常会在服 ...
- apache2.4域名配置参数
apache2.4和 2.2版本的配置有区别 2.4的配置如下 <VirtualHost *:80>DocumentRoot /www/web/projectServerName www. ...
- 一步一步配置NLB(续)之深入测试
在这里http://blog.csdn.net/haoxiaozigang1/article/details/12198679跟大家分享了NLB配置的过程,下面写一些对NLB不同情况的下测试的结果: ...
- PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )
/** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...
- 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。
package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...