FlatBuffers - 聯合體



概述

聯合體資料型別是 FlatBuffers 的複合資料型別之一。它用於建立靈活的資料結構,可以採用任何所需型別。

繼續我們從Flat Buffers - 字串章節的劇院示例,以下是我們需要用來指示 FlatBuffers 將要建立一個聯合體的語法:

theater.fbs

namespace com.tutorialspoint.theater;

union People { Employee, Viewer }

table Theater {
   people: People;
}

table Employee {
   name:string;
   address:string;
   id: int;
}

table Viewer {
   name: string;
   address: string;
}
root_type Theater;

現在我們的包含定義為兩個表 Employee 和 Viewer 的 People 的聯合體屬性。在 Theater 表中,我們定義了聯合型別的 people,這意味著我們可以將 Employee 或 Viewer 中的任何一個儲存在 people 變數中。

從 fbs 檔案建立 Java 類

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

flatc  --java theater.fbs

這將在當前目錄中的com > tutorialspoint > theater資料夾中建立TheaterPeopleEmployeeViewer類。我們在應用程式中使用此類,與Flat Buffers - 模式章節中所做的一樣。

使用從 fbs 檔案建立的 Java 類

建立和寫入聯合體

為了建立一個聯合體,我們需要首先準備所需型別的偏移量,例如 Viewer,然後我們可以將 viewer 及其型別新增到 flat buffer。

// create offset for Viewer
int viewerName = builder.createString("Mery");
int viewerAddress = builder.createString("Avenue 4");
int viewer = Viewer.createViewer(builder, viewerName, viewerAddress);

//add union tyoe
Theater.addPeopleType(builder, People.Viewer);
// add details to the Theater FlatBuffer
Theater.addPeople(builder, viewer);

下面的示例程式碼顯示了建立聯合體的過程。

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);
      
      // create offset for Viewer
      int viewerName = builder.createString("Mery");
      int viewerAddress = builder.createString("Avenue 4");
      int viewer = Viewer.createViewer(builder, viewerName, viewerAddress);
      
      // create offset for vector
      //int people = Theater.createPeople
      
      // create theater FlatBuffers using startTheater() method
      Theater.startTheater(builder);

      //add union type
      Theater.addPeopleType(builder, People.Viewer);

      // add details to the Theater FlatBuffer
      Theater.addPeople(builder, viewer);
      
      // 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);
   }
}	

讀取聯合體

為了讀取聯合體,我們可以檢查聯合體物件的型別,然後相應地檢索值。

// get the saved union type
int unionType = theater.peopleType();

// if union is of type Viewer
if(unionType == People.Viewer) {
   Viewer viewer = (Viewer)theater.people(new Viewer());
   System.out.println("Name: " + viewer.name());
   System.out.println("Address: " + viewer.address());       	 
} 
// if union is of type Employee
else if(unionType == People.Employee) {
   Employee employee = (Employee)theater.people(new Employee());
   System.out.println("Name: " + employee.name());
   System.out.println("Address: " + employee.address());  
   System.out.println("Id: " + employee.id()); 
}

下面的示例程式碼顯示了讀取聯合體的過程。

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("People: ");
         // get the type of union
		 int unionType = theater.peopleType();
         // if union is of Viewer type  
         if(unionType == People.Viewer) {
            Viewer viewer = (Viewer)theater.people(new Viewer());
            System.out.println("Name: " + viewer.name());
            System.out.println("Address: " + viewer.address());       	 
         } else if(unionType == People.Employee) {
            Employee employee = (Employee)theater.people(new Employee());
            System.out.println("Name: " + employee.name());
            System.out.println("Address: " + employee.address());  
            System.out.println("Id: " + employee.id()); 
         }       
      }
   }
}

編譯專案

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

mvn clean install

序列化 Java 物件

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

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

Saving theater information to file: theater_flatbuffers_output
Saved theater information with following data to disk:
60

反序列化序列化的物件

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

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

Reading from file theater_flatbuffers_output
People:
Name: Mery
Address: Avenue 4

因此,正如我們所看到的,我們能夠透過將二進位制資料反序列化為Theater物件來讀取序列化的結構體。在下一章Flat Buffers - 巢狀表中,我們將瞭解複合型別的巢狀表。

廣告
© . All rights reserved.