Flat Buffers - union

概述

union 数据类型是 Flat Buffers 的复合数据类型之一。它用于创建灵活的数据结构,可以采用任何所需类型。

继续使用 Flat Buffers - String 章节中的 theater 示例,以下是我们需要用来指示 FlatBuffers 创建 union 的语法 −

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 的 union 属性。在 Theater 表中,我们定义了 union 类型的 people,这意味着我们可以将任何 Employee 或 Viewer 存储在 people 变量中。

从 fbs 文件创建 Java 类

要使用 FlatBuffers,我们现在必须使用 flatc 二进制文件从此".fbs"文件创建所需的类。让我们看看如何做到这一点 −

flatc --java theater.fbs

这将在当前目录的 com > tutorialspoint > theater 文件夹中创建 TheaterPeopleEmployeeViewer 类。我们在应用程序中使用此类的方式与 Flat Buffers - Schema 一章中的方式类似。

使用从 fbs 文件创建的 Java 类

创建和编写 Union

为了创建 Union,我们首先需要准备所需类型的偏移量,例如 Viewer,然后我们可以将查看器及其类型添加到Flat Buffers。

// 为 Viewer 创建偏移量
int viewerName = builder.createString("Mery");
int viewerAddress = builder.createString("Avenue 4");
int viewer = Viewer.createViewer(builder, viewerName, viewerAddress);

//添加 union 类型
Theater.addPeopleType(builder, People.Viewer);
// 向 Theater FlatBuffer 添加详细信息
Theater.addPeople(builder, viewer);

以下示例代码展示了创建 Union 的过程。

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 {
    // 创建一个Flat Buffers构建器
    // 它将用于创建 Theater FlatBuffer
    FlatBufferBuilder builder = new FlatBufferBuilder(1024);
    
    // 为 Viewer 创建偏移量
    int viewerName = builder.createString("Mery");
    int viewerAddress = builder.createString("Avenue 4");
    int viewer = Viewer.createViewer(builder, viewerName, viewerAddress);
    
    // 为向量创建偏移量
    //int people = Theater.createPeople
    
    // 使用 startTheater() 方法创建 theater FlatBuffers
    Theater.startTheater(builder);
    
    //添加联合类型
    Theater.addPeopleType(builder, People.Viewer);
    
    // 向 Theater FlatBuffer 添加详细信息
    Theater.addPeople(builder, viewer);
    
    // 标记在 Greet FlatBuffer 中输入的数据的结束
    int theater = Theater.endTheater(builder);
    
    // 完成构建器
    builder.finish(theater);
    
    // 获取要存储的字节
    byte[] data = builder.sizedByteArray();
    
    String filename = "theater_flatbuffers_output";
    System.out.println("将Theater 保存到文件:" + filename);
    // 将构建器内容写入名为 theater_flatbuffers_output 的文件
    try(FileOutputStream output = new FileOutputStream(filename)){
         output.write(data);
      }
      System.out.println("Saved theater with following data to disk: 
" + theater);
   }
}	

读取 Union

为了读取 Union,我们可以检查 union 对象的类型,然后相应地检索值。

// 获取保存的 union 类型
int unionType = theater.peopleType();

// 如果 union 是 Viewer 类型
if(unionType == People.Viewer) {
   Viewer viewer = (Viewer)theater.people(new Viewer());
   System.out.println("Name: " + viewer.name());
   System.out.println("Address: " + viewer.address());       	 
} 
// 如果 union 是 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()); 
}

以下示例代码展示了读取 Union 的过程。

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)) {
        // 获取序列化数据
        byte[] data = input.readAllBytes();
        ByteBuffer buf = ByteBuffer.wrap(data);
        // 读取序列化数据中的根对象
        Theater theater = Theater.getRootAsTheater(buf);
        // 打印 theater 值
        System.out.println("People: ");
        // 获取 union 的类型
        int unionType = theater.peopleType();
        // 如果 union 是 Viewer 类型
        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()); 
         }       
      }
   }
}

编译项目

现在我们已经设置了readerwriter,让我们编译项目。

mvn clean install

序列化 Java 对象

现在,编译后,让我们先执行writer

> java -cp .	arget\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 .	arget\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader

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

因此,如我们所见,我们可以通过将二进制数据反序列化为 Theater 对象来读取序列化的 struct。在下一章 Flat Buffers - Nested Tables 中,我们将研究 Nested Tables,一种复合类型。