Fork me on GitHub

其他技术----protobuf

protobuf介绍级使用

介绍

说起protobuf首先我第一想到的是jsonxml

因为他们都是一种数据传输格式。下面就简单介绍一下protobuf

protobuf是谷歌用于序列化结构化数据(XML)的语言无关、平台无关、可扩展的机制,他更小、更快和更简单。一旦定义了数据的结构化方式,就可以使用特殊生成的源代码轻松地从各种数据流和使用各种语言编写和读取结构化数据。

https://developers.google.com/protocol-buffers/ 需外网
https://github.com/protocolbuffers/protobuf/tags GitHub

下载protobuf

  1. 下载protocol buffer的编译器

    下载的时候注意一下您要使用的语言版本,目前支持的语言有c++ c# java javascript object-c php python ruby dart

    下载链接 https://github.com/protocolbuffers/protobuf/releases

  2. 安装您的编译器(windows)

    将下载好的protoc.exe 编译器放在一个不带中文路径的文件夹内。配置环境变量例如:C:\file\protoc-3.12.3-win64\bin

  3. 检查环境变量是否配置成功

    打开cmd输入protoc 看是否有东西输出

格式说明

syntax = "proto3";

option java_package = "com.example.test";//文件选项,生成的java代码所在的目录
option java_outer_classname = "ProBuf";//类名----PersonProBuf.java

message SearchRequest {
  required string query = 1;
  optional int32 page_number = 2;
  optional int32 result_per_page = 3;
  repeated int32 samples = 4 [packed=true];
  reserved 2, 15, 9 to 11;
  enum PhoneType {
       MOBILE = 0; //枚举类型的第一个默认值为0,必须是0
       HOME = 1;
       WORK = 2;
   }
}
  • syntax 申明版本,若不申明默认是2.0版本(2.0版本与3.0版本的差异还是蛮大的)
  • required 消息必须传的对象
  • optional 消息可传可不传的对象
  • repeated 相当List
  • message 相当class
  • option 文件选项可用于操作生成文件后的一系列操作
  • enum 枚举类型
  • reserved 保留字段,在版本改变的时候有用
  • 使用 // /**/的注释方式

编译protobuf文件为对应的实体类

protoc --proto_path C:\Users\hysong\Desktop --java_out ../src/main/java G:\code\thriftdemo\src\main\resources\hello.proto

对于各语言生成的对照表

.proto Type Notes C++ Type Java Type Python Type[2] Go Type Ruby Type C# Type PHP Type Dart Type
double double double float float64 Float double float double
float float float float float32 Float float float double
int32 使用可变长度编码。编码负数效率低下–如果字段可能有负值,请改用sint32 int32 int int int32 Fixnum or Bignum (as required) int integer int
int64 使用可变长度编码。编码负数效率低下–如果字段可能有负值,请改用sint64 int64 long int/long[3] int64 Bignum long integer/string[5] Int64
uint32 使用可变长度编码。 uint32 int[1] int/long[3] uint32 Fixnum or Bignum (as required) uint integer int
uint64 使用可变长度编码。 uint64 long[1] int/long[3] uint64 Bignum ulong integer/string[5] Int64
sint32 使用可变长度编码。有符号整型值。这些比普通的int32更有效地编码负数。 int32 int int int32 Fixnum or Bignum (as required) int integer int
sint64 使用可变长度编码。有符号整型值。这些比普通的int64更有效地编码负数。 int64 long int/long[3] int64 Bignum long integer/string[5] Int64
fixed32 总是四个字节。如果值通常大于228,则比uint32更有效。 uint32 int[1] int/long[3] uint32 Fixnum or Bignum (as required) uint integer int
fixed64 总是8个字节。如果值通常大于256.uint64,则比uint64更有效 uint64 long[1] int/long[3] uint64 Bignum ulong integer/string[5] Int64
sfixed32 总是四个字节。 int32 int int int32 Fixnum or Bignum (as required) int integer int
sfixed64 总是8个字节。 int64 long int/long[3] int64 Bignum long integer/string[5] Int64
bool bool boolean bool bool TrueClass/FalseClass bool boolean bool
string 字符串必须始终包含UTF-8编码或7ASCII文本,并且长度不能超过232 string String str/unicode[4] string String (UTF-8) string string String
bytes 可以包含不超过232的任意字节序列。 string ByteString str []byte String (ASCII-8BIT) ByteString string List

使用方式

JAVA使用方式

  • 项目中导入maven依赖
<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>3.9.0</version>
</dependency>
  • 编写protobuf文件并使用命令protoc --java_out test.proto 生成proto文件

  • 将生成的java文件放入IDE中编写测试代码

    public class Test {
      private static String local = "C:\\Users\\hysong\\Desktop\\prototbuf.data";
    
      public static void main(String[] args) throws IOException {
        //    writePB();
        PersonPB.Person person = readPB();
        System.out.println(person);
      }
    
      private static PersonPB.Person readPB() throws IOException {
        InputStream inputStream = new FileInputStream(local);
        PersonPB.Person person = PersonPB.Person.parseFrom(inputStream);
        return person;
      }
    
      private static void writePB() throws IOException {
        PersonPB.Person person = PersonPB.Person.newBuilder().setId(1).setName("小红").build();
        System.out.printf("person: {%s}", person);
        @Cleanup FileOutputStream fileOutputStream = new FileOutputStream(local);
        person.writeTo(fileOutputStream);  }
    }
    
    

    我们可以打开看下生成的二进制文件

    • 这里的protobuf生成的Java对象的toString方法会将属性转换为八进制输出。
posted @   Banana_Fish  阅读(233)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
点击右上角即可分享
微信分享提示