Protobuffer和json深度對比

JSON相信你們都知道是什麼東西,若是不知道,那可就真的OUT了,GOOGLE一下去。這裏就不介紹啥的了。javascript

Protobuffer你們估計就不多據說了,但若是說到是GOOGLE搞的,相信你們都會有興趣去試一下,畢竟GOOGLE出口,多屬精品。java

Protobuffer是一個相似JSON的一個傳輸協議,其實也不能說是協議,只是一個數據傳輸的東西罷了。python

那它跟JSON有什麼區別呢?json

跨語言,這是它的一個優勢。它自帶了一個編譯器,protoc,只須要用它進行編譯,能夠編譯成JAVA、python、C++代碼,暫時只有這三個,其餘就暫時不要想了,而後就能夠直接使用,不須要再寫任何其餘代碼。連解析的那些都已經自帶有的。JSON固然也是跨語言的,但這個跨語言是創建在編寫代碼的基礎上。windows

若是想再深刻了解的,能夠去看看:服務器

https://developers.google.com/protocol-buffers/docs/overviewapp

好了,廢話很少說,咱們直接來看看,爲何咱們須要對比protobuffer(下面簡稱GPB)和JSON。maven

一、JSON由於有必定的格式,而且是以字符存在的,在數據量上還有能夠壓縮的空間。而GPB上大數據量時,空間比JSON小不少,等一下的例子咱們能夠看到。測試

二、JSON各個庫之間的效率相差比較大,jackson庫和GSON就大概有5-10的差距(這個只作過一次測試,若有誤,請你們輕拍)。而GPB只須要一個,沒有所謂的多個庫的區別。固然這個點只是弄出來湊數的,能夠忽略不計哈。大數據

 

Talk is cheap,Just show me the code。

在程序界,代碼永遠是王道,下面就直接來代碼吧。

上代碼前,你們要先下載protobuffer,在這裏:

https://code.google.com/p/protobuf/downloads/list

注意,須要下載兩個,一個是complier,另一個是source code,相信這個難不倒你們了,這裏略過。

一、首先,GPB是須要有一個相似類定義的文件,叫proto文件 。

咱們以學生和老師的例子來進行一個例子:

咱們有如下兩個文件:student.proto

 

Java代碼

 收藏代碼

  1. <span style="font-size: 16px;">option java_package = "com.shun";  
  2. option java_outer_classname = "StudentProto";  
  3.   
  4. message Student {  
  5.     required int32 id = 1;  
  6.     optional string name = 2;  
  7.     optional int32 age = 3;  
  8. }</span>  

 teacher.proto

 

Java代碼

 收藏代碼

  1. <span style="font-size: 16px;">import "student.proto";  
  2. option java_package = "com.shun";  
  3. option java_outer_classname = "TeacherProto";  
  4.   
  5. message Teacher {  
  6.     required int32 id = 1;  
  7.     optional string name = 2;  
  8.   
  9.     repeated Student student_list = 3;  
  10. }</span>  

這裏咱們遇到了一些比較奇怪的東西:

import,int32,repated,required,optional,option等

一個個來吧:

1)import表示引入其餘的proto文件

2)required,optional表示字段是否可選,這個決定了該字段有無值的狀況下protobuffer會進行什麼處理。若是標誌了required,但當處理時,該字段沒有進行傳值,則會報錯;若是標誌了optional,不傳值則不會有什麼問題。

3)repeated相信應該都看得懂了,就是是否重複,跟JAVA裏面的list相似

4)message就是至關於class了

5)option表示選項,其中的java_package表示包名,即生成JAVA代碼時使用的包名,java_outer_classname即爲類名,注意這個類名不能跟下面的message中的類名相同。

至於還有其餘的選項和相關類型的,請參觀官方文檔。

 

二、有了這幾個文件,咱們能怎麼樣呢?

記得上面下載的編譯器了吧,解壓出來,咱們獲得一個protoc.exe,這固然是windows下的,我沒弄其餘系統的,有興趣的同窗去折騰下羅。

加到path(加不加能夠隨便,只是方不方便而已),而後就能夠經過上面的文件生成咱們須要的類文件了。

protoc --java_out=存放源代碼的路徑 --proto_path=proto文件的路徑 proto具體文件

--proto_path指定的是proto文件的文件夾路徑,並非單個文件,主要是爲了import文件查找使用的,能夠省略

 

如我須要把源代碼放在D:\protobufferVsJson\src,而個人proto文件存放在D:\protoFiles

那麼個人編譯命令就是:

protoc --java_out=D:\protobufferVsJson\src 

D:\protoFiles\teacher.proto D:\protoFiles\student.proto

注意,這裏最後的文件,咱們須要指定須要編譯的全部文件

 

