Protocol Buffers - 複合資料型別



概述

還有兩種複合資料型別可能對複雜的用例很有用。它們是“oneof”“any”。在本章中,我們將學習如何使用這兩種 Protobuf 資料型別。

oneof

我們將一些引數傳遞給此oneof資料型別,Protobuf確保只有一個引數被設定。如果我們設定其中一個,並嘗試設定另一個,第一個屬性將被重置。讓我們透過一個例子來理解這一點。

繼續我們來自Protocol Buffers - 字串章節的theater示例,假設我們有一個用於獲取可用員工數量的API。從該API返回的值隨後被設定為以下檔案中的'count'標籤。但是,如果該API出錯,我們無法真正獲得'count',而是附加錯誤日誌。

理想情況下,我們總是會設定其中一個,即呼叫成功並且我們得到計數,或者計數計算失敗並且我們得到錯誤訊息。

以下是我們需要使用的語法,以指示 Protobuf 我們將建立一個oneof屬性:

theater.proto

syntax = "proto3";
package theater;
option java_package = "com.tutorialspoint.theater";

message Theater { 
   oneof availableEmployees {
      int32 count = 4;
      string errorLog = 5;
   }
}

使用Java進行序列化

要使用 Protobuf,我們現在必須使用protoc二進位制檔案從此“.proto”檔案建立所需的類。讓我們看看如何做到這一點:

protoc  --java_out=. theater.proto

這將在當前目錄的com > tutorialspoint > theater資料夾中建立一個TheaterOuterClass.java類。我們像在Protocol Buffers - 基本應用章節中所做的那樣,在我們的應用程式中使用此類。

使用從Proto檔案建立的Java類

首先,我們將建立一個寫入器來寫入劇院資訊:

TheaterWriter.java

package com.tutorialspoint.theater;

import java.io.FileOutputStream;
import java.io.IOException;

import com.tutorialspoint.theater.TheaterOuterClass.Theater;

public class TheaterWriter{
   public static void main(String[] args) throws IOException {
    	   
      Theater theater = Theater.newBuilder()
         .setCount(3)
         .setErrorLog("No employee found")
         .build();
		
      String filename = "theater_protobuf_output";
      System.out.println("Saving theater information to file: " + filename);
		
      try(FileOutputStream output = new FileOutputStream(filename)){
         theater.writeTo(output);
      }
      System.out.println("Saved theater information with following data to disk: \n" + theater);
   }
}

接下來,我們將有一個讀取器來讀取劇院資訊:

TheaterReader.java

package com.tutorialspoint.theater;

import java.io.FileInputStream;
import java.io.IOException;

import com.tutorialspoint.theater.TheaterOuterClass.Theater;
import com.tutorialspoint.theater.TheaterOuterClass.Theater.Builder;

public class TheaterReader{
   public static void main(String[] args) throws IOException {
      Builder theaterBuilder = Theater.newBuilder();

      String filename = "theater_protobuf_output";
      System.out.println("Reading from file " + filename);
        
      try(FileInputStream input = new FileInputStream(filename)) {
         Theater theater = theaterBuilder.mergeFrom(input).build();
         System.out.println(theater);
      }
   }
}

編譯專案

現在我們已經設定了讀取器寫入器,讓我們編譯專案。

mvn clean install

序列化Java物件

現在,編譯後,讓我們首先執行寫入器

> java -cp .\target\protobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter

Saving theater information to file: theater_protobuf_output
Saved theater information with following data to disk:
errorLog: "No employee found"

反序列化序列化物件

現在,讓我們執行讀取器從同一檔案讀取:

java -cp .\target\protobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader

Reading from file theater_protobuf_output
errorLog: "No employee found"

因此,正如我們所看到的,我們只能看到後來設定的錯誤日誌。

Any

下一個可用於複雜用例的資料型別是Any。我們可以將任何型別/訊息/類傳遞給此資料型別,Protobuf 不會報錯。讓我們透過一個例子來理解這一點。

繼續使用劇院示例,假設我們想要跟蹤劇院內的人員。其中一些可能是員工,另一些可能是觀眾。但最終他們都是人,所以我們將把他們放在一個包含兩種型別的列表中。

現在我們的類/訊息包含一個Any屬性'peopleInside'列表以及ViewerEmployee類,即劇院內人員的資訊。讓我們看看它的實際應用。

以下是我們需要使用的語法,以指示 Protobuf 我們將建立一個Any屬性:

theater.proto

syntax = "proto3";
package theater;
option java_package = "com.tutorialspoint.theater";

import "google/protobuf/any.proto";

message Theater {
   string name = 1;
   string address = 2;
  
   repeated google.protobuf.Any peopleInside = 3;
}
message Employee{
   string name = 1;
   string address = 2;
}
message Viewer{
   string name = 1;
   int32 age = 2;
   string sex = 3;
}

使用Java進行序列化

