protobuf java 安装及使用

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/HUXINY/article/details/82986519

安装

https://blog.csdn.net/warrially/article/details/52683611
下载
protobuf-java-3.0.0.tar.gz
解压

编译安装
./configure --prefix=/home/protobuf
make
sudo make install

使用

先在任意文件夹下编写一个xxx.proto文件,如下:
protoTest.proto

syntax = "proto3";

message testBean {
	int64 id = 1;
	string name = 2;
}

进入安装目录的bin目录,运行下面命令生成Java文件:
(这里我安装在了/home/protobuf)

./protoc -I=/home/pzs/protobuf/protofile --java_out=/home/pzs/protobuf/javaclass /home/pzs/protobuf/protofile/test.proto

解释:

  • 第1个路径/home/pzs/protobuf/protofile是存放test.proto文件的父目录.
  • 第2个路径/home/pzs/protobuf/javaclass是生成java的文件存放地址.
  • 第3个路径/home/pzs/protobuf/protofile/protoTest.proto是完成整的proto文件路径.
    (第一和第三个路径声明不是冗余了吗?)

完成上述操作后可以看到protobuf为我们在/home/pzs/protobuf/javaclass路径下自动生成了ProtoTest.java文件.

这个自动生成的ProtoTest.java我们提供了哪些功能?

注意:要使用ProtoTest.java要在maven中添加下面的依赖:

    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>3.5.0</version>
    </dependency>
  • set, get功能.
        ProtoTest.testBean.Builder protoTest_builder = ProtoTest.testBean.newBuilder();
        protoTest_builder.setId(10000);
        protoTest_builder.setName("huxinyi");
        System.out.println(protoTest_builder.getId());
        System.out.println(protoTest_builder.getName());
  • 序列化和反序列化功能.
        ProtoTest.testBean.Builder protoTest_builder = ProtoTest.testBean.newBuilder();
        protoTest_builder.setId(10000);
        protoTest_builder.setName("huxinyi");
        System.out.println(protoTest_builder.getId());
        System.out.println(protoTest_builder.getName());

        ProtoTest.testBean serialization = protoTest_builder.build();
        ProtoTest.testBean deserialization = ProtoTest.testBean.parseFrom(serialization.toByteArray());
        System.out.println(serialization.getId());
        System.out.println(serialization.getName());

或者将序列化数据存放到文件中

        ProtoTest.testBean.Builder protoTest_builder = ProtoTest.testBean.newBuilder();
        protoTest_builder.setId(10000);
        protoTest_builder.setName("huxinyi");
        System.out.println(protoTest_builder.getId());
        System.out.println(protoTest_builder.getName());

        ProtoTest.testBean serialization = protoTest_builder.build();
        ProtoTest.testBean deserialization = null;
        OutputStream os = new FileOutputStream(new File("/home/pzs/protobuf/serializationFile"));
        FileInputStream fis = new FileInputStream(new File("/home/pzs/protobuf/serializationFile"));
        serialization.writeTo(os);
        deserialization = ProtoTest.testBean.parseFrom(fis);
        System.out.println(deserialization.getId());
        System.out.println(deserialization.getName());
  • protobuf与json相互转换

需要用到JsonFormat,要在maven里添加依赖支持

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

protobuf转json

        ProtoTest.testBean.Builder protoTest_builder = ProtoTest.testBean.newBuilder();
        protoTest_builder.setId(10000);
        protoTest_builder.setName("huxinyi");
        
        String js = JsonFormat.printer().print(protoTest_builder);
        System.out.println(js);

json转protobuf
(像个一合并操作)

        ProtoTest.testBean.Builder protoTest_builder = ProtoTest.testBean.newBuilder();
        ProtoTest.testBean.Builder formjson_build = ProtoTest.testBean.newBuilder();
        protoTest_builder.setId(10000);
        protoTest_builder.setName("husin");
        
        String js = JsonFormat.printer().print(protoTest_builder);
        System.out.println(js);
        
        JsonFormat.Parser parser = JsonFormat.parser();
        parser.merge(js,formjson_build);
        System.out.println(formjson_build.getId());
        System.out.println(formjson_build.getName());

总结

Protocol buffers是一个灵活的、高效的、自动化的用于对结构化数据进行序列化的协议,将数据结构化、序列化后有利于数据理解、传输和存储。

Protocol buffers数据结构是允许类型嵌套的,这就让它跟json一样强大了。

Protocol buffers支持的数据类型:数据类型这里可以去看Protobuf支持的基本数据类型的表。

展开阅读全文

没有更多推荐了,返回首页