編譯後能夠看到生成的文件。

代碼就不貼出來了,太多了。你們能夠私下看看,代碼裏面有一大堆Builder,相信一看就知道是建造者模式了。

這時能夠把代碼貼到你的項目中了,固然,錯誤一堆了。

 

記得咱們前面下載的源代碼嗎?解壓它吧,不要手軟。而後找到src/main/java/複製其中的一堆到你的項目,固然,你也能夠ant或者maven編譯,但這兩個東西我都不熟,就不獻醜了,我仍是習慣直接複製到項目中。


代碼出錯,哈哈,正常。不知道爲什麼,GOOGLE非要留下這麼個坑給咱們。

翻回到protobuffer目錄下的\java看到有個readme.txt了吧,找到一句:

看來看去,感受這個代碼會有點奇怪的,好像錯錯的感受,反正我是沒按那個執行,個人命令是:

 

Java代碼

 收藏代碼

  1. <span style="font-size: 16px;">protoc --java_out=仍是上面的放代碼的地方 proto文件的路徑(這裏是descriptor.proto文件的路徑)</span>  

執行後,咱們能夠看到代碼中的錯誤木有了。

 

三、接下來固然就是測試了。

咱們先進行GPB寫入測試:

Java代碼

 收藏代碼

  1. <span style="font-size: 16px;">package com.shun.test;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import com.shun.StudentProto.Student;  
  9. import com.shun.TeacherProto.Teacher;  
  10.   
  11. public class ProtoWriteTest {  
  12.   
  13.     public static void main(String[] args) throws IOException {  
  14.           
  15.         Student.Builder stuBuilder = Student.newBuilder();  
  16.         stuBuilder.setAge(25);  
  17.         stuBuilder.setId(11);  
  18.         stuBuilder.setName("shun");  
  19.           
  20.         //構造List  
  21.         List<Student> stuBuilderList = new ArrayList<Student>();  
  22.         stuBuilderList.add(stuBuilder.build());  
  23.           
  24.         Teacher.Builder teaBuilder = Teacher.newBuilder();  
  25.         teaBuilder.setId(1);  
  26.         teaBuilder.setName("testTea");  
  27.         teaBuilder.addAllStudentList(stuBuilderList);  
  28.           
  29.         //把gpb寫入到文件  
  30.         FileOutputStream fos = new FileOutputStream("C:\\Users\\shun\\Desktop\\test\\test.protoout");  
  31.         teaBuilder.build().writeTo(fos);  
  32.         fos.close();  
  33.     }  
  34.   
  35. }</span>  

咱們去看看文件,如無心外,應該是生成了的。

生成了以後,咱們確定要讀回它的。

Java代碼

 收藏代碼

  1. <span style="font-size: 16px;">package com.shun.test;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6.   
  7. import com.shun.StudentProto.Student;  
  8. import com.shun.TeacherProto.Teacher;  
  9.   
  10. public class ProtoReadTest {  
  11.   
  12.     public static void main(String[] args) throws FileNotFoundException, IOException {  
  13.           
  14.         Teacher teacher = Teacher.parseFrom(new FileInputStream("C:\\Users\\shun\\Desktop\\test\\test.protoout"));  
  15.         System.out.println("Teacher ID:" + teacher.getId() + ",Name:" + teacher.getName());  
  16.         for (Student stu:teacher.getStudentListList()) {  
  17.             System.out.println("Student ID:" + stu.getId() + ",Name:" + stu.getName() + ",Age:" + stu.getAge());  
  18.         }  
  19.     }  
  20.   
  21. }</span>  

代碼很簡單,由於GPB生成的代碼都幫咱們完成了。

上面知道基本的用法了,咱們重點來關注GPB跟JSON生成文件大小的區別,JSON的詳細代碼我這裏就不貼了,以後會貼出示例,你們有興趣能夠下載。

這裏咱們用Gson來解析JSON,下面只給出對象轉換成JSON後寫出文件的代碼:

兩個類Student和Teacher的基本定義就不弄了,你們隨意就行,代碼以下:

 

Java代碼

 收藏代碼

  1. <span style="font-size: 16px;">package com.shun.test;  
  2.   
  3. import java.io.FileWriter;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import com.google.gson.Gson;  
  9. import com.shun.Student;  
  10. import com.shun.Teacher;  
  11.   
  12. public class GsonWriteTest {  
  13.   
  14.     public static void main(String[] args) throws IOException {  
  15.         Student stu = new Student();  
  16.         stu.setAge(25);  
  17.         stu.setId(22);  
  18.         stu.setName("shun");  
  19.           
  20.         List<Student> stuList = new ArrayList<Student>();  
  21.         stuList.add(stu);  
  22.           
  23.         Teacher teacher = new Teacher();  
  24.         teacher.setId(22);  
  25.         teacher.setName("shun");  
  26.         teacher.setStuList(stuList);  
  27.           
  28.         String result = new Gson().toJson(teacher);  
  29.         FileWriter fw = new FileWriter("C:\\Users\\shun\\Desktop\\test\\json");  
  30.         fw.write(result);  
  31.         fw.close();  
  32.     }  
  33.   
  34. }</span>  

