协议_flatbuffer_protobuf_thrift

flatBuffer

过程

1.定义 .fbs  元数据文件 schema 文件
2.用 `flatc` 工具编译 schema 文件
3. 使用生成的代码生成二进制数据流  使用生成的代码创建和操作数据对象。将数据转换为二进制格式,便于高效的传输和存储。

fbs

 1.命名空间(namespaces)
 2.类型和默认值 
     类型和默认值
       FlatBuffers 支持多种数据类型,比如整数、浮点数、字符串等。每个字段可以有一个默认值
	   支持的标量类型:byte short int  float  double
	    enum Enums(枚举):是用来表示一组固定值的类型
		union Unions(联合):类似于枚举,但它不仅仅是数值,而是可以包含不同类型的对象
    Table 是 FlatBuffers 中最常用的一种数据结构。表格里的字段是可选的
        ● 添加字段:如果你想添加新的字段,必须放在表格的末尾
	    ● 删除字段:你不能直接删除字段,但可以标记它为 “废弃”(deprecated)
    Struct 则不同,它的字段是固定的,所有字段必须有值,不能跳过
    root_type 至多1个 root_type 关键字用于指定根类型,而且只能指定一个根类型。
     每个FlatBuffers schema 文件只能有一个根类型
  3.flatBuffers 支持原生数组类型  
     提供更灵活的数据结构选项(table 和 struct)
     原生支持数组和 Union 类型,让数据处理更加高效。		

示例:
    namespace org.apache.arrow.flatbuf;
    enum MetadataVersion:short { V1, V2, V3, V4, V5,}
    enum Feature : long { UNUSED = 0,DICTIONARY_REPLACEMENT = 1, COMPRESSED_BODY = 2}
    table Null {}
    table Struct_ {}
    union Type { Null,Int, FloatingPoint, Binary,LargeListView,}
    struct Buffer { offset: long; length: long;}
    table Schema { endianness: Endianness=Little; fields: [Field];
       custom_metadata: [ KeyValue ]; features : [ Feature ];}
    root_type Schema;

    
format/Message.fbs		 
  include "Schema.fbs";
  namespace org.apache.arrow.flatbuf;
  struct FieldNode { length: long; null_count: long;}
  enum CompressionType:byte { LZ4_FRAME, ZSTD}
  union MessageHeader { Schema, DictionaryBatch, RecordBatch, Tensor, SparseTensor}
  table Message {version: org.apache.arrow.flatbuf.MetadataVersion;
         header: MessageHeader; bodyLength: long; custom_metadata: [ KeyValue ];}
  root_type Message;

Protocol Buffers

● 对象定义的名称:
       ● 在 Protocol Buffers 中,定义对象用的是 `message` 关键字。
       ● 在 FlatBuffers 中,定义对象用的是 `table` 关键字。
● 字段 ID 的编号:
    ● 在 Protocol Buffers 中,字段 ID 从 1 开始编号。
    ● 在 FlatBuffers 中,字段 ID 从 0 开始编号	  

thrift

 Thrift作为Facebook开源的RPC框架
   完整的数据类型系统,
     包括基本类型(如bool、byte、i16、i32、i64、double、string)、容器类型(list、set、map)以及结构体类型

    thrift的向后兼容性(Version)借助属性标识(数字编号id + 属性类型type)来实现
	新增的字段属性, 采用id递增的方式标识并以optional修饰来添加
	Thrift实际上是实现了C/S模式,通过代码生成工具将接口定义文件生成服务器端和客户端代码,从而实现服务端和客户端跨语言的支持
	Thrift 编译器(thrift)用于将 IDL 文件编译生成目标语言的客户端和服务器代码

示例:

  src/main/thrift/parquet.thrift
    namespace cpp parquet
    namespace java org.apache.parquet.format
    enum Type {	BOOLEAN = 0;INT32 = 1;INT64 = 2; INT96 = 3;FLOAT = 4;  DOUBLE = 5; BYTE_ARRAY = 6; FIXED_LEN_BYTE_ARRAY = 7;}
    struct SizeStatistics {
         1: optional i64 unencoded_byte_array_data_bytes;
    	 2: optional list<i64> repetition_level_histogram;
    	 3: optional list<i64> definition_level_histogram;
    	}
    struct BoundingBox { 1: required double xmin; 2: required double xmax;
        3: required double ymin; 4: required double ymax;
        5: optional double zmin; 6: optional double zmax;
        7: optional double mmin; 8: optional double mmax;
        }	
    union TimeUnit {1: MilliSeconds MILLIS   2: MicroSeconds MICROS 3: NanoSeconds NANOS}  

protobuf

protoc 版本  原理,包括其 数据结构、编码规则以及使用方式
协议文件 版本
 message 消息中承载的数据分别对应于每一个字段都有一个名字和一种类型
 optional
 repeated :在格式正确的消息中,此字段类型可以重复零次或多次。系统会保留重复值的顺序
 字段规则 字段类型 字段名称=字段编号[default=0];
 Protobuf编码 是通过成员的唯一编号来绑定对应的数据

参考

 https://github.com/apache/arrow/blob/main/format/Schema.fbs	  
 https://github.com/apache/parquet-format/blob/master/src/main/thrift/parquet.thrift  
posted @ 2025-05-30 17:31  辰令  阅读(56)  评论(0)    收藏  举报