- Protocol Buffers 教程
- Protocol Buffers - 首頁
- Protocol Buffers - 簡介
- Protocol Buffers - 基本應用
- Protocol Buffers - 結構
- Protocol Buffers - 訊息
- Protocol Buffers - 字串
- Protocol Buffers - 數字
- Protocol Buffers - 布林值
- Protocol Buffers - 列舉
- Protocol Buffers - 重複欄位
- Protocol Buffers - 對映
- Protocol Buffers - 巢狀類
- Protocol Buffers - 可選欄位和預設值
- Protocol Buffers - 語言無關性
- Protocol Buffers - 複合資料型別
- Protocol Buffers - 命令列用法
- Protocol Buffers - 更新定義規則
- Protocol Buffers - 與 Kafka 整合
- Protocol Buffers - 其他語言支援
- Protocol Buffers 有用資源
- Protocol Buffers - 快速指南
- Protocol Buffers - 有用資源
- Protocol Buffers - 討論
Protocol Buffers - 數字
概述
數字包括 protobuf 型別,例如 **int32、int64、float、double**,它們是 Protobuf 的基本構建塊。它們分別轉換為我們使用的語言(例如 Java、Python 等)中的 **int、long、float、double**。
繼續我們從 Protocol Buffers - 字串 章節中的 **theater** 示例,以下是我們需要使用的語法,以便指示 Protobuf 我們將建立 **數字**:
theater.proto
syntax = "proto3";
package theater;
option java_package = "com.tutorialspoint.theater";
message Theater {
int32 total_capcity = 3;
int64 mobile = 4;
float base_ticket_price = 5;
}
現在我們的 **類/訊息** 包含 **數值** 屬性。每個屬性還有一個 **位置**,這是 Protobuf 在序列化和反序列化過程中使用的。**成員** 的每個屬性都需要分配一個唯一的編號。
從 Proto 檔案建立 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()
.setTotalCapcity(320)
.setMobile(98234567189L)
.setBaseTicketPrice(22.45f)
.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);
}
}
接下來,我們將有一個 **讀取器** 來讀取 **theater** 資訊:
TheaterReader.java
package com.tutorialspoint.theater;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.tutorialspoint.greeting.Greeting.Greet;
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.getBaseTicketPrice());
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: total_capcity: 320 mobile: 98234567189 base_ticket_price: 22.45
反序列化序列化物件
現在,讓我們執行 **讀取器** 從同一個檔案讀取:
java -cp .\target\protobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader Reading from file theater_protobuf_output 22.45 total_capcity: 320 mobile: 98234567189 base_ticket_price: 22.45
因此,正如我們所看到的,我們能夠透過將二進位制資料反序列化為 **Theater** 物件來讀取序列化的 **int、float** 和 **long**。在下一章 Protocol Buffers - 布林值 中,我們將瞭解布林型別。