- 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 - 語言獨立性
概述
到目前為止,我們一直在使用 Java 來序列化和反序列化電影院資料。但是,Google Protocol Buffers 提供的關鍵功能之一是“語言獨立性”。在本章中,我們將瞭解如何使用 Java 進行序列化,並使用 Python 進行反序列化。
繼續我們從Protocol Buffers - 字串章節開始的theater示例,以下是我們需要使用的語法,以指示 Protobuf 我們將建立不同的資料型別:
theater.proto
syntax = "proto3";
package theater;
option java_package = "com.tutorialspoint.theater";
message Theater {
string name = 1;
string address = 2;
int32 total_capcity = 3;
int64 mobile = 4;
float base_ticket_price = 5;
bool drive_in = 6;
enum PAYMENT_SYSTEM {
CASH = 0;
CREDIT_CARD = 1;
DEBIT_CARD = 2;
APP = 3;
}
PAYMENT_SYSTEM payment = 7;
repeated string snacks = 8;
map<string, int32> movieTicketPrice = 9;
TheaterOwner owner = 10;
}
message TheaterOwner{
string name = 1;
string address = 2;
}
使用 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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.tutorialspoint.theater.TheaterOuterClass.Theater;
import com.tutorialspoint.theater.TheaterOuterClass.TheaterOwner;
import com.tutorialspoint.theater.TheaterOuterClass.Theater.PAYMENT_SYSTEM;
public class TheaterWriter {
public static void main(String[] args) throws IOException {
TheaterOwner owner = TheaterOwner.newBuilder()
.setName("Anthony Gonsalves")
.setAddress("513, St Paul Street, West Coast, California")
.build();
List<String> snacks = new ArrayList<>();
snacks.add("Popcorn");
snacks.add("Coke");
snacks.add("Chips");
snacks.add("Soda");
Map<String, Integer> ticketPrice = new HashMap<>();
ticketPrice.put("Avengers Endgame", 700);
ticketPrice.put("Captain America", 200);
ticketPrice.put("Wonder Woman 1984", 400);
Theater theater = Theater.newBuilder()
.setName("Silver Screener")
.setAddress("212, Maple Street, LA, California")
.setDriveIn(true)
.setTotalCapcity(320)
.setMobile(98234567189L)
.setBaseTicketPrice(22.45f)
.setPayment(PAYMENT_SYSTEM.CREDIT_CARD)
.putAllMovieTicketPrice(ticketPrice)
.addAllSnacks(snacks)
.setOwner(owner)
.build();
String filename = "E:/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);
}
}
讓我們編譯專案。
mvn clean install
序列化 Java 物件
現在,編譯後,讓我們執行writer:
> java -cp .\target\protobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter
Saving theater information to file: E:/theater_protobuf_output
Saved theater information with following data to disk:
name: "Silver Screener"
address: "212, Maple Street, LA, California"
total_capcity: 320
mobile: 98234567189
base_ticket_price: 22.45
drive_in: true
payment: CREDIT_CARD
snacks: "Popcorn"
snacks: "Coke"
snacks: "Chips"
snacks: "Soda"
movieTicketPrice {
key: "Avengers Endgame"
value: 700
}
movieTicketPrice {
key: "Captain America"
value: 200
}
movieTicketPrice {
key: "Wonder Woman 1984"
value: 400
}
owner {
name: "Anthony Gonsalves"
address: "513, St Paul Street, West Coast, California"
}
使用 Python 反序列化序列化物件
從 proto 檔案生成 Python 類
讓我們為 Theater 類生成 Python 程式碼:
protoc --python_out=. theater.proto
執行此命令後,您會在當前目錄中注意到一個自動生成的類theater_pb2.py。此類將幫助我們反序列化Theater物件。
使用生成的 Python 類
現在,讓我們編寫資料讀取器,它將使用 Java 讀取包含序列化物件的檔案:
theaterReader.py
import theater_pb2
filename = "E:/theater_protobuf_output";
print("Reading from file: " + filename)
theater = theater_pb2.Theater()
f = open(filename, "rb")
theater.ParseFromString(f.read())
f.close()
print("Read theater from disk: \n" + str(theater))
然後,讓我們執行reader。
python theaterReader.py
Reading from file: E:/greeting_protobuf_output
Read theater from disk:
name: "Silver Screener"
address: "212, Maple Street, LA, California"
total_capcity: 320
mobile: 98234567189
base_ticket_price: 22.45
drive_in: true
payment: CREDIT_CARD
snacks: "Popcorn"
snacks: "Coke"
snacks: "Chips"
snacks: "Soda"
movieTicketPrice {
key: "Wonder Woman 1984"
value: 400
}
movieTicketPrice {
key: "Captain America"
value: 200
}
movieTicketPrice {
key: "Avengers Endgame"
value: 700
}
owner {
name: "Anthony Gonsalves"
address: "513, St Paul Street, West Coast, California"
}
因此,正如我們所看到的,Java 客戶端寫入的所有值都被正確地反序列化並由我們的 Python 客戶端讀取,這有效地意味著 Protobuf 是語言獨立的。
廣告