资源描述
JSON相信大家都知道是什么东西,Protobuffer大家估计就很少听说了,但如果说到是GOOGLE推出的,相信大家都会有兴趣了解一下,毕竟GOOGLE出品,多属精品。
Protobuffer是一个类似JSON的一个传输协议,其实准确来说不应该叫协议,而是定义了数据封装的格式。
那它跟JSON有什么区别呢?
跨语言,这是它的一个优点。它自带了一个编译器,protoc,只需要用它进行编译,可以编译成JAVA、python、C++代码,暂时只有这三个,其他就暂时不要想了,然后就可以直接使用,不需要再写任何其他代码。JSON当然也是跨语言的,但这个跨语言是建立在编写代码的基础上,bean的定义和数据解析还得自己做。
我们直接来看看,为什么我们需要对比protobuffer(下面简称GPB)和JSON。
1、JSON因为有一定的格式,并且是以字符存在的,在数据量上还有可以压缩的空间。而GPB上大数据量时,空间比JSON小很多,等一下的例子我们可以看到。
2、JSON各个库之间的效率相差比较大,jackson库和GSON就大概有5-10的差距(这个只做过一次测试,如有误,请大家轻拍)。而GPB只需要一个,没有所谓的多个库的区别。
Talk is cheap,Just show me the code。
在程序界,代码永远是王道,下面就直接来代码吧。
上代码前,大家要先下载protobuffer,在这里:
注意,需要下载两个,一个是complier,另外一个是source code,相信这个难不倒大家了,这里略过。
1、首先,GPB是需要有一个类似类定义的文件,叫proto文件 。
我们以学生和老师的例子来进行一个例子:
我们有以下两个文件:student.proto
Java代码
1. option java_package = "com.shun";
2. option java_outer_classname = "StudentProto";
3. message Student {
4. required int32 id = 1;
5. optional string name = 2;
6. optional int32 age = 3;
7. }
teacher.proto
Java代码
1. import "student.proto";
2. option java_package = "com.shun";
3. option java_outer_classname = "TeacherProto";
4. message Teacher {
5. required int32 id = 1;
6. optional string name = 2;
7. repeated Student student_list = 3;
8. }
这里我们遇到了一些比较奇怪的东西:
import,int32,repated,required,optional,option等
一个个来吧:
1)import表示引入其他的proto文件
2)required,optional表示字段是否可选,这个决定了该字段有无值的情况下protobuffer会进行什么处理。如果标志了required,但当处理时,该字段没有进行传值,则会报错;如果标志了optional,不传值则不会有什么问题。
3)repeated相信应该都看得懂了,就是是否重复,也就是数据或集合
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
那么我的编译命令就是:
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. protoc --java_out=还是上面的放代码的地方 proto文件的路径(这里是descriptor.proto文件的路径)
执行后,我们可以看到代码中的错误木有了。
3、接下来当然就是测试了。
我们先进行GPB写入测试:
Java代码
1. 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. }
我们去看看文件,如无意外,应该是生成了的。
生成了之后,我们肯定要读回它的。
Java代码
1. 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. }
代码很简单,因为GPB生成的代码都帮我们完成了。
上面知道基本的用法了,我们重点来关注GPB跟JSON生成文件大小的区别,JSON的详细代码我这里就不贴了,之后会贴出示例,大家有兴趣可以下载。
这里我们用Gson来解析JSON,下面只给出对象转换成JSON后写出文件的代码:
两个类Student和Teacher的基本定义就不弄了,大家随意就行,代码如下:
Java代码
1. 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. }
接下来正式进入我们的真正测试代码了,前面我们只是在列表中放入一个对象,接下来,我们依次测试100,1000,10000,100000,1000000,5000000这几个数量的GPB和JSON生成的文件大小。
改进一下之前的GPB代码,让它生成不同数量的列表,再生成文件:
Java代码
1. 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. }
这里的SIZE依次改成我们上面据说的测试数,可以得到如下:
然后我们再看看JSON的测试代码:
Java代码
1. 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. }
同样的方法修改SIZE,并作相应的测试。
可以明显得看到json的文件大小跟GPB的文件大小在数据量慢慢大上去的时候就会有比较大的差别了,JSON明显要大上许多。
上面的表应该可以看得比较清楚了,在大数据的GPB是非常占优势的,但一般情况下客户端和服务端并不会直接进行这么大数据的交互,大数据主要发生在服务器端的传输上,如果你面对需求是每天需要把几百M的日志文件传到另外一台服务器,那么这里GPB可能就能帮你的大忙了。
说是深度对比,其实主要对比的是大小方面,时间方面可比性不会太大,也没相差太大。
文章中选择的Gson解析器,有兴趣的朋友可以选择Jackson或者fastjson,又或者其他的,但生成的文件大小是一样的,只是解析时间有区别。
展开阅读全文