数据类型
Schema 数据类型
| DataType | 描述 |
|---|---|
DataType::Boolean() | Boolean 值 |
DataType::TinyInt() | 8 位有符号整数 |
DataType::SmallInt() | 16 位有符号整数 |
DataType::Int() | 32 位有符号整数 |
DataType::BigInt() | 64 位有符号整数 |
DataType::Float() | 32 位浮点数 |
DataType::Double() | 64 位浮点数 |
DataType::String() | UTF-8 字符串 |
DataType::Bytes() | 二进制数据 |
DataType::Date() | 日期(自 epoch 起的天数) |
DataType::Time() | 时间(自午夜起的毫秒数) |
DataType::Timestamp() | 不带时区的 timestamp(默认 precision 6,微秒) |
DataType::TimestampLtz() | 带时区的 timestamp(默认 precision 6,微秒) |
DataType::Decimal(p, s) | 带 precision 和 scale 的 decimal |
DataType::Array(element) | 给定元素类型的 array(支持嵌套) |
DataType::Map(key, value) | key/value 类型的 map(两者均可为复杂类型) |
DataType::Row({{name, type}, ...}) | 具名字段的 row(struct)(类型可为复杂类型) |
MAP / ROW 列(以及嵌套它们的 array)可以组合到任意深度 —— 参见 声明 MAP 和 ROW 列。
可空性
所有 DataType 默认可为 null。使用 .NotNull() 创建一个 NOT NULL 类型:
auto schema = fluss::Schema::NewBuilder()
.AddColumn("id", fluss::DataType::Int().NotNull())
.AddColumn("name", fluss::DataType::String()) // nullable by default
.Build();无论 DataType 如何设置,主键列都会被自动强制为 NOT NULL。
对于嵌套类型,可空性在每一层 array 以及叶子元素上都会保留:
auto schema = fluss::Schema::NewBuilder()
.AddColumn("tags", fluss::DataType::Array(fluss::DataType::String().NotNull()))
.AddColumn("ids", fluss::DataType::Array(fluss::DataType::Int()).NotNull())
.AddColumn("nested", fluss::DataType::Array(
fluss::DataType::Array(fluss::DataType::Int()).NotNull()))
.Build();
// "tags": ARRAY<STRING NOT NULL> (outer nullable, elements NOT NULL)
// "ids": ARRAY<INT> NOT NULL (outer NOT NULL, elements nullable)
// "nested": ARRAY<ARRAY<INT> NOT NULL> (outer nullable, inner array NOT NULL)你可以在运行时查询可空性:
auto info = table.GetTableInfo();
bool is_nullable = info.schema.columns[0].data_type.nullable();写入一个 Arrow batch
你的 batch 必须包含该表的列,使用该表的名称,并按该表的顺序排列。ROW 列的字段也同样如此。任何不符合的内容都会被拒绝,并在错误信息中标明对应的列。
列类型也必须与该表一致,因为 AppendArrowBatch 会按原样写入你的 buffer,而 reader 会用该表的 schema 来解码 —— 例如把一个 timestamp[ns] 列写入 TIMESTAMP(3) 的列,读回时会被当作毫秒。仅在存储相同值的方式上有所区别的类型会为你转换:large_utf8、large_binary、large_list、view 类型以及 dictionary 编码。任何会改变值的差异,例如不同的 TIMESTAMP 单位或 DECIMAL scale,都会被拒绝。
GetArrowSchema 返回该表期望的 schema。基于它来构建即可完全避免上述转换:
std::shared_ptr<arrow::Schema> expected;
auto result = table.GetArrowSchema(expected);你的 schema 上的 nullable 标记不必匹配 —— 实际的 null 会单独针对表的 NOT NULL 列进行检查。
声明 MAP 和 ROW 列
MAP<K,V> 和 ROW<...> 列通过 DataType::Map 和 DataType::Row 工厂来声明,像其他任何类型一样组合 —— 可嵌套到任意深度:
auto schema = fluss::Schema::NewBuilder()
.AddColumn("id", fluss::DataType::Int())
.AddColumn("attrs", fluss::DataType::Map(fluss::DataType::String(),
fluss::DataType::Int()))
.AddColumn("profile", fluss::DataType::Row({
{"seq", fluss::DataType::Int()},
{"label", fluss::DataType::String()},
}))
// arbitrarily nested, e.g. array<row<seq: int, attrs: map<string,int>>>:
.AddColumn("events", fluss::DataType::Array(fluss::DataType::Row({
{"seq", fluss::DataType::Int()},
{"attrs", fluss::DataType::Map(fluss::DataType::String(), fluss::DataType::Int())},
})))
.SetPrimaryKeys({"id"})
.Build();Arrow escape hatch
如果你已经有 arrow::Schema,可以用 fluss::Schema::FromArrow(arrow_schema, /*primary_keys=*/{"id"}) 直接传入。二者是等价的 —— 上面这些原生工厂在内部会降级为相同的 Arrow 类型,从而无需把 Arrow 引入你的代码。
GenericRow Setter
SetInt32 用于 TinyInt、SmallInt 和 Int 列。对于 TinyInt 和 SmallInt,其值会在写入时进行校验 —— 若超出该列的范围(例如 TinyInt 为 [-128, 127],SmallInt 为 [-32768, 32767])则返回错误。
fluss::GenericRow row;
row.SetNull(0);
row.SetBool(1, true);
row.SetInt32(2, 42);
row.SetInt64(3, 1234567890L);
row.SetFloat32(4, 3.14f);
row.SetFloat64(5, 2.71828);
row.SetString(6, "hello");
row.SetBytes(7, {0x01, 0x02, 0x03});Array 列
Array 值使用 ArrayWriter 逐个元素构建,然后通过 SetArray 挂到 row 上:
fluss::ArrayWriter aw(3, fluss::DataType::Int());
aw.SetInt32(0, 10);
aw.SetInt32(1, 20);
aw.SetNull(2);
row.SetArray(8, std::move(aw));对于嵌套 array(例如 ARRAY<ARRAY<INT>>),请先构建内层 array:
fluss::ArrayWriter inner(2, fluss::DataType::Int());
inner.SetInt32(0, 1);
inner.SetInt32(1, 2);
fluss::ArrayWriter outer(1, fluss::DataType::Array(fluss::DataType::Int()));
outer.SetArray(0, std::move(inner));
row.SetArray(9, std::move(outer));Map 列
Map 值使用 MapWriter 构建:逐个暂存每个条目的 key 和 value,然后调用 Commit()。key 不能为 null;未设置的 value 默认为 null。
fluss::MapWriter mw(2, fluss::DataType::String(), fluss::DataType::Int());
mw.SetKeyString("a"); mw.SetValueInt32(1); mw.Commit();
mw.SetKeyString("b"); mw.SetValueNull(); mw.Commit();
row.SetMap(10, std::move(mw));当 key 或 value 自身就是 ROW / MAP / ARRAY 时,请以原生方式传入该复合类型,并使用复合 setter(SetValueRow / SetValueMap / SetValueArray)来暂存值:
fluss::MapWriter mr(1, fluss::DataType::String(),
fluss::DataType::Row({{"seq", fluss::DataType::Int()}}));
mr.SetKeyString("k");
fluss::GenericRow v(1); v.SetInt32(0, 7);
mr.SetValueRow(std::move(v));
mr.Commit();
row.SetMap(11, std::move(mr));Row 列
ROW 值是一个嵌套的、无 schema 的 GenericRow,通过 SetRow 挂载:
fluss::GenericRow nested(2);
nested.SetInt32(0, 7);
nested.SetString(1, "seven");
row.SetRow(12, std::move(nested));基于名称的 Setter
使用 table.NewRow() 时,你可以按列名设置字段。setter 会根据 schema 自动路由到正确的类型:
auto row = table.NewRow();
row.Set("user_id", 1);
row.Set("name", "Alice");
row.Set("score", 95.5f);
row.Set("balance", "1234.56"); // decimal as string
row.Set("birth_date", fluss::Date::FromYMD(1990, 3, 15));
row.Set("login_time", fluss::Time::FromHMS(9, 30, 0));
row.Set("created_at", fluss::Timestamp::FromMillis(1700000000000));
row.Set("nickname", nullptr); // set to null读取字段值
字段值通过 RowView(来自 scan 结果)和 LookupResult(来自 lookup)读取,而不是通过 GenericRow。二者提供相同的 getter 接口,并对 string 和 bytes 数据提供零拷贝访问。
ScanRecord 是值类型 —— 它可以自由拷贝、存储,并通过引用计数在多次 Poll() 调用之间累积。
string_view Lifetime
GetString() 返回的 std::string_view 借用自底层数据。只要任何引用同一 poll 结果的 ScanRecord 仍然存活,该 string_view 就有效。若你需要在所有记录都销毁后仍使用该值,请拷贝到 std::string。
// ScanRecord is a value type — safe to store and accumulate:
std::vector<fluss::ScanRecord> all_records;
fluss::ScanRecords records;
scanner.Poll(5000, records);
for (const auto& rec : records) {
all_records.push_back(rec); // safe! ref-counted
auto name = rec.row.GetString(0); // zero-copy string_view
auto owned = std::string(rec.row.GetString(0)); // explicit copy when needed
}
// DON'T — string_view dangles after all records referencing the data are destroyed:
std::string_view dangling;
{
fluss::ScanRecords records;
scanner.Poll(5000, records);
dangling = records[0].row.GetString(0);
}
// dangling is undefined behavior here — no ScanRecord keeps the data alive!来自 Scan 结果(RowView)
for (const auto& rec : records) {
auto name = rec.row.GetString(1); // zero-copy string_view
float score = rec.row.GetFloat32(3);
auto balance = rec.row.GetDecimalString(4); // std::string (already owned)
fluss::Date date = rec.row.GetDate(5);
fluss::Time time = rec.row.GetTime(6);
fluss::Timestamp ts = rec.row.GetTimestamp(7);
}来自 Lookup 结果(LookupResult)
fluss::LookupResult result;
lookuper.Lookup(pk_row, result);
if (result.Found()) {
auto name = result.GetString(1); // zero-copy string_view
int64_t age = result.GetInt64(2);
}读取复杂列(ARRAY / MAP / ROW)
复杂列通过由 GetValue(idx) / GetValue(name) 返回的单个递归 Value 句柄读取。用 At / KeyAt / ValueAt / Field 导航结构(每个都返回一个子 Value);用 Get*() 方法读取叶子值(句柄已经指向某一个值,因此无需索引):
// ARRAY<INT>
auto arr = rec.row.GetValue("ids");
for (size_t i = 0; i < arr.Size(); i++) {
if (!arr.At(i).IsNull()) {
int32_t v = arr.At(i).GetInt32();
}
}
// MAP<STRING, INT> — entries addressed by position
auto m = rec.row.GetValue("attrs");
for (size_t i = 0; i < m.Size(); i++) {
auto key = m.KeyAt(i).GetString();
if (!m.ValueAt(i).IsNull()) {
int32_t v = m.ValueAt(i).GetInt32();
}
}
// ROW<seq: INT, label: STRING> — fields by index or name
auto r = rec.row.GetValue("profile");
int32_t seq = r.Field(0).GetInt32();
auto label = r.Field("label").GetString();
// Nesting is just chained navigation, with the same Get* leaf reads:
auto rows = rec.row.GetValue("arr_of_rows"); // ARRAY<ROW<...>>
int32_t first_seq = rows.At(0).Field("seq").GetInt32();GetValue 在 RowView(scan)、LookupResult 和 PrefixRowView 上用法相同。对 null 或类型错误的值进行带类型的 Get*() 会抛出异常。
TypeId 枚举
TinyInt 和 SmallInt 值在读取时会被拓宽为 int32_t。
| TypeId | C++ 类型 | Getter |
|---|---|---|
Boolean | bool | GetBool(idx) |
TinyInt | int32_t | GetInt32(idx) |
SmallInt | int32_t | GetInt32(idx) |
Int | int32_t | GetInt32(idx) |
BigInt | int64_t | GetInt64(idx) |
Float | float | GetFloat32(idx) |
Double | double | GetFloat64(idx) |
String | std::string_view | GetString(idx) |
Bytes | std::pair<const uint8_t*, size_t> | GetBytes(idx) |
Date | Date | GetDate(idx) |
Time | Time | GetTime(idx) |
Timestamp | Timestamp | GetTimestamp(idx) |
TimestampLtz | Timestamp | GetTimestamp(idx) |
Decimal | std::string | GetDecimalString(idx) |
Array | Value | GetValue(idx) |
Map | Value | GetValue(idx) |
Row | Value | GetValue(idx) |
类型检查
if (rec.row.GetType(0) == fluss::TypeId::Int) {
int32_t value = rec.row.GetInt32(0);
}
if (rec.row.IsNull(1)) {
// field is null
}
if (rec.row.IsDecimal(2)) {
std::string decimal_str = rec.row.GetDecimalString(2);
}常量
constexpr int64_t fluss::EARLIEST_OFFSET = -2; // Start from earliest要从最新 offset 开始读取,请在订阅前通过 ListOffsets 解析当前 offset:
std::unordered_map<int32_t, int64_t> offsets;
admin.ListOffsets(table_path, {0}, fluss::OffsetSpec::Latest(), offsets);
scanner.Subscribe(0, offsets[0]);