行格式
本页介绍用于高性能、缓存友好的数据访问的行格式序列化。
概述
Apache Fory™ 行格式是一种针对以下场景优化的二进制格式:
- 随机访问:无需反序列化整个对象即可读取任意字段
- 零拷贝:无数据复制的直接内存访问
- 缓存友好:连续内存布局提高 CPU 缓存效率
- 列式转换:易于转换为 Apache Arrow 格式
- 部分序列化:只序列化需要的字段
何时使用行格式
| 使用场景 | 行格式 | 对象图 |
|---|---|---|
| 分析/OLAP | ✅ | ❌ |
| 随机字段访问 | ✅ | ❌ |
| 完整对象序列化 | ❌ | ✅ |
| 复杂对象图 | ❌ | ✅ |
| 引用跟踪 | ❌ | ✅ |
| 跨语言(简单类型) | ✅ | ✅ |
快速开始
#include "fory/encoder/row_encoder.h"
#include "fory/row/writer.h"
using namespace fory::row;
using namespace fory::row::encoder;
// 定义结构体
struct Person {
int32_t id;
std::string name;
float score;
};
// 注册字段元数据(行编码必需)
FORY_FIELD_INFO(Person, id, name, score);
int main() {
// 创建编码器
RowEncoder<Person> encoder;
// 编码一个 person
Person person{1, "Alice", 95.5f};
encoder.Encode(person);
// 获取编码后的行
auto row = encoder.GetWriter().ToRow();
// 随机访问字段
int32_t id = row->GetInt32(0);
std::string name = row->GetString(1);
float score = row->GetFloat(2);
assert(id == 1);
assert(name == "Alice");
assert(score == 95.5f);
return 0;
}