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
广告
© . All rights reserved.