接下來正式進入咱們的真正測試代碼了,前面咱們只是在列表中放入一個對象,接下來,咱們依次測試100,1000,10000,100000,1000000,5000000這幾個數量的GPB和JSON生成的文件大小。

改進一下以前的GPB代碼,讓它生成不一樣數量的列表,再生成文件:

 

Java代碼

 收藏代碼

  1. <span style="font-size: 16px;">package com.shun.test;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import com.shun.StudentProto.Student;  
  9. import com.shun.TeacherProto.Teacher;  
  10.   
  11. public class ProtoWriteTest {  
  12.   
  13.     public static final int SIZE = 100;  
  14.       
  15.     public static void main(String[] args) throws IOException {  
  16.           
  17.         //構造List  
  18.         List<Student> stuBuilderList = new ArrayList<Student>();  
  19.         for (int i = 0; i < SIZE; i ++) {  
  20.             Student.Builder stuBuilder = Student.newBuilder();  
  21.             stuBuilder.setAge(25);  
  22.             stuBuilder.setId(11);  
  23.             stuBuilder.setName("shun");  
  24.               
  25.             stuBuilderList.add(stuBuilder.build());  
  26.         }  
  27.           
  28.         Teacher.Builder teaBuilder = Teacher.newBuilder();  
  29.         teaBuilder.setId(1);  
  30.         teaBuilder.setName("testTea");  
  31.         teaBuilder.addAllStudentList(stuBuilderList);  
  32.           
  33.         //把gpb寫入到文件  
  34.         FileOutputStream fos = new FileOutputStream("C:\\Users\\shun\\Desktop\\test\\proto-" + SIZE);  
  35.         teaBuilder.build().writeTo(fos);  
  36.         fos.close();  
  37.     }  
  38.   
  39. }</span>  

 這裏的SIZE依次改爲咱們上面聽說的測試數,能夠獲得以下:

 


 而後咱們再看看JSON的測試代碼:

 

 

Java代碼

 收藏代碼

  1. <span style="font-size: 16px;">package com.shun.test;  
  2.   
  3. import java.io.FileWriter;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7.   
  8. import com.google.gson.Gson;  
  9. import com.shun.Student;  
  10. import com.shun.Teacher;  
  11.   
  12. public class GsonWriteTest {  
  13.   
  14.     public static final int SIZE = 100;  
  15.       
  16.     public static void main(String[] args) throws IOException {  
  17.           
  18.         List<Student> stuList = new ArrayList<Student>();  
  19.         for (int i = 0; i < SIZE; i ++) {  
  20.             Student stu = new Student();  
  21.             stu.setAge(25);  
  22.             stu.setId(22);  
  23.             stu.setName("shun");  
  24.               
  25.             stuList.add(stu);  
  26.         }  
  27.           
  28.           
  29.         Teacher teacher = new Teacher();  
  30.         teacher.setId(22);  
  31.         teacher.setName("shun");  
  32.         teacher.setStuList(stuList);  
  33.           
  34.         String result = new Gson().toJson(teacher);  
  35.         FileWriter fw = new FileWriter("C:\\Users\\shun\\Desktop\\test\\json" + SIZE);  
  36.         fw.write(result);  
  37.         fw.close();  
  38.     }  
  39.   
  40. }</span>  

 一樣的方法修改SIZE,並做相應的測試。

能夠明顯得看到json的文件大小跟GPB的文件大小在數據量慢慢大上去的時候就會有比較大的差異了,JSON明顯要大上許多。


上面的表應該能夠看得比較清楚了,在大數據的GPB是很是佔優點的,但通常狀況下客戶端和服務端並不會直接進行這麼大數據的交互,大數據主要發生在服務器端的傳輸上,若是你面對需求是天天須要把幾百M的日誌文件傳到另一臺服務器,那麼這裏GPB可能就能幫你的大忙了。

 

 

說是深度對比,其實主要對比的是大小方面,時間方面可比性不會太大,也沒相差太大。

文章中選擇的Gson解析器,有興趣的朋友能夠選擇Jackson或者fastjson,又或者其餘的,但生成的文件大小是同樣的,只是解析時間有區別。

相關文章
相關標籤/搜索