其他技術----protobuf

protobuf介紹級使用

介紹

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

因爲他們都是一種數據傳輸格式。下面就簡單介紹一下protobuf吧

protobuf 是谷歌用於序列化結構化數據(如XML)的語言無關、平臺無關、可擴展的機制,他更小、更快和更簡單。一旦定義了數據的結構化方式,就可以使用特殊生成的源代碼輕鬆地從各種數據流和使用各種語言編寫和讀取結構化數據。

https://developers.google.com/protocol-buffers/

下載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編碼或7位ASCII文本,並且長度不能超過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方法會將屬性轉換爲八進制輸出。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章