http://blog.csdn.net/xyz317100759/article/details/6261236

2011-03-19 15:44 1185人阅读 评论(5) 收藏 举报

由于项目的关系,前不久学习了Protocol Buffer。刚开始的时候,有些迷茫,网上的资料大多是介绍C++语言环境下的,或者是不太完整的java环境的介绍。后来只好去官网看官方的文档,通过文档一步一步学习,终于有了一些进展。为使初学者少走弯路,特此分享自己的经历。

Ps:为准确理解资料的含义,建议阅读官方文档,在此也给个地址http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/overview.html,好了,进入主题吧!


protobuf介绍


按照官网的描述:protobuf是google提供的一个开源序列化框架。主要应用于通信协议,数据存储中的结构化数据的序列化。它类似于XML,JSON这样的数据表示语言,其最大的特点是基于二进制,因此比传统的XML表示高效短小得多。虽然是二进制数据格式,但并没有因此变得复杂,开发人员通过按照一定的语法定义结构化的消息格式,然后送给命令行工具,工具将自动生成相关的类,可以支持java、c++、python等语言环境。通过将这些类包含在项目中,可以很轻松的调用相关方法来完成业务消息的序列化与反序列化工作。



protobuf的优势


1、语言中立

2、平台中立

3、高效性


protobuf入门(eclipse下java环境的搭建)


更多案例请查阅源代码包protobuf-2.3.0.zip 里面有关于各种支持语言(java,C++,python等)的案例。

1、下载jar包 protobuf-java-2.3.0.jar

2、下载编译器protoc.exe

3、新建java工程test_protobuf

4、导入protobuf-java-2.3.0.jar包

5、导入编译器protoc.exe到项目下

6、在项目下建存放文件.proto的文件夹proto

7、编写message并放在proto文件夹下,有关编写规范和说明请参考官网文档,在这里我引用官网的例子,创建addressbook.proto代码如下:

[java] view plaincopyprint?
  1. package tutorial;
  2. option java_package = "com.example.tutorial";
  3. option java_outer_classname = "AddressBookProtos";
  4. message Person {
  5. required string name = 1;
  6. required int32 id = 2;        // Unique ID number for this person.
  7. optional string email = 3;
  8. enum PhoneType {
  9. MOBILE = 0;
  10. HOME = 1;
  11. WORK = 2;
  12. }
  13. message PhoneNumber {
  14. required string number = 1;
  15. optional PhoneType type = 2 [default = HOME];
  16. }
  17. repeated PhoneNumber phone = 4;
  18. }
  19. // Our address book file is just one of these.
  20. message AddressBook {
  21. repeated Person person = 1;
  22. }

