- FlatBuffers 教程
- FlatBuffers - 首頁
- FlatBuffers - 簡介
- FlatBuffers - Schema
- FlatBuffers - 構造
- FlatBuffers - 表格
- FlatBuffers - 字串
- FlatBuffers - 數字
- FlatBuffers - 布林值
- FlatBuffers - 列舉
- FlatBuffers - 向量
- FlatBuffers - 結構體
- FlatBuffers - 聯合體
- FlatBuffers - 巢狀表格
- FlatBuffers - 預設值
- FlatBuffers - JSON 轉二進位制
- FlatBuffers - 二進位制轉 JSON
- FlatBuffers - 可變緩衝區
- FlatBuffers - 向後相容性
- FlatBuffers - 語言無關性
- FlatBuffers 有用資源
- FlatBuffers - 快速指南
- FlatBuffers - 有用資源
- FlatBuffers - 討論
FlatBuffers - 向後相容性
概述
FlatBuffers schema 具有向後相容性。這意味著,如果我們稍後更改、新增或刪除 FlatBuffers schema 的屬性,現有程式碼仍然可以工作。這在維護遺留程式碼庫時非常有用。考慮一個場景,其中劇院 Schema 只包含名稱和地址,如下所示:
theater.fbs
namespace com.tutorialspoint.theater;
table Theater {
name:string;
address:string;
}
root_type Theater;
如果我們為此 schema 生成程式碼,它將支援在 FlatBuffer 二進位制檔案中儲存名稱和地址。
現在隨著時間的推移,我們需要向 schema 新增一個手機號,那麼我們需要再次生成更新的程式碼。結果,我們也需要更新寫入器和讀取器程式碼。但在生產環境中,直接更改程式碼通常並不容易,並且進行此類更改可能會破壞整個系統。FlatBuffers 在這裡確保舊的讀取器程式碼仍然可以與新的基於 schema 生成的 FlatBuffers 二進位制檔案一起正常工作,無需任何更改。
從 fbs 檔案建立 Java 類
要使用 FlatBuffers,我們現在必須使用 **flatc** 二進位制檔案從此“.fbs”檔案建立所需的類。讓我們看看如何做到這一點:
flatc --java theater.fbs
這將在當前目錄的 **com > tutorialspoint > theater** 資料夾中建立一個 Theater.java 類。我們在應用程式中使用此類,類似於在 Flat Buffers - Schema 章節中所做的。
使用從 fbs 檔案建立的 Java 類
首先,讓我們建立一個 **寫入器** 來寫入 **劇院** 資訊:
TheaterWriter.java
package com.tutorialspoint.theater;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.google.flatbuffers.FlatBufferBuilder;
public class TheaterWriter {
public static void main(String[] args) throws FileNotFoundException, IOException {
// create a flat buffer builder
// it will be used to create Theater FlatBuffer
FlatBufferBuilder builder = new FlatBufferBuilder(1024);
int name = builder.createString("Silver Screener");
int address = builder.createString("212, Maple Street, LA, California");
// create theater FlatBuffers using startTheater() method
Theater.startTheater(builder);
// add the name and address to the Theater FlatBuffer
Theater.addName(builder, name);
Theater.addAddress(builder, address);
// mark end of data being entered in Greet FlatBuffer
int theater = Theater.endTheater(builder);
// finish the builder
builder.finish(theater);
// get the bytes to be stored
byte[] data = builder.sizedByteArray();
String filename = "theater_flatbuffers_output";
System.out.println("Saving theater to file: " + filename);
// write the builder content to the file named theater_flatbuffers_output
try(FileOutputStream output = new FileOutputStream(filename)){
output.write(data);
}
System.out.println("Saved theater with following data to disk: \n" + theater);
}
}
接下來,我們將有一個 **讀取器** 來讀取 **劇院** 資訊:
TheaterReader.java
package com.tutorialspoint.theater;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
public class TheaterReader {
public static void main(String[] args) throws FileNotFoundException, IOException {
String filename = "theater_flatbuffers_output";
System.out.println("Reading from file " + filename);
try(FileInputStream input = new FileInputStream(filename)) {
// get the serialized data
byte[] data = input.readAllBytes();
ByteBuffer buf = ByteBuffer.wrap(data);
// read the root object in serialized data
Theater theater = Theater.getRootAsTheater(buf);
// print theater values
System.out.println("Name: " + theater.name());
System.out.println("Address: " + theater.address());
}
}
}
編譯專案
現在我們已經設定了 **讀取器** 和 **寫入器**,讓我們編譯專案。
mvn clean install
序列化 Java 物件
現在,編譯後,讓我們首先執行 **寫入器**:
java -cp .\target\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter Saving theater to file: theater_flatbuffers_output Saved theater with following data to disk: 72
反序列化已序列化的物件
現在,讓我們執行 **讀取器** 從同一個檔案讀取:
java -cp .\target\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader Reading from file theater_flatbuffers_output Name: Silver Screener Address: 212, Maple Street, LA, California
向後相容性測試
現在讓我們向 schema 新增一個手機號,更新寫入器並執行讀取器(無需更新它)以檢查向後相容性。
theater.fbs
namespace com.tutorialspoint.theater;
table Theater {
name:string;
address:string;
mobile:int;
}
root_type Theater;
從 fbs 檔案建立 Java 類
使用 **flatc** 二進位制檔案從此“.fbs”檔案建立所需的類。
flatc --java theater.fbs
使用從 fbs 檔案建立的 Java 類
首先,讓我們建立一個 **寫入器** 來寫入 **劇院** 資訊:
TheaterWriter.java
package com.tutorialspoint.theater;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.google.flatbuffers.FlatBufferBuilder;
public class TheaterWriter {
public static void main(String[] args) throws FileNotFoundException, IOException {
// create a flat buffer builder
// it will be used to create Theater FlatBuffer
FlatBufferBuilder builder = new FlatBufferBuilder(1024);
int name = builder.createString("Silver Screener");
int address = builder.createString("212, Maple Street, LA, California");
// create theater FlatBuffers using startTheater() method
Theater.startTheater(builder);
// add the name, address and mobile to the Theater FlatBuffer
Theater.addName(builder, name);
Theater.addAddress(builder, address);
Theater.addMobile(builder, 12233345);
// mark end of data being entered in Greet FlatBuffer
int theater = Theater.endTheater(builder);
// finish the builder
builder.finish(theater);
// get the bytes to be stored
byte[] data = builder.sizedByteArray();
String filename = "theater_flatbuffers_output";
System.out.println("Saving theater to file: " + filename);
// write the builder content to the file named theater_flatbuffers_output
try(FileOutputStream output = new FileOutputStream(filename)){
output.write(data);
}
System.out.println("Saved theater with following data to disk: \n" + theater);
}
}
編譯專案
現在我們已經設定了 **寫入器**,讓我們編譯專案。
mvn clean install
序列化 Java 物件
現在,編譯後,讓我們首先執行 **寫入器**:
java -cp .\target\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter Saving theater to file: theater_flatbuffers_output Saved theater with following data to disk: 76
使用舊讀取器反序列化已序列化的物件
現在,讓我們執行 **讀取器** 從同一個檔案讀取:
java -cp .\target\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader Reading from file theater_flatbuffers_output Name: Silver Screener Address: 212, Maple Street, LA, California
更新讀取器並再次反序列化
TheaterReader.java
package com.tutorialspoint.theater;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
public class TheaterReader {
public static void main(String[] args) throws FileNotFoundException, IOException {
String filename = "theater_flatbuffers_output";
System.out.println("Reading from file " + filename);
try(FileInputStream input = new FileInputStream(filename)) {
// get the serialized data
byte[] data = input.readAllBytes();
ByteBuffer buf = ByteBuffer.wrap(data);
// read the root object in serialized data
Theater theater = Theater.getRootAsTheater(buf);
// print theater values
System.out.println("Name: " + theater.name());
System.out.println("Address: " + theater.address());
System.out.println("Mobile: " + theater.mobile());
}
}
}
編譯專案
現在我們已經設定了 **讀取器** 和 **寫入器**,讓我們編譯專案。
mvn clean install
反序列化已序列化的物件
現在,讓我們執行 **讀取器** 從同一個檔案讀取:
java -cp .\target\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader Reading from file theater_flatbuffers_output Name: Silver Screener Address: 212, Maple Street, LA, California Mobile: 12233345