通過Java來測試JSON和Protocol Buffer的傳輸文件大小
JSON相信大家都知道是什么東西,如果不知道,那可就真的OUT了,GOOGLE一下去。這里就不介紹啥的了。
Protobuffer大家估計就很少聽說了,但如果說到是GOOGLE搞的,相信大家都會有興趣去試一下,畢竟GOOGLE出口,多屬精品。
Protobuffer是一個類似JSON的一個傳輸協(xié)議,其實也不能說是協(xié)議,只是一個數(shù)據(jù)傳輸?shù)臇|西罷了。
那它跟JSON有什么區(qū)別呢?
跨語言,這是它的一個優(yōu)點。它自帶了一個編譯器,protoc,只需要用它進行編譯,可以編譯成JAVA、python、C++代碼,暫時只有這三個,其他就暫時不要想了,然后就可以直接使用,不需要再寫任何其他代碼。連解析的那些都已經(jīng)自帶有的。JSON當(dāng)然也是跨語言的,但這個跨語言是建立在編寫代碼的基礎(chǔ)上。
如果想再深入了解的,可以去看看:
https://developers.google.com/protocol-buffers/docs/overview
好了,廢話不多說,我們直接來看看,為什么我們需要對比protobuffer(下面簡稱GPB)和JSON。
1、JSON因為有一定的格式,并且是以字符存在的,在數(shù)據(jù)量上還有可以壓縮的空間。而GPB上大數(shù)據(jù)量時,空間比JSON小很多,等一下的例子我們可以看到。
2、JSON各個庫之間的效率相差比較大,jackson庫和GSON就大概有5-10的差距(這個只做過一次測試,如有誤,請大家輕拍)。而GPB只需要一個,沒有所謂的多個庫的區(qū)別。當(dāng)然這個點只是弄出來湊數(shù)的,可以忽略不計哈。
Talk is cheap,Just show me the code。
在程序界,代碼永遠是王道,下面就直接來代碼吧。
上代碼前,大家要先下載protobuffer,在這里:
https://github.com/google/protobuf
1、首先,GPB是需要有一個類似類定義的文件,叫proto文件 。
我們以學(xué)生和老師的例子來進行一個例子:
我們有以下兩個文件:student.proto
option java_package = "com.shun"; option java_outer_classname = "StudentProto"; message Student { required int32 id = 1; optional string name = 2; optional int32 age = 3; }</span>
teacher.proto
import "student.proto"; option java_package = "com.shun"; option java_outer_classname = "TeacherProto"; message Teacher { required int32 id = 1; optional string name = 2; repeated Student student_list = 3; }</span>
這里我們遇到了一些比較奇怪的東西:
import,int32,repated,required,optional,option等
一個個來吧:
1)import表示引入其他的proto文件
2)required,optional表示字段是否可選,這個決定了該字段有無值的情況下protobuffer會進行什么處理。如果標志了required,但當(dāng)處理時,該字段沒有進行傳值,則會報錯;如果標志了optional,不傳值則不會有什么問題。
3)repeated相信應(yīng)該都看得懂了,就是是否重復(fù),跟JAVA里面的list類似
4)message就是相當(dāng)于class了
5)option表示選項,其中的java_package表示包名,即生成JAVA代碼時使用的包名,java_outer_classname即為類名,注意這個類名不能跟下面的message中的類名相同。
至于還有其他的選項和相關(guān)類型的,請參觀官方文檔。
2、有了這幾個文件,我們能怎么樣呢?
記得上面下載的編譯器了吧,解壓出來,我們得到一個protoc.exe,這當(dāng)然是windows下的,我沒弄其他系統(tǒng)的,有興趣的同學(xué)去折騰下羅。
加到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,相信一看就知道是建造者模式了。
這時可以把代碼貼到你的項目中了,當(dāng)然,錯誤一堆了。
記得我們前面下載的源代碼嗎?解壓它吧,不要手軟。然后找到src/main/java/復(fù)制其中的一堆到你的項目,當(dāng)然,你也可以ant或者maven編譯,但這兩個東西我都不熟,就不獻丑了,我還是習(xí)慣直接復(fù)制到項目中。
代碼出錯,哈哈,正常。不知道為何,GOOGLE非要留下這么個坑給我們。
翻回到protobuffer目錄下的\java看到有個readme.txt了吧,找到一句:
看來看去,感覺這個代碼會有點奇怪的,好像錯錯的感覺,反正我是沒按那個執(zhí)行,我的命令是:
<span style="font-size: 16px;">protoc --java_out=還是上面的放代碼的地方 proto文件的路徑(這里是descriptor.proto文件的路徑)</span>
執(zhí)行后,我們可以看到代碼中的錯誤木有了。
3、接下來當(dāng)然就是測試了。
我們先進行GPB寫入測試:
package com.shun.test; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.shun.StudentProto.Student; import com.shun.TeacherProto.Teacher; public class ProtoWriteTest { public static void main(String[] args) throws IOException { Student.Builder stuBuilder = Student.newBuilder(); stuBuilder.setAge(25); stuBuilder.setId(11); stuBuilder.setName("shun"); //構(gòu)造List List<Student> stuBuilderList = new ArrayList<Student>(); stuBuilderList.add(stuBuilder.build()); Teacher.Builder teaBuilder = Teacher.newBuilder(); teaBuilder.setId(1); teaBuilder.setName("testTea"); teaBuilder.addAllStudentList(stuBuilderList); //把gpb寫入到文件 FileOutputStream fos = new FileOutputStream("C:\\Users\\shun\\Desktop\\test\\test.protoout"); teaBuilder.build().writeTo(fos); fos.close(); } }</span>
我們?nèi)タ纯次募?,如無意外,應(yīng)該是生成了的。
生成了之后,我們肯定要讀回它的。
package com.shun.test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import com.shun.StudentProto.Student; import com.shun.TeacherProto.Teacher; public class ProtoReadTest { public static void main(String[] args) throws FileNotFoundException, IOException { Teacher teacher = Teacher.parseFrom(new FileInputStream("C:\\Users\\shun\\Desktop\\test\\test.protoout")); System.out.println("Teacher ID:" + teacher.getId() + ",Name:" + teacher.getName()); for (Student stu:teacher.getStudentListList()) { System.out.println("Student ID:" + stu.getId() + ",Name:" + stu.getName() + ",Age:" + stu.getAge()); } } }</span>
代碼很簡單,因為GPB生成的代碼都幫我們完成了。
上面知道基本的用法了,我們重點來關(guān)注GPB跟JSON生成文件大小的區(qū)別,JSON的詳細代碼我這里就不貼了,之后會貼出示例,大家有興趣可以下載。
這里我們用Gson來解析JSON,下面只給出對象轉(zhuǎn)換成JSON后寫出文件的代碼:
兩個類Student和Teacher的基本定義就不弄了,大家隨意就行,代碼如下:
package com.shun.test; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; import com.shun.Student; import com.shun.Teacher; public class GsonWriteTest { public static void main(String[] args) throws IOException { Student stu = new Student(); stu.setAge(25); stu.setId(22); stu.setName("shun"); List<Student> stuList = new ArrayList<Student>(); stuList.add(stu); Teacher teacher = new Teacher(); teacher.setId(22); teacher.setName("shun"); teacher.setStuList(stuList); String result = new Gson().toJson(teacher); FileWriter fw = new FileWriter("C:\\Users\\shun\\Desktop\\test\\json"); fw.write(result); fw.close(); } }</span>
接下來正式進入我們的真正測試代碼了,前面我們只是在列表中放入一個對象,接下來,我們依次測試100,1000,10000,100000,1000000,5000000這幾個數(shù)量的GPB和JSON生成的文件大小。
改進一下之前的GPB代碼,讓它生成不同數(shù)量的列表,再生成文件:
package com.shun.test; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.shun.StudentProto.Student; import com.shun.TeacherProto.Teacher; public class ProtoWriteTest { public static final int SIZE = 100; public static void main(String[] args) throws IOException { //構(gòu)造List List<Student> stuBuilderList = new ArrayList<Student>(); for (int i = 0; i < SIZE; i ++) { Student.Builder stuBuilder = Student.newBuilder(); stuBuilder.setAge(25); stuBuilder.setId(11); stuBuilder.setName("shun"); stuBuilderList.add(stuBuilder.build()); } Teacher.Builder teaBuilder = Teacher.newBuilder(); teaBuilder.setId(1); teaBuilder.setName("testTea"); teaBuilder.addAllStudentList(stuBuilderList); //把gpb寫入到文件 FileOutputStream fos = new FileOutputStream("C:\\Users\\shun\\Desktop\\test\\proto-" + SIZE); teaBuilder.build().writeTo(fos); fos.close(); } }</span>
這里的SIZE依次改成我們上面據(jù)說的測試數(shù),可以得到如下:
然后我們再看看JSON的測試代碼:
package com.shun.test; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; import com.shun.Student; import com.shun.Teacher; public class GsonWriteTest { public static final int SIZE = 100; public static void main(String[] args) throws IOException { List<Student> stuList = new ArrayList<Student>(); for (int i = 0; i < SIZE; i ++) { Student stu = new Student(); stu.setAge(25); stu.setId(22); stu.setName("shun"); stuList.add(stu); } Teacher teacher = new Teacher(); teacher.setId(22); teacher.setName("shun"); teacher.setStuList(stuList); String result = new Gson().toJson(teacher); FileWriter fw = new FileWriter("C:\\Users\\shun\\Desktop\\test\\json" + SIZE); fw.write(result); fw.close(); } }</span>
同樣的方法修改SIZE,并作相應(yīng)的測試。
可以明顯得看到j(luò)son的文件大小跟GPB的文件大小在數(shù)據(jù)量慢慢大上去的時候就會有比較大的差別了,JSON明顯要大上許多。
上面的表應(yīng)該可以看得比較清楚了,在大數(shù)據(jù)的GPB是非常占優(yōu)勢的,但一般情況下客戶端和服務(wù)端并不會直接進行這么大數(shù)據(jù)的交互,大數(shù)據(jù)主要發(fā)生在服務(wù)器端的傳輸上,如果你面對需求是每天需要把幾百M的日志文件傳到另外一臺服務(wù)器,那么這里GPB可能就能幫你的大忙了。
說是深度對比,其實主要對比的是大小方面,時間方面可比性不會太大,也沒相差太大。
文章中選擇的Gson解析器,有興趣的朋友可以選擇Jackson或者fastjson,又或者其他的,但生成的文件大小是一樣的,只是解析時間有區(qū)別。
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)及算法實例:插入排序 Insertion Sort
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實例:插入排序 Insertion Sort,本文直接給出實例代碼,代碼中包含詳細注釋,需要的朋友可以參考下2015-06-06Java中forward轉(zhuǎn)發(fā)與redirect重定向的區(qū)別
轉(zhuǎn)發(fā)和重定向都是常用的頁面跳轉(zhuǎn)方式,但在實現(xiàn)上有一些區(qū)別,本文主要介紹了Java中forward轉(zhuǎn)發(fā)與redirect重定向的區(qū)別,具有一定的參考價值,感興趣的可以了解一下2023-11-11Spring實現(xiàn)類私有方法的幾個問題(親測通用解決方案)
現(xiàn)實的業(yè)務(wù)場景中,可能需要對Spring的實現(xiàn)類的私有方法進行測試。本文給大家分享Spring實現(xiàn)類私有方法面臨的幾個問題及解決方案,感興趣的朋友跟隨小編一起看看吧2021-06-06