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 compilerprotoc
on your.proto
:(generate the classes you'll need to read and writeAddressBook
)
Here are some of the accessors for thePerson
class:
// 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程序设计》第二周学习总结
20145224陈颢文<Java程序设计>第二周学习总结 教材学习内容总结 一.类型.变量与运算符 1.类型 整数: 可细分为为short整数(占2字节),int整数(占4字节),long ...
- Compound Interest Calculator2.0
Compound Interest Calculator2.0 1.如果按照单利计算,本息又是多少呢? 2.假如30年之后要筹措到300万元的养老金,平均的年回报率是3%,那么,现在必须投入的本金是多 ...
- 我的Github注册使用之旅
[个人介绍] 我是来自网络工程143班的姜金金,学号是1413042066.我没什么大的爱好,闲时喜欢在有阳光的午后喝喝小茶,捧一本书慢慢品茗:也喜欢散散步,欣赏细碎事物的美好,驻足沿路美丽的风景.说 ...
- C#WPF做FTP上传下载获取文件列表
Xaml.cs: using Microsoft.Win32;using System;using System.Collections.Generic;using System.IO;using S ...
- 在单元测试中指定log4j的配置文件
在开发过程中,我们会使用到log4j来输出日志,我们希望在单元测试的时候,只看到部分日志信息,或者定义日志输出的级别. 这个时候手工指定log4j的配置文件: 具体做法如下: 定义类如下: i ...
- Jsp页面中使用fckeditor控件的两种方法 [转]
fckeditor控件请到官方网站下载http://www.fckeditor.net,本例主要用到FCKeditor_2.6.3.zip.fckeditor-java-demo-2.4.1.zip. ...
- iOS --- 通过openURL实现APP之间跳转并传递数据
在IOS中,实现一个应用启动另外一个应用,使用UIApplication的openURL:方法就可实现,这里以test跳到test02为例.(需要先创建这两个工程) 注册自定义URL协议(在test中 ...
- 嵌套 click 第二层 click会叠加 导致 触发 多次
$("#appearHiddenDiv").click(function(){ $("#hiddenDiv").css({display:"block ...
- TableLayout练习
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android=" ...
- POJ 1083 Moving Tables 思路 难度:0
http://poj.org/problem?id=1083 这道题题意是有若干段线段,每次要求线段不重叠地取,问最少取多少次. 因为这些线段都是必须取的,所以需要让空隙最小 思路: 循环直到线段全部 ...