要使用 Protobuf,我們現在必須使用protoc二進位制檔案從此“.proto”檔案建立所需的類。讓我們看看如何做到這一點:

protoc  --java_out=. theater.proto

這將在當前目錄的com > tutorialspoint > theater資料夾中建立一個TheaterOuterClass.java類。我們像在Protocol Buffers - 基本應用章節中所做的那樣,在我們的應用程式中使用此類。

使用從Proto檔案建立的Java類

首先,我們將建立一個寫入器來寫入劇院資訊:

TheaterWriter.java

package com.tutorialspoint.theater;

import java.util.List;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import com.google.protobuf.Any;
import com.tutorialspoint.theater.TheaterOuterClass.Employee;
import com.tutorialspoint.theater.TheaterOuterClass.Theater;
import com.tutorialspoint.theater.TheaterOuterClass.Viewer;

public class TheaterWriter{
   public static void main(String[] args) throws IOException {
	    
   List<Any> people = new ArrayList<>();
   people.add(Any.pack(Employee.newBuilder().setName("John").build()));
   people.add(Any.pack(Viewer.newBuilder().setName("Jane").setAge(30).build()));
   people.add(Any.pack(Employee.newBuilder().setName("Simon").build()));
   people.add(Any.pack(Viewer.newBuilder().setName("Janice").setAge(25).build()));
	    
   Theater theater = Theater.newBuilder()
      .setName("SilverScreen")
      .addAllPeopleInside(people)
      .build();
		
      String filename = "theater_protobuf_output_silver";
      System.out.println("Saving theater information to file: " + filename);
		
      try(FileOutputStream output = new FileOutputStream(filename)){
         theater.writeTo(output);
      }
	    
      System.out.println("Saved theater information with following data to disk: \n" + theater);
   }
}

接下來,我們將有一個讀取器來讀取劇院資訊:

TheaterReader.java

package com.tutorialspoint.theater;

import java.io.FileInputStream;
import java.io.IOException;

import com.google.protobuf.Any;
import com.tutorialspoint.theater.TheaterOuterClass.Employee;
import com.tutorialspoint.theater.TheaterOuterClass.Theater;
import com.tutorialspoint.theater.TheaterOuterClass.Theater.Builder;
import com.tutorialspoint.theater.TheaterOuterClass.Viewer;

public class TheaterReader{
   public static void main(String[] args) throws IOException {
	   Builder theaterBuilder = Theater.newBuilder();

      String filename = "theater_protobuf_output_silver";
      System.out.println("Reading from file " + filename);
        
      try(FileInputStream input = new FileInputStream(filename)) {
         Theater theater = theaterBuilder.mergeFrom(input).build();
            
         System.out.println("Name:" + theater.getName() + "\n");
            
         for (Any anyPeople : theater.getPeopleInsideList()) {
            if(anyPeople.is(Employee.class)) {
               Employee employee = anyPeople.unpack(Employee.class);
               System.out.println("Employee:" + employee + "\n");
            }
                
            if(anyPeople.is(Viewer.class)) {
               Viewer viewer = anyPeople.unpack(Viewer.class);
               System.out.println("Viewer:" + viewer + "\n");
            }
         }
      }
   }
}	

編譯專案

現在我們已經設定了讀取器寫入器,讓我們編譯專案。

mvn clean install

序列化Java物件

現在,編譯後,讓我們首先執行寫入器

> java -cp .\target\protobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter

Saving theater information to file: theater_protobuf_output
Saved theater information with following data to disk:
name: "SilverScreen"
peopleInside {
   type_url: "type.googleapis.com/theater.Employee"
   value: "\n\004John"
}
peopleInside {
   type_url: "type.googleapis.com/theater.Viewer"
   value: "\n\004Jane\020\036"
}
peopleInside {
   type_url: "type.googleapis.com/theater.Employee"
   value: "\n\005Simon"
}
peopleInside {
   type_url: "type.googleapis.com/theater.Viewer"
   value: "\n\006Janice\020\031"
}

注意 - 需要注意兩點:

  • 對於Any,Protobuf 將任何標籤內的內容打包/序列化為位元組,然後將其儲存為'value'。基本上,這允許我們使用此'Any'標籤傳送任何訊息型別。

  • 我們還看到了"type.googleapis.com/theater.Viewer""type.googleapis.com/theater.Employee"。Protobuf 使用它來儲存物件型別以及資料,因為Any資料型別的型別可能有所不同。

反序列化序列化物件

現在讓我們執行讀取器從同一檔案讀取:

java -cp .\target\protobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader

Reading from file theater_protobuf_output_silver
Name:SilverScreen

Employee:name: "John"

Viewer:name: "Jane"
age: 30

Employee:name: "Simon"

Viewer:name: "Janice"
age: 25

因此,正如我們所看到的,我們的讀取器程式碼能夠成功地區分EmployeeViewer,即使它們出現在同一個陣列中。

廣告
© . All rights reserved.