package tutorial; option java_package = "com.example.tutorial"; option java_outer_classname = "AddressBookProtos"; message Person { required string name = 1; required int32 id = 2; // Unique ID number for this person. 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; } // Our address book file is just one of these. message AddressBook { repeated Person person = 1; }

8、编译addressbook.proto成指定的java类

命令行下进入编译器所在目录,执行如下命令

protoc -I=proto/ --java_out=src proto/addressbook.proto

其中,src为生成的java类的目标位置,这里我们选择项目的默认包,proto/addressbook.proto表示我们的proto文件,运行后即生成java类,生成的java类被放在了package com.example.tutorial中。刚才我们指定的目标位置是src,为什么现在却被放在了这个包中呢?这和我们的addressbook.proto文件中的option java_package = "com.example.tutorial";有关。欲了解更多,参考上节提到的官方文档。

9、现在有了生成的AddressBookProtos.java类,我们可以向文件里写入消息了,首先编写AddPerson.java,代码如下:

[java] view plaincopyprint?
  1. import com.example.tutorial.AddressBookProtos.AddressBook;
  2. import com.example.tutorial.AddressBookProtos.Person;
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.IOException;
  9. import java.io.PrintStream;
  10. class AddPerson{
  11. // This function fills in a Person message based on user input.
  12. static Person PromptForAddress(BufferedReader stdin,PrintStream stdout)throws IOException{
  13. Person.Builder person = Person.newBuilder();
  14. stdout.print("Enter person ID: ");
  15. person.setId(Integer.valueOf(stdin.readLine()));
  16. stdout.print("Enter name: ");
  17. person.setName(stdin.readLine());
  18. stdout.print("Enter email address (blank for none): ");
  19. String email = stdin.readLine();
  20. if (email.length() > 0){
  21. person.setEmail(email);
  22. }
  23. while (true){
  24. stdout.print("Enter a phone number (or leave blank to finish): ");
  25. String number = stdin.readLine();
  26. if (number.length() == 0){
  27. break;
  28. }
  29. Person.PhoneNumber.Builder phoneNumber = Person.PhoneNumber.newBuilder().setNumber(number);
  30. stdout.print("Is this a mobile, home, or work phone? ");
  31. String type = stdin.readLine();
  32. if (type.equals("mobile")){
  33. phoneNumber.setType(Person.PhoneType.MOBILE);
  34. } else if (type.equals("home")) {
  35. phoneNumber.setType(Person.PhoneType.HOME);
  36. } else if (type.equals("work")) {
  37. phoneNumber.setType(Person.PhoneType.WORK);
  38. } else {
  39. stdout.println("Unknown phone type.  Using default.");
  40. }
  41. person.addPhone(phoneNumber);
  42. }
  43. return person.build();
  44. }
  45. // Main function: Reads the entire address book from a file,  adds one person based on user input,
  46. //then writes it back out to the same file.
  47. public static void main(String[] args) throws Exception{
  48. if (args.length != 1) {
  49. System.err.println("Usage:  AddPerson ADDRESS_BOOK_FILE");
  50. System.exit(-1);
  51. }
  52. AddressBook.Builder addressBook = AddressBook.newBuilder();
  53. // Read the existing address book.
  54. try {
  55. addressBook.mergeFrom(new FileInputStream(args[0]));
  56. } catch (FileNotFoundException e) {
  57. System.out.println(args[0] + ": File not found.  Creating a new file.");
  58. }
  59. // Add an address.
  60. addressBook.addPerson(
  61. PromptForAddress(new BufferedReader(new InputStreamReader(System.in)), System.out));
  62. // Write the new address book back to disk.
  63. FileOutputStream output = new FileOutputStream(args[0]);
  64. addressBook.build().writeTo(output);
  65. output.close();
  66. }
  67. }

import com.example.tutorial.AddressBookProtos.AddressBook; import com.example.tutorial.AddressBookProtos.Person; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.PrintStream; class AddPerson{ // This function fills in a Person message based on user input. static Person PromptForAddress(BufferedReader stdin,PrintStream stdout)throws IOException{ Person.Builder person = Person.newBuilder(); stdout.print("Enter person ID: "); person.setId(Integer.valueOf(stdin.readLine())); stdout.print("Enter name: "); person.setName(stdin.readLine()); stdout.print("Enter email address (blank for none): "); String email = stdin.readLine(); if (email.length() > 0){ person.setEmail(email); } while (true){ stdout.print("Enter a phone number (or leave blank to finish): "); String number = stdin.readLine(); if (number.length() == 0){ break; } Person.PhoneNumber.Builder phoneNumber = Person.PhoneNumber.newBuilder().setNumber(number); stdout.print("Is this a mobile, home, or work phone? "); String type = stdin.readLine(); if (type.equals("mobile")){ phoneNumber.setType(Person.PhoneType.MOBILE); } else if (type.equals("home")) { phoneNumber.setType(Person.PhoneType.HOME); } else if (type.equals("work")) { phoneNumber.setType(Person.PhoneType.WORK); } else { stdout.println("Unknown phone type. Using default."); } person.addPhone(phoneNumber); } return person.build(); } // Main function: Reads the entire address book from a file, adds one person based on user input, //then writes it back out to the same file. public static void main(String[] args) throws Exception{ if (args.length != 1) { System.err.println("Usage: AddPerson ADDRESS_BOOK_FILE"); System.exit(-1); } AddressBook.Builder addressBook = AddressBook.newBuilder(); // Read the existing address book. try { addressBook.mergeFrom(new FileInputStream(args[0])); } catch (FileNotFoundException e) { System.out.println(args[0] + ": File not found. Creating a new file."); } // Add an address. addressBook.addPerson( PromptForAddress(new BufferedReader(new InputStreamReader(System.in)), System.out)); // Write the new address book back to disk. FileOutputStream output = new FileOutputStream(args[0]); addressBook.build().writeTo(output); output.close(); } }

首先配置参数,也就是消息被序列化后存储的文件名,这里,我们就把参数设置成AddressBook.代码中提到,运行时如果文件不存在,将会创建文件并写入;如果存在,就写入。运行程序,按照提示输入消息。然后查看我们的项目路径下,将会产生AddressBook文件

10、上一步是将消息序列化到文件中,这一步将文件中的消息反序列化,类似地,我们创建一个类:ListPeople.java 代码如下:

[java] view plaincopyprint?
  1. import com.example.tutorial.AddressBookProtos.AddressBook;
  2. import com.example.tutorial.AddressBookProtos.Person;
  3. import java.io.FileInputStream;
  4. public class ListPeople {
  5. // Iterates though all people in the AddressBook and prints info about them.
  6. static void Print(AddressBook addressBook) {
  7. for (Person person: addressBook.getPersonList()) {
  8. System.out.println("Person ID: " + person.getId());
  9. System.out.println("  Name: " + person.getName());
  10. if (person.hasEmail()) {
  11. System.out.println("  E-mail address: " + person.getEmail());
  12. }
  13. for (Person.PhoneNumber phoneNumber : person.getPhoneList()) {
  14. switch (phoneNumber.getType()) {
  15. case MOBILE:
  16. System.out.print("  Mobile phone #: ");
  17. break;
  18. case HOME:
  19. System.out.print("  Home phone #: ");
  20. break;
  21. case WORK:
  22. System.out.print("  Work phone #: ");
  23. break;
  24. }
  25. System.out.println(phoneNumber.getNumber());
  26. }
  27. }
  28. }
  29. // Main function:  Reads the entire address book from a file and prints all  the information inside.
  30. /**
  31. * @param args
  32. * @throws Exception
  33. */
  34. public static void main(String[] args) throws Exception {
  35. if (args.length != 1) {
  36. System.err.println("Usage:  ListPeople ADDRESS_BOOK_FILE");
  37. System.exit(-1);
  38. }
  39. // Read the existing address book.
  40. AddressBook addressBook = AddressBook.parseFrom(new FileInputStream(args[0]));
  41. Print(addressBook);
  42. }
  43. }

import com.example.tutorial.AddressBookProtos.AddressBook; import com.example.tutorial.AddressBookProtos.Person; import java.io.FileInputStream; public class ListPeople { // Iterates though all people in the AddressBook and prints info about them. static void Print(AddressBook addressBook) { for (Person person: addressBook.getPersonList()) { System.out.println("Person ID: " + person.getId()); System.out.println(" Name: " + person.getName()); if (person.hasEmail()) { System.out.println(" E-mail address: " + person.getEmail()); } for (Person.PhoneNumber phoneNumber : person.getPhoneList()) { switch (phoneNumber.getType()) { case MOBILE: System.out.print(" Mobile phone #: "); break; case HOME: System.out.print(" Home phone #: "); break; case WORK: System.out.print(" Work phone #: "); break; } System.out.println(phoneNumber.getNumber()); } } } // Main function: Reads the entire address book from a file and prints all the information inside. /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: ListPeople ADDRESS_BOOK_FILE"); System.exit(-1); } // Read the existing address book. AddressBook addressBook = AddressBook.parseFrom(new FileInputStream(args[0])); Print(addressBook); } }

运行程序,将会看到我们输入的消息体被遍历并打印出来了!

通过一个简单的例子,希望能够对大家有所帮助!!

Protocol Buffer入门——轻松搭建java环境 .相关推荐

  1. linux 搭建开发stm32 stlink,ROS2与STM32入门教程-搭建开发环境(ubuntu+eclipse+cubemx+stlink+openocd)...

    ROS2与C++入门教程-搭建开发环境(ubuntu+eclipse+cubemx+stlink+opencd) 说明: 介绍如何在ubuntu下搭建开发环境 环境:ubuntu20.04 + ecl ...

  2. Ubuntu 18.04.1 搭建Java环境和HelloWorld

    一.搭建Java环境 系统环境 Ubuntu 18.04.1 JDK 8 IDEA 2018.2 1.下载JDK 官网地址:http://www.oracle.com/technet... 选择相应的 ...

  3. Ubuntu 18.04.1 搭建Java环境和HelloWorld 1

    一.搭建Java环境 系统环境 Ubuntu 18.04.1 JDK 8 IDEA 2018.2 1.下载JDK 官网地址:http://www.oracle.com/technetwork/java ...

  4. xp/win 7 系统搭建 Java环境

    win 7 系统搭建 Java环境  xp系统大同小异 下面是具体的值

  5. Linux下搭建Java环境

    date: 2018/12/7 16:19:35 Linux搭建Java环境 1 查看当前系统版本 cat /proc/version 2 查看java版本 java -version 3 查看当前系 ...

  6. win java_Win搭建JAVA环境

    导读热词 一:下载JDK 选择你的系统环境进行下载 二:安装JDK 直接运行exe可执行程序选择下一步,默认安装即可: 备注:路径可以选其他盘符,不建议路径包含中文名及特殊符号. 三:配置环境变量 打 ...

  7. cmd窗口太炫酷了,电脑编码软件太多?手把手教你搭建Java环境,利用dos命令实现运行操作

    Java学习打卡:第十四天 内容导航 Java学习打卡:第十四天 内容管理 什么是cmd 写文背景介绍 搭建java环境 首先先下载JDK,java开发工具包 第二步:将压缩包解压到指定目录 第三步: ...

  8. 服务器上搭建java环境,安装tomcat以及MySQL数据库-小白教程

    文章目录 前言 一.安装jdk,搭建java环境 二.安装MySQL 三.安装Tomcat 总结 前言 我第一次租服务器的时候也是从网上找方法,各种教程琳琅满目,太杂乱,我在这里做一个总结.我的系统是 ...

  9. cytoscape安装java_搭建java环境——安装GSEA和Cytoscape

    GSEA和Cytoscape的安装都需要java环境,所以首先要搭建好java环境.在搭建java环境之前新建一个虚拟环境,将java环境与外部环境隔绝起来.我使用的是conda建立的虚拟环境:con ...

最新文章

  1. ueditor上传图片回调_(常见解决方法)UEditor报错“后端配置项没有正常加载,上传插件不能正常使用”...
  2. capistranorb
  3. SQL 2005新增的几个函数之学习
  4. Metal之简单渲染动态切换屏幕颜色
  5. try....exception....finally
  6. 用React开发SAP Fiori应用
  7. 子列和列_最大子列和 - fanlinglong - 博客园
  8. Flex3与BlazeDS HelloWorld 详解
  9. chrome浏览器 提示Adobe Flash Player未安装的解决方法
  10. (翻译)开始iOS 7中自动布局教程(二)
  11. vba excel 取得chart保存图片_保存Excel中的图片
  12. Java 详解(JVM) 垃圾回收机制原理
  13. ICLR 2022 | 合作博弈新范式:为可解释性等机器学习估值问题提供新方法
  14. spring实战笔记
  15. UVa 10036 - Divisibility
  16. RCNN,Fast RCNN, Faster RCN解析
  17. 电子商务竞争加速:顺丰速运及天极均推B2C平台
  18. Elasticsearch 入门
  19. 基于Gevent的firefly重要迭代版本推出
  20. 一个关于laragon环境下多个laravel项目部署问题

热门文章

  1. 81. 搜索旋转排序数组 II
  2. 79. Word Search 单词搜索
  3. DSP入门:中断PIE
  4. wireshark抓包数据:理解与分析
  5. 【Qt】Qt资源应用--菜单图标
  6. 【Linux】一步一步学Linux——groups命令(93)
  7. [Qt教程] 第47篇 进阶(七) 定制Qt帮助系统
  8. html实现div打印,如何在html div的中间打印/附加从按钮单击的值?
  9. r语言员工离职_HR,你真的会做员工面谈吗?
  10. LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))