Java使用Protocol Buffers入門四步驟

來源:互聯網
上載者:User

Java使用Protocol Buffers入門四步驟

Protocol Buffers(簡稱protobuf)是Google的一項技術,用於將結構化的資料序列化、還原序列化,經常用於網路傳輸。

這貨實際上類似於XML產生和解析,但protobuf的效率高於XML,不過protobuf產生的是位元組碼,可讀性比XML差。類似的還有json、Java的Serializable等。

protobuf支援各種語言。本文以Java為例,簡單介紹protobuf如何使用。其他語言使用方法類似。

首先需要下載:

http://download.csdn.net/download/xiao__gui/7586617

解壓後有兩個檔案:protobuf-java-2.5.0.jar和protoc.exe。

protobuf-java-2.5.0.jar即protobuf所需要的jar包,如果用maven的話可以無視這個檔案;

protoc.exe是protobuf代碼產生工具。

第一步:定義資料結構

首先要定義protobuf的資料結構,這裡要寫一個.proto檔案。這個檔案有點類似於定義一個類。例如定義一個Person,儲存檔案PersonMsg.proto(注意檔案名稱和裡面的message名不要一樣)。

message Person {// ID(必需)required int32 id = 1;// 姓名(必需)required string name = 2;// email(可選)optional string email = 3;// 朋友(集合)repeated string friends = 4;}

上面的1、2、3、4是unique numbered tag,是一個唯一標識。

上面的例子中定義了一個非常簡單的資料結構,當然還可以定義更複雜的結構,這裡不再討論,具體可以看官方文檔。

第二步:protoc.exe產生Java代碼

使用檔案protoc.exe,cmd命令列運行:

protoc.exe --java_out=E:\java PersonMsg.proto

輸入檔案是PersonMsg.proto,也就是定義資料結構的檔案;輸出檔案夾是E:\java,將java檔案產生在E:\java中。運行命令成功後會產生PersonMsg.java:


在Eclipse中建立一個項目,將java檔案拷貝到項目中。項目中需要引入protobuf-java-2.5.0.jar包。如果是maven項目則加入:

<dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><version>2.5.0</version></dependency>

第三步:序列化
第四步:還原序列化

一般來說,序列化和還原序列化是分開的。例如網路傳輸,由一方將資料序列化後發送給另一方來接收並解析,序列化發送和接收還原序列化並不在一起。但是下面為了例子簡單將二者寫在同一程式中。

import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.List;public class Main {public static void main(String[] args) throws IOException {// 按照定義的資料結構,建立一個PersonPersonMsg.Person.Builder personBuilder = PersonMsg.Person.newBuilder();personBuilder.setId(1);personBuilder.setName("叉叉哥");personBuilder.setEmail("xxg@163.com");personBuilder.addFriends("Friend A");personBuilder.addFriends("Friend B");PersonMsg.Person xxg = personBuilder.build();// 將資料寫到輸出資料流,如網路輸出資料流,這裡就用ByteArrayOutputStream來代替ByteArrayOutputStream output = new ByteArrayOutputStream();xxg.writeTo(output);// -------------- 分割線:上面是發送方,將資料序列化後發送 ---------------byte[] byteArray = output.toByteArray();// -------------- 分割線:下面是接收方,將資料接收後還原序列化 ---------------// 接收到流並讀取,如網路輸入資料流,這裡用ByteArrayInputStream來代替ByteArrayInputStream input = new ByteArrayInputStream(byteArray);// 還原序列化PersonMsg.Person xxg2 = PersonMsg.Person.parseFrom(input);System.out.println("ID:" + xxg2.getId());System.out.println("name:" + xxg2.getName());System.out.println("email:" + xxg2.getEmail());System.out.println("friend:");List<String> friends = xxg2.getFriendsList();for(String friend : friends) {System.out.println(friend);}}}


作者:叉叉哥   轉載請註明出處:http://blog.csdn.net/xiao__gui/article/details/36643949



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.