JSON相信大家都知道是什么東西,如果不知道,那可就真的OUT了,GOOGLE一下去。這里就不介紹啥的了。
Protobuffer大家估計就很少聽說了,但如果說到是GOOGLE搞的,相信大家都會有興趣去試一下,畢竟GOOGLE出口,多屬精品。
Protobuffer是一個類似JSON的一個傳輸協議,其實也不能說是協議,只是一個數據傳輸的東西罷了。
那它跟JSON有什么區別呢?
跨語言,這是它的一個優點。它自帶了一個編譯器,protoc,只需要用它進行編譯,可以編譯成JAVA、python、C++代碼,暫時只有這三個,其他就暫時不要想了,然后就可以直接使用,不需要再寫任何其他代碼。連解析的那些都已經自帶有的。JSON當然也是跨語言的,但這個跨語言是建立在編寫代碼的基礎上。
如果想再深入了解的,可以去看看:
https://developers.google.com/protocol-buffers/docs/overview
好了,廢話不多說,我們直接來看看,為什么我們需要對比protobuffer(下面簡稱GPB)和JSON。
1、JSON因為有一定的格式,并且是以字符存在的,在數據量上還有可以壓縮的空間。而GPB上大數據量時,空間比JSON小很多,等一下的例子我們可以看到。
2、JSON各個庫之間的效率相差比較大,jackson庫和GSON就大概有5-10的差距(這個只做過一次測試,如有誤,請大家輕拍)。而GPB只需要一個,沒有所謂的多個庫的區別。當然這個點只是弄出來湊數的,可以忽略不計哈。
Talk is cheap,Just show me the code。
在程序界,代碼永遠是王道,下面就直接來代碼吧。
上代碼前,大家要先下載protobuffer,在這里:
https://github.com/google/protobuf
1、首先,GPB是需要有一個類似類定義的文件,叫proto文件 。
我們以學生和老師的例子來進行一個例子:
我們有以下兩個文件:student.proto
1
2
3
4
5
6
7
8
|
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
1
2
3
4
5
6
7
8
9
10
|
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,但當處理時,該字段沒有進行傳值,則會報錯;如果標志了optional,不傳值則不會有什么問題。
3)repeated相信應該都看得懂了,就是是否重復,跟JAVA里面的list類似
4)message就是相當于class了
5)option表示選項,其中的java_package表示包名,即生成JAVA代碼時使用的包名,java_outer_classname即為類名,注意這個類名不能跟下面的message中的類名相同。
至于還有其他的選項和相關類型的,請參觀官方文檔。
2、有了這幾個文件,我們能怎么樣呢?
記得上面下載的編譯器了吧,解壓出來,我們得到一個protoc.exe,這當然是windows下的,我沒弄其他系統的,有興趣的同學去折騰下羅。
加到path(加不加可以隨便,只是方不方便而已),然后就可以通過上面的文件生成我們需要的類文件了。
protoc --java_out=存放源代碼的路徑 --proto_path=proto文件的路徑 proto具體文件
--proto_path指定的是proto文件的文件夾路徑,并不是單個文件,主要是為了import文件查找使用的,可以省略
如我需要把源代碼放在D:\protobufferVsJson\src,而我的proto文件存放在D:\protoFiles
那么我的編譯命令就是:
1
2
|
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了吧,找到一句:
看來看去,感覺這個代碼會有點奇怪的,好像錯錯的感覺,反正我是沒按那個執行,我的命令是:
1
|
<span style="font-size: 16px;">protoc --java_out=還是上面的放代碼的地方 proto文件的路徑(這里是descriptor.proto文件的路徑)</span> |
執行后,我們可以看到代碼中的錯誤木有了。
3、接下來當然就是測試了。
我們先進行GPB寫入測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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" ); //構造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> |
我們去看看文件,如無意外,應該是生成了的。
生成了之后,我們肯定要讀回它的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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生成的代碼都幫我們完成了。
上面知道基本的用法了,我們重點來關注GPB跟JSON生成文件大小的區別,JSON的詳細代碼我這里就不貼了,之后會貼出示例,大家有興趣可以下載。
這里我們用Gson來解析JSON,下面只給出對象轉換成JSON后寫出文件的代碼:
兩個類Student和Teacher的基本定義就不弄了,大家隨意就行,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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這幾個數量的GPB和JSON生成的文件大小。
改進一下之前的GPB代碼,讓它生成不同數量的列表,再生成文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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 { //構造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依次改成我們上面據說的測試數,可以得到如下:
然后我們再看看JSON的測試代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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,并作相應的測試。
可以明顯得看到json的文件大小跟GPB的文件大小在數據量慢慢大上去的時候就會有比較大的差別了,JSON明顯要大上許多。
上面的表應該可以看得比較清楚了,在大數據的GPB是非常占優勢的,但一般情況下客戶端和服務端并不會直接進行這么大數據的交互,大數據主要發生在服務器端的傳輸上,如果你面對需求是每天需要把幾百M的日志文件傳到另外一臺服務器,那么這里GPB可能就能幫你的大忙了。
說是深度對比,其實主要對比的是大小方面,時間方面可比性不會太大,也沒相差太大。
文章中選擇的Gson解析器,有興趣的朋友可以選擇Jackson或者fastjson,又或者其他的,但生成的文件大小是一樣的,只是解析時間有區別。