Flat Buffers - 向后兼容性

概述

FlatBuffers 模式向后兼容。这意味着,如果我们稍后将属性添加到 flatbuffers 模式,现有代码仍然可以工作。这在维护旧代码库时非常有用。考虑一个 Theater Schema 仅包含名称和地址的场景,如下所示:

theater.fbs

namespace com.tutorialspoint.theater;

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

如果我们为该模式生成代码,它将支持将名称和地址存储在 flatbuffer bin 文件中。

现在随着时间的推移,我们需要向模式添加手机号码,然后我们需要再次生成更新的代码。因此,我们还需要更新写入器和读取器代码。但在生产中,通常直接更改代码并不容易,并且进行这样的更改可能会破坏整个系统。这里的Flat Buffers可确保旧的读取器代码仍可与基于新模式生成的 flatbuffers bin 文件正常工作,而无需进行任何更改。

从 fbs 文件创建 Java 类

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

flatc --java theater.fbs

这将在 com > 中创建一个 Theater.java 类tutorialspoint > theater 文件夹。我们在应用程序中使用此类的方式与 Flat Buffers - Schema 一章中的方式类似。

使用从 fbs 文件创建的 Java 类

首先,让我们创建一个 writer 来写入 theater 信息 −

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);

      int name = builder.createString("Silver Screener");
      int address = builder.createString("212, Maple Street, LA, California");

      // 使用 startTheater() 方法创建Theater  FlatBuffers
      Theater.startTheater(builder);
      // 将名称和地址添加到 Theater FlatBuffer
      Theater.addName(builder, name);
      Theater.addAddress(builder, address);

      // 标记在 Gret FlatBuffer 中输入数据的结束
      int theater = Theater.endTheater(builder);

      // 完成构建器
      builder.finish(theater);

      // 获取要存储的字节
      byte[] data = builder.sizedByteArray();

      String filename = "theater_flatbuffers_output";
      System.out.println("Saving theater to file: " + filename);
      // 将构建器内容写入名为 theater_flatbuffers_output 的文件
      try(FileOutputStream output = new FileOutputStream(filename)){
         output.write(data);
      }
      System.out.println("Saved theater with following data to disk: 
" + theater);
   }
}	

接下来,我们将有一个阅读器来读取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)) {
         // 获取序列化数据
         byte[] data = input.readAllBytes();
         ByteBuffer buf = ByteBuffer.wrap(data);
         // 读取序列化数据中的根对象
         Theater theater = Theater.getRootAsTheater(buf);
         // 打印 theater 剧场值
         
         System.out.println("Name: " + theater.name());
         System.out.println("Address: " + theater.address());
      }
   }
}

编译项目

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

mvn clean install

序列化 Java 对象

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

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

Reading from file theater_flatbuffers_output
Name: Silver Screener
Address: 212, Maple Street, LA, California

向后兼容性测试

现在让我们向架构添加一个手机号码,更新写入器并在不更新它的情况下运行读取器以检查向后兼容性。

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 类

首先让我们创建一个 writer 来写入 theater 信息−

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);

      int name = builder.createString("Silver Screener");
      int address = builder.createString("212, Maple Street, LA, California");

      // 使用 startTheater() 方法创建Theater  FlatBuffers
      Theater.startTheater(builder);
      // 将姓名、地址和手机添加到 Theater FlatBuffer
      Theater.addName(builder, name);
      Theater.addAddress(builder, address);
      Theater.addMobile(builder, 12233345);

      // 标记在 Gret FlatBuffer 中输入数据的结束
      int theater = Theater.endTheater(builder);

      // 完成构建器
      builder.finish(theater);

      // 获取要存储的字节
      byte[] data = builder.sizedByteArray();

      String filename = "theater_flatbuffers_output";
      System.out.println("Saving theater to file: " + filename);
      // 将构建器内容写入名为 theater_flatbuffers_output 的文件
      try(FileOutputStream output = new FileOutputStream(filename)){
         output.write(data);
      }
      System.out.println("Saved theater with following data to disk: 
" + theater);
   }
}	

编译项目

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

mvn clean install

序列化 Java 对象

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

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

Reading from file theater_flatbuffers_output
Name: Silver Screener
Address: 212, Maple Street, LA, California

更新 Reader 并再次反序列化

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("Name: " + theater.name());
         System.out.println("Address: " + theater.address());
         System.out.println("Mobile: " + theater.mobile());
      }
   }
}

编译项目

现在我们已经设置了读取器写入器,让我们编译项目。

mvn clean install

反序列化序列化对象

现在,让我们执行读取器来读取同一个文件−

java -cp .	arget\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