API 参考
Fluss C++ 客户端的完整 API 参考。
Result
| 字段 / 方法 | 类型 | 描述 |
|---|---|---|
error_code | int32_t | 0 表示成功,非 0 表示错误 |
error_message | std::string | 人类可读的错误描述 |
Ok() | bool | 操作成功(error_code == 0)时返回 true |
Configuration
| 字段 | 类型 | 默认值 | 描述 |
|---|---|---|---|
bootstrap_servers | std::string | "127.0.0.1:9123" | Coordinator server 地址 |
writer_request_max_size | int32_t | 10485760 (10 MB) | 请求的最大字节数 |
writer_acks | std::string | "all" | 确认设置("all"、"0"、"1" 或 "-1") |
writer_retries | int32_t | INT32_MAX | 失败时的重试次数 |
writer_batch_size | int32_t | 2097152 (2 MB) | 写入批次的字节大小。开启动态调整时为上限;关闭时为固定批次大小 |
writer_dynamic_batch_size_enabled | bool | true | 启用按表动态调整批次大小:填充超过 80% 时目标增大 10%,低于 50% 时缩小 5% |
writer_dynamic_batch_size_min | int32_t | 262144 (256 KB) | 动态批次大小估算器的下限(禁用时忽略) |
writer_batch_timeout_ms | int64_t | 100 | 等待写入批次填满后再发送的最长时间(毫秒) |
writer_bucket_no_key_assigner | std::string | "sticky" | 无 bucket key 的表的 bucket 分配策略:"sticky" 或 "round_robin" |
writer_dynamic_create_partition_enabled | bool | true | 写入时自动创建尚不存在的分区。为 false 时,此类写入以 PARTITION_NOT_EXISTS 失败 |
scanner_remote_log_prefetch_num | size_t | 4 | 预取的远程 log segment 数量 |
remote_file_download_thread_num | size_t | 3 | 远程日志下载的线程数 |
scanner_remote_log_read_concurrency | size_t | 4 | 单个远程日志文件内的流式读取并发度 |
scanner_io_tmp_dir | std::string | "" | 远程日志下载的父目录;为空时使用操作系统临时目录加 /fluss |
scanner_log_max_poll_records | size_t | 500 | 单次 Poll() 返回的最大记录数 |
scanner_log_fetch_max_bytes | int32_t | 16777216 (16 MB) | LogScanner 单次 fetch 响应的最大字节数 |
scanner_log_fetch_min_bytes | int32_t | 1 | 服务端返回 fetch 响应前必须累积的最小字节数 |
scanner_log_fetch_wait_max_time_ms | int32_t | 500 | 服务端为满足最小字节数可等待的最长时间(毫秒) |
scanner_log_fetch_max_bytes_for_bucket | int32_t | 1048576 (1 MB) | LogScanner 每个 bucket 单次 fetch 响应的最大字节数 |
scanner_log_check_crc | bool | true | 检查所拉取日志批次的 CRC,损坏的批次会导致扫描失败。服务端投影(projected)的批次不检查 |
lookup_queue_size | size_t | 25600 | 发送队列容量;队列满时新的 lookup 会等待。每个 bucket 在等待 leader 期间也最多容纳这么多个 lookup;超出该 bucket 的 lookup 会立即因缓冲区耗尽错误而失败 |
lookup_max_retries | int32_t | INT32_MAX | lookup 因可重试错误而重发的最大次数。等待 bucket 的 leader 不算重试,因此这不是 lookup 超时 |
lookup_retry_backoff_ms | uint64_t | 100 | 因可重试错误而失败的 lookup 重发前的初始延迟(毫秒),该 lookup 每次重试都会翻倍;也是无已知 leader 的 bucket 再次刷新前的延迟,该 bucket 每次刷新都会翻倍并由其 lookup 共享。±20% 抖动;0 表示立即重发和刷新 |
lookup_retry_max_backoff_ms | uint64_t | 1000 | lookup 重试延迟与无已知 leader 的 bucket 刷新延迟的上限(毫秒),在抖动之前生效;必须 >= lookup_retry_backoff_ms |
connect_timeout_ms | uint64_t | 15000 | 连接建立超时(毫秒),包括 TCP 连接、API 协商和 SASL 认证 |
connection_max_idle_time_ms | uint64_t | 600000 (10 min) | 空闲的缓存连接在该毫秒数之后被关闭,仍在等待这些连接的请求会以可重试错误失败;0 表示禁用关闭空闲连接 |
security_protocol | std::string | "PLAINTEXT" | "PLAINTEXT"(默认)或 "sasl"(用于 SASL 认证) |
security_sasl_mechanism | std::string | "PLAIN" | SASL 机制(目前仅支持 "PLAIN") |
security_sasl_username | std::string | (空) | SASL 用户名(协议为 "sasl" 时必填) |
security_sasl_password | std::string | (空) | SASL 密码(协议为 "sasl" 时必填) |
Connection
| 方法 | 描述 |
|---|---|
static Create(const Configuration& config, Connection& out) -> Result | 创建一个到 Fluss 集群的连接 |
GetAdmin(Admin& out) -> Result | 获取 admin 接口 |
GetTable(const TablePath& table_path, Table& out) -> Result | 获取一个用于读写的表 |
Available() -> bool | 检查连接是否有效且已初始化 |
Admin
数据库操作
| 方法 | 描述 |
|---|---|
CreateDatabase(const std::string& database_name, const DatabaseDescriptor& descriptor, bool ignore_if_exists) -> Result | 创建数据库 |
DropDatabase(const std::string& name, bool ignore_if_not_exists, bool cascade) -> Result | 删除数据库 |
ListDatabases(std::vector<std::string>& out) -> Result | 列出所有数据库 |
DatabaseExists(const std::string& name, bool& out) -> Result | 检查数据库是否存在 |
GetDatabaseInfo(const std::string& name, DatabaseInfo& out) -> Result | 获取数据库元数据 |
表操作
| 方法 | 描述 |
|---|---|
CreateTable(const TablePath& path, const TableDescriptor& descriptor, bool ignore_if_exists) -> Result | 创建表 |
DropTable(const TablePath& path, bool ignore_if_not_exists) -> Result | 删除表 |
GetTableInfo(const TablePath& path, TableInfo& out) -> Result | 获取表元数据 |
ListTables(const std::string& database_name, std::vector<std::string>& out) -> Result | 列出数据库中的所有表 |
TableExists(const TablePath& path, bool& out) -> Result | 检查表是否存在 |
分区操作
| 方法 | 描述 |
|---|---|
CreatePartition(const TablePath& path, const std::unordered_map<std::string, std::string>& partition_spec, bool ignore_if_exists) -> Result | 创建分区 |
DropPartition(const TablePath& path, const std::unordered_map<std::string, std::string>& partition_spec, bool ignore_if_not_exists) -> Result | 删除分区 |
ListPartitionInfos(const TablePath& path, std::vector<PartitionInfo>& out) -> Result | 列出分区元数据 |
Offset 操作
| 方法 | 描述 |
|---|---|
ListOffsets(const TablePath& path, const std::vector<int32_t>& bucket_ids, const OffsetSpec& query, std::unordered_map<int32_t, int64_t>& out) -> Result | 获取各 bucket 的 offset |
ListPartitionOffsets(const TablePath& path, const std::string& partition_name, const std::vector<int32_t>& bucket_ids, const OffsetSpec& query, std::unordered_map<int32_t, int64_t>& out) -> Result | 获取某个分区各 bucket 的 offset |
Lake 操作
| 方法 | 描述 |
|---|---|
GetLatestLakeSnapshot(const TablePath& path, LakeSnapshot& out) -> Result | 获取最新的 lake 快照 |
集群操作
| 方法 | 描述 |
|---|---|
GetServerNodes(std::vector<ServerNode>& out) -> Result | 获取所有存活的 server 节点(coordinator + tablets) |
ServerNode
| 字段 | 类型 | 描述 |
|---|---|---|
id | int32_t | Server 节点 ID |
host | std::string | Server 的主机名 |
port | uint32_t | 端口号 |
server_type | std::string | Server 类型("CoordinatorServer"、"TabletServer" 或 "Unknown") |
uid | std::string | 唯一标识符(例如 "cs-0"、"ts-1") |
当客户端尚未确定端点类型时(例如 bootstrap 端点),会使用 "Unknown"。
Table
| 方法 | 描述 |
|---|---|
NewRow() -> GenericRow | 为该表创建一个 schema 感知的 row |
NewAppend() -> TableAppend | 为 log table 创建一个 append 构建器 |
NewUpsert() -> TableUpsert | 为 PK table 创建一个 upsert 构建器 |
NewLookup() -> TableLookup | 为 PK table 创建一个 lookup 构建器 |
NewPrefixLookup(std::vector<std::string> cols, PrefixLookuper& out) -> Result | 创建一个 prefix(bucket-key)lookuper |
NewScan() -> TableScan | 创建一个 scan 构建器 |
GetTableInfo() -> TableInfo | 获取表元数据 |
GetArrowSchema(std::shared_ptr<arrow::Schema>& out) -> Result | AppendArrowBatch 期望的 Arrow schema |
GetTablePath() -> TablePath | 获取表路径 |
HasPrimaryKey() -> bool | 检查表是否有主键 |
TableAppend
| 方法 | 描述 |
|---|---|
CreateWriter(AppendWriter& out) -> Result | 创建 append writer |
TableUpsert
| 方法 | 描述 |
|---|---|
PartialUpdateByIndex(std::vector<size_t> column_indices) -> TableUpsert& | 按列索引配置部分更新 |
PartialUpdateByName(std::vector<std::string> column_names) -> TableUpsert& | 按列名配置部分更新 |
CreateWriter(UpsertWriter& out) -> Result | 创建 upsert writer |
TableLookup
| 方法 | 描述 |
|---|---|
CreateLookuper(Lookuper& out) -> Result | 创建用于点查的 lookuper |
TableScan
| 方法 | 描述 |
|---|---|
ProjectByIndex(std::vector<size_t> column_indices) -> TableScan& | 按索引投影列 |
ProjectByName(std::vector<std::string> column_names) -> TableScan& | 按列名投影列 |
Limit(int32_t row_number) -> TableScan& | 设置正的行数上限(启用 CreateBucketBatchScanner;log scanner 会拒绝) |
CreateLogScanner(LogScanner& out) -> Result | 创建基于 record 的 log scanner;在 primary-key table 上订阅其 CDC changelog(每条记录带 change_type) |
CreateRecordBatchLogScanner(LogScanner& out) -> Result | 创建基于 Arrow RecordBatch 的 log scanner(仅 log table —— 无逐条 change type) |
CreateBucketBatchScanner(const TableBucket& bucket, BatchScanner& out) -> Result | 对单个 bucket 进行有界扫描(需要 Limit) |
AppendWriter
| 方法 | 描述 |
|---|---|
Append(const GenericRow& row) -> Result | 追加一行(发后即忘) |
Append(const GenericRow& row, WriteResult& out) -> Result | 追加一行并等待写入确认 |
Flush() -> Result | 刷新所有待处理的写入 |
UpsertWriter
| 方法 | 描述 |
|---|---|
Upsert(const GenericRow& row) -> Result | Upsert 一行(发后即忘) |
Upsert(const GenericRow& row, WriteResult& out) -> Result | Upsert 一行并等待写入确认 |
Delete(const GenericRow& row) -> Result | 按主键删除一行(发后即忘) |
Delete(const GenericRow& row, WriteResult& out) -> Result | 删除一行并等待写入确认 |
Flush() -> Result | 刷新所有待处理的操作 |
WriteResult
| 方法 | 描述 |
|---|---|
Wait() -> Result | 等待服务端对本次写入的确认 |
Lookuper
按主键执行点查。可通过 TableLookup::CreateLookuper() 获取。每次调用会阻塞直到结果返回。多个线程可以共享同一个 Lookuper,它们并发发起的 lookup 会被合并进同一批请求发送到服务端。
| 方法 | 描述 |
|---|---|
Lookup(const GenericRow& pk_row, LookupResult& out) const -> Result | 按主键查找一行 |
Lookup(const GenericRow& pk_row, PendingLookup& out) const -> Result | 发起一次 lookup 但不阻塞;由 out 等待其对应的行 |
每次阻塞式 lookup 都会等待其所属批次,最长不超过 lookup_batch_timeout_ms。若要在单个线程中查找大量 key,可以先全部发起,再逐个等待:
std::vector<fluss::PendingLookup> pending(keys.size());
for (size_t i = 0; i < keys.size(); ++i) {
if (!lookuper.Lookup(keys[i], pending[i]).Ok()) {
// This lookup did not start.
}
}
for (auto& lookup : pending) {
fluss::LookupResult result;
if (lookup.Available() && lookup.Wait(result).Ok() && result.Found()) {
// Use result.
}
}PendingLookup
由 Lookuper::Lookup(pk_row, PendingLookup&) 发起的 lookup。仅可移动。在 Wait 返回之前销毁它,会放弃该次 lookup。
| 方法 | 描述 |
|---|---|
Wait(LookupResult& out) -> Result | 阻塞直到目标行到达 |
Wait(LookupResult& out, int64_t timeout_ms) -> Result | 最多阻塞 timeout_ms。若等待超时,它返回 REQUEST_TIME_OUT,而该 lookup 仍在继续运行。lookup 本身也可能以 REQUEST_TIME_OUT 失败并就此结束,因此再次等待前需先检查 Available() |
Available() const -> bool | lookup 仍在运行时为 true;一旦 Wait 返回了结果(可能是任意错误码,包括 REQUEST_TIME_OUT)则为 false |
PrefixLookuper
执行 prefix(bucket-key)lookup,返回主键以给定 prefix 开头的所有行。可通过 Table::NewPrefixLookup() 获取。与 Lookuper 类似,它同样可以被多个线程共享,也支持以非阻塞方式发起 lookup。PendingPrefixLookup 的方法与 PendingLookup 相同。参见 Prefix Lookup 示例。
| 方法 | 描述 |
|---|---|
PrefixLookup(const GenericRow& prefix_row, PrefixLookupResult& out) const -> Result | 查找所有匹配 prefix 列的行 |
PrefixLookup(const GenericRow& prefix_row, PendingPrefixLookup& out) const -> Result | 发起一次 lookup 但不阻塞;由 out 等待其结果行 |
LogScanner
| 方法 | 描述 |
|---|---|
Subscribe(int32_t bucket_id, int64_t offset) -> Result | 从指定 offset 订阅单个 bucket |
Subscribe(const std::vector<BucketSubscription>& bucket_offsets) -> Result | 订阅多个 bucket |
SubscribePartitionBuckets(int64_t partition_id, int32_t bucket_id, int64_t start_offset) -> Result | 订阅单个 partition bucket |
SubscribePartitionBuckets(const std::vector<PartitionBucketSubscription>& subscriptions) -> Result | 订阅多个 partition bucket |
Unsubscribe(int32_t bucket_id) -> Result | 取消订阅一个非分区 bucket |
UnsubscribePartition(int64_t partition_id, int32_t bucket_id) -> Result | 取消订阅一个 partition bucket |
Poll(int64_t timeout_ms, ScanRecords& out) -> Result | 轮询单条记录 |
PollRecordBatch(int64_t timeout_ms, ArrowRecordBatches& out) -> Result | 轮询 Arrow RecordBatch |
BatchScanner
对单个 bucket 的一次性有界扫描。可通过 TableScan::CreateBucketBatchScanner() 获取。扫描在首次调用下述方法时执行,产出其唯一一个 batch,之后即耗尽。
| 方法 | 描述 |
|---|---|
NextBatch(ArrowRecordBatches& out) -> Result | 执行扫描;只返回该 batch 一次,之后返回空 |
CollectAllBatches(ArrowRecordBatches& out) -> Result | 将 scanner 排空为全部 batch |
GenericRow
GenericRow 是一个 只写 的 row,用于 append、upsert、delete 以及 lookup key 的构建。若要从 scan 或 lookup 结果中读取字段值,请参见 RowView 和 LookupResult。
基于索引的 Setter
| 方法 | 描述 |
|---|---|
SetNull(size_t idx) | 将字段设为 null |
SetBool(size_t idx, bool value) | 设置 boolean 值 |
SetInt32(size_t idx, int32_t value) | 设置 32 位整数 |
SetInt64(size_t idx, int64_t value) | 设置 64 位整数 |
SetFloat32(size_t idx, float value) | 设置 32 位浮点数 |
SetFloat64(size_t idx, double value) | 设置 64 位浮点数 |
SetString(size_t idx, const std::string& value) | 设置字符串值 |
SetBytes(size_t idx, const std::vector<uint8_t>& value) | 设置二进制数据 |
SetDate(size_t idx, const Date& value) | 设置日期值 |
SetTime(size_t idx, const Time& value) | 设置时间值 |
SetTimestampNtz(size_t idx, const Timestamp& value) | 设置不带时区的 timestamp |
SetTimestampLtz(size_t idx, const Timestamp& value) | 设置带时区的 timestamp |
SetDecimal(size_t idx, const std::string& value) | 从字符串设置 decimal |
SetArray(size_t idx, ArrayWriter&& writer) | 设置 array 值(消耗该 writer) |
SetMap(size_t idx, MapWriter&& writer) | 设置 map 值(消耗该 writer) |
SetRow(size_t idx, GenericRow&& nested) | 从嵌套 row 设置 ROW 值(消耗该 row) |
基于名称的 Setter
使用 table.NewRow() 时,Set() 方法会根据 schema 自动路由到正确的类型:
| 方法 | 描述 |
|---|---|
Set(const std::string& name, std::nullptr_t) | 按列名将字段设为 null |
Set(const std::string& name, bool value) | 按列名设置 boolean |
Set(const std::string& name, int32_t value) | 按列名设置整数 |
Set(const std::string& name, int64_t value) | 按列名设置大整数 |
Set(const std::string& name, float value) | 按列名设置 float |
Set(const std::string& name, double value) | 按列名设置 double |
Set(const std::string& name, const std::string& value) | 按列名设置 string/decimal |
Set(const std::string& name, const Date& value) | 按列名设置日期 |
Set(const std::string& name, const Time& value) | 按列名设置时间 |
Set(const std::string& name, const Timestamp& value) | 按列名设置 timestamp |
Set(const std::string& name, ArrayWriter&& writer) | 按列名设置 array 值 |
Set(const std::string& name, MapWriter&& writer) | 按列名设置 map 值 |
Set(const std::string& name, GenericRow&& nested) | 按列名设置 ROW 值 |
RowView
用于 scan 结果的只读 row 视图。对 string 和 bytes 数据提供零拷贝访问。RowView 通过引用计数共享底层 scan 数据的所有权,因此可以安全地比产生它的 ScanRecords 存活更久。
string_view Lifetime
GetString() 返回的 std::string_view 借用自底层数据。只要任何引用同一 poll 结果的 RowView(或 ScanRecord)仍然存活,该 string_view 就有效。若你需要在所有引用都销毁后仍使用该值,请拷贝到 std::string。
基于索引的 Getter
| 方法 | 描述 |
|---|---|
FieldCount() -> size_t | 获取字段数量 |
GetType(size_t idx) -> TypeId | 获取指定索引处的类型 |
IsNull(size_t idx) -> bool | 检查字段是否为 null |
GetBool(size_t idx) -> bool | 获取指定索引处的 boolean 值 |
GetInt32(size_t idx) -> int32_t | 获取指定索引处的 32 位整数 |
GetInt64(size_t idx) -> int64_t | 获取指定索引处的 64 位整数 |
GetFloat32(size_t idx) -> float | 获取指定索引处的 32 位浮点数 |
GetFloat64(size_t idx) -> double | 获取指定索引处的 64 位浮点数 |
GetString(size_t idx) -> std::string_view | 获取指定索引处的字符串(零拷贝) |
GetBytes(size_t idx) -> std::pair<const uint8_t*, size_t> | 获取指定索引处的二进制数据(零拷贝) |
GetDate(size_t idx) -> Date | 获取指定索引处的日期 |
GetTime(size_t idx) -> Time | 获取指定索引处的时间 |
GetTimestamp(size_t idx) -> Timestamp | 获取指定索引处的 timestamp |
IsDecimal(size_t idx) -> bool | 检查字段是否为 decimal 类型 |
GetDecimalString(size_t idx) -> std::string | 获取指定索引处的 decimal 字符串 |
复杂类型的 Getter(ARRAY / MAP / ROW)
| 方法 | 描述 |
|---|---|
GetValue(size_t idx) -> Value | 将复杂列获取为递归的 Value 句柄 |
GetValue(const std::string& name) -> Value | 同上,按列名 |
使用 At / KeyAt / ValueAt / Field 遍历返回的 Value,并用其 Get*() 方法读取叶子值。
基于名称的 Getter
| 方法 | 描述 |
|---|---|
IsNull(const std::string& name) -> bool | 按名称检查字段是否为 null |
GetBool(const std::string& name) -> bool | 按列名获取 boolean |
GetInt32(const std::string& name) -> int32_t | 按列名获取 32 位整数 |
GetInt64(const std::string& name) -> int64_t | 按列名获取 64 位整数 |
GetFloat32(const std::string& name) -> float | 按列名获取 32 位浮点数 |
GetFloat64(const std::string& name) -> double | 按列名获取 64 位浮点数 |
GetString(const std::string& name) -> std::string_view | 按列名获取字符串 |
GetBytes(const std::string& name) -> std::pair<const uint8_t*, size_t> | 按列名获取二进制数据 |
GetDate(const std::string& name) -> Date | 按列名获取日期 |
GetTime(const std::string& name) -> Time | 按列名获取时间 |
GetTimestamp(const std::string& name) -> Timestamp | 按列名获取 timestamp |
GetDecimalString(const std::string& name) -> std::string | 按列名获取 decimal 字符串 |
ScanRecord
ScanRecord 是值类型,可以自由拷贝、存储,并在多次 Poll() 调用之间累积。它通过引用计数共享底层 scan 数据的所有权。
| 字段 | 类型 | 描述 |
|---|---|---|
offset | int64_t | 记录在日志中的 offset |
timestamp | int64_t | 记录时间戳 |
change_type | ChangeType | 变更类型(AppendOnly、Insert、UpdateBefore、UpdateAfter、Delete) |
row | RowView | 行数据(值类型,通过引用计数共享所有权) |
ScanRecords
扁平访问
| 方法 | 描述 |
|---|---|
Count() -> size_t | 所有 bucket 中的记录总数 |
IsEmpty() -> bool | 检查是否为空 |
begin() / end() | 支持基于范围的 for 循环的迭代器 |
对所有记录(不分 bucket)的扁平迭代:
for (const auto& rec : records) {
std::cout << "offset=" << rec.offset << std::endl;
}按 bucket 访问
| 方法 | 描述 |
|---|---|
BucketCount() -> size_t | 不同 bucket 的数量 |
Buckets() -> std::vector<TableBucket> | 不同 bucket 的列表 |
Records(const TableBucket& bucket) -> BucketRecords | 指定 bucket 的记录(若该 bucket 不存在则为空) |
BucketAt(size_t idx) -> BucketRecords | 按 bucket 索引获取记录(从 0 开始,O(1)) |
BucketRecords
属于单个 bucket 的一组 scan 记录。可通过 ScanRecords::Records() 或 ScanRecords::BucketAt() 获取。BucketRecords 是值类型 —— 它通过引用计数共享底层 scan 数据的所有权,因此可以安全地比产生它的 ScanRecords 存活更久。
| 方法 | 描述 |
|---|---|
Size() -> size_t | 该 bucket 中的记录数 |
Empty() -> bool | 检查是否为空 |
Bucket() -> const TableBucket& | 获取该 bucket |
operator[](size_t idx) -> ScanRecord | 按索引访问该 bucket 内的记录 |
begin() / end() | 支持基于范围的 for 循环的迭代器 |
TableBucket
| 字段 / 方法 | 描述 |
|---|---|
table_id -> int64_t | 表 ID |
bucket_id -> int32_t | Bucket ID |
partition_id -> std::optional<int64_t> | 分区 ID(非分区时为空) |
operator==(const TableBucket&) -> bool | 相等性比较 |
LookupResult
查找操作的只读结果。对字段值提供零拷贝访问。
元数据
| 方法 | 描述 |
|---|---|
Found() -> bool | 是否找到匹配的行 |
FieldCount() -> size_t | 获取字段数量 |
基于索引的 Getter
| 方法 | 描述 |
|---|---|
GetType(size_t idx) -> TypeId | 获取指定索引处的类型 |
IsNull(size_t idx) -> bool | 检查字段是否为 null |
GetBool(size_t idx) -> bool | 获取指定索引处的 boolean 值 |
GetInt32(size_t idx) -> int32_t | 获取指定索引处的 32 位整数 |
GetInt64(size_t idx) -> int64_t | 获取指定索引处的 64 位整数 |
GetFloat32(size_t idx) -> float | 获取指定索引处的 32 位浮点数 |
GetFloat64(size_t idx) -> double | 获取指定索引处的 64 位浮点数 |
GetString(size_t idx) -> std::string_view | 获取指定索引处的字符串(零拷贝) |
GetBytes(size_t idx) -> std::pair<const uint8_t*, size_t> | 获取指定索引处的二进制数据(零拷贝) |
GetDate(size_t idx) -> Date | 获取指定索引处的日期 |
GetTime(size_t idx) -> Time | 获取指定索引处的时间 |
GetTimestamp(size_t idx) -> Timestamp | 获取指定索引处的 timestamp |
IsDecimal(size_t idx) -> bool | 检查字段是否为 decimal 类型 |
GetDecimalString(size_t idx) -> std::string | 获取指定索引处的 decimal 字符串 |
复杂类型的 Getter(ARRAY / MAP / ROW)
GetValue(size_t idx) -> Value 和 GetValue(const std::string& name) -> Value,与 RowView 上相同。遍历返回的 Value。
基于名称的 Getter
| 方法 | 描述 |
|---|---|
IsNull(const std::string& name) -> bool | 按名称检查字段是否为 null |
GetBool(const std::string& name) -> bool | 按列名获取 boolean |
GetInt32(const std::string& name) -> int32_t | 按列名获取 32 位整数 |
GetInt64(const std::string& name) -> int64_t | 按列名获取 64 位整数 |
GetFloat32(const std::string& name) -> float | 按列名获取 32 位浮点数 |
GetFloat64(const std::string& name) -> double | 按列名获取 64 位浮点数 |
GetString(const std::string& name) -> std::string_view | 按列名获取字符串 |
GetBytes(const std::string& name) -> std::pair<const uint8_t*, size_t> | 按列名获取二进制数据 |
GetDate(const std::string& name) -> Date | 按列名获取日期 |
GetTime(const std::string& name) -> Time | 按列名获取时间 |
GetTimestamp(const std::string& name) -> Timestamp | 按列名获取 timestamp |
GetDecimalString(const std::string& name) -> std::string | 按列名获取 decimal 字符串 |
PrefixLookupResult
prefix lookup 的只读结果 —— 零个或多个匹配行。每一行是一个 PrefixRowView,暴露与 RowView 相同的标量 getter(基于索引和名称),并为复杂(ARRAY / MAP / ROW)列提供 GetValue(...)。
| 方法 | 描述 |
|---|---|
Size() -> size_t | 匹配的行数 |
IsEmpty() -> bool | 结果是否没有行 |
GetRow(size_t index) -> PrefixRowView | 获取 index 处的行(越界时抛出异常) |
ArrowRecordBatch
| 方法 | 描述 |
|---|---|
GetArrowRecordBatch() -> std::shared_ptr<arrow::RecordBatch> | 获取底层的 Arrow RecordBatch |
Available() -> bool | 检查该 batch 是否有效 |
NumRows() -> int64_t | 该 batch 中的行数 |
GetTableId() -> int64_t | 表 ID |
GetPartitionId() -> int64_t | 分区 ID |
GetBucketId() -> int32_t | Bucket ID |
GetBaseOffset() -> int64_t | 首条记录的 offset |
GetLastOffset() -> int64_t | 最后一条记录的 offset |
GetCommitTimestamp() -> int64_t | 提交时间(毫秒时间戳) |
GetSchemaId() -> int32_t | 来源 log batch 的 Schema ID |
GetCommitTimestamp() 在该 batch 不携带单一时间戳时返回 ArrowRecordBatch::NO_COMMIT_TIMESTAMP(-1);limit scan 会将多个 log batch 合并为一个 Arrow batch,因此属于这种情况。
GetSchemaId() 返回写入该 log batch 时所用 schema 的 ID。它可能与 batch 各列所属的 schema 不同,因为 scanner 会将每个 batch 对齐到创建它时所使用的表 schema。对于 limit scan,它返回 ArrowRecordBatch::NO_SCHEMA_ID(-1);limit scan 会将多个 log batch 合并为一个 batch。
ArrowRecordBatches
| 方法 | 描述 |
|---|---|
Size() -> size_t | batch 数量 |
Empty() -> bool | 检查是否为空 |
operator[](size_t idx) | 按索引访问 batch |
begin() / end() | 支持基于范围的 for 循环的迭代器 |
Schema
| 方法 | 描述 |
|---|---|
NewBuilder() -> Schema::Builder | 创建一个新的 schema 构建器 |
FromArrow(std::shared_ptr<arrow::Schema> schema, std::vector<std::string> primary_keys = {}) -> Schema | 从已有的 Arrow schema 构建 schema(逃生通道;优先使用 DataType::Map / DataType::Row) |
Schema::Builder
| 方法 | 描述 |
|---|---|
AddColumn(const std::string& name, const DataType& type) -> Builder& | 添加一列 |
SetPrimaryKeys(const std::vector<std::string>& keys) -> Builder& | 设置主键列 |
SetAutoIncrementColumn(std::string column) -> Builder& | 设置自增列。必须是 primary-key table 的 INT 或 BIGINT 列,且其自身不能是主键列。 |
Build() -> Schema | 构建 schema |
TableDescriptor
| 方法 | 描述 |
|---|---|
NewBuilder() -> TableDescriptor::Builder | 创建一个新的 table descriptor 构建器 |
TableDescriptor::Builder
| 方法 | 描述 |
|---|---|
SetSchema(const Schema& schema) -> Builder& | 设置表 schema |
SetPartitionKeys(const std::vector<std::string>& keys) -> Builder& | 设置分区键列 |
SetBucketCount(int32_t count) -> Builder& | 设置 bucket 数量 |
SetBucketKeys(const std::vector<std::string>& keys) -> Builder& | 设置 bucket key 列 |
SetProperty(const std::string& key, const std::string& value) -> Builder& | 设置一个表属性 |
SetCustomProperty(const std::string& key, const std::string& value) -> Builder& | 设置一个自定义属性 |
SetComment(const std::string& comment) -> Builder& | 设置表注释 |
Build() -> TableDescriptor | 构建 table descriptor |
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(int precision) | 不带时区的 timestamp |
DataType::TimestampLtz(int precision) | 带时区的 timestamp |
DataType::Decimal(int precision, int scale) | 带 precision 和 scale 的 decimal |
DataType::Array(DataType element) | 给定元素类型的 array |
DataType::Map(DataType key, DataType value) | key/value 类型的 map(两者均可为复杂类型) |
DataType::Row(std::vector<std::pair<std::string, DataType>> fields) | 具名字段的 row(类型可为复杂类型) |
MAP / ROW(以及嵌套它们的 array)可以组合到任意深度;在创建表或构建 writer 时,绑定会在内部将它们降级为 Arrow。
访问器
| 方法 | 描述 |
|---|---|
id() -> TypeId | 获取类型 ID |
precision() -> int | 获取 precision(用于 Decimal/Timestamp 类型) |
scale() -> int | 获取 scale(用于 Decimal 类型) |
nullable() -> bool | 若该类型可为 null(默认)返回 true,若为 NOT NULL 返回 false |
element_type() -> const DataType* | 获取元素类型(用于 Array 类型,否则为 nullptr) |
key_type() / value_type() -> const DataType* | MAP 的 key / value 类型(否则为 nullptr) |
field_count() -> size_t | ROW 字段数量(否则为 0) |
field_name(size_t i) -> const std::string& | i 处的 ROW 字段名 |
field_type(size_t i) -> const DataType* | i 处的 ROW 字段类型 |
NotNull() -> DataType | 返回该类型的一份拷贝,并将 nullable 置为 false |
ArrayWriter
array 列值的只写构建器。以固定的 size 和元素类型构造,然后逐个元素填充。仅可移动 —— 由 GenericRow::SetArray() 或用于嵌套 array 的 ArrayWriter::SetArray() 消耗。对于元素为 ROW / MAP 的 array,请从 Arrow 元素类型(arrow::struct_(...) / arrow::map(...))构造。
| 方法 | 描述 |
|---|---|
ArrayWriter(size_t size, DataType element_type) | 创建一个 array writer;元素可为复杂类型(DataType::Map/Row) |
ArrayWriter(size_t size, std::shared_ptr<arrow::DataType> element_type) | 元素类型的 Arrow 逃生通道 |
SetNull(size_t idx) | 将元素设为 null |
SetBool(size_t idx, bool value) | 设置 boolean 元素 |
SetInt32(size_t idx, int32_t value) | 设置 32 位整数元素 |
SetInt64(size_t idx, int64_t value) | 设置 64 位整数元素 |
SetFloat32(size_t idx, float value) | 设置 32 位浮点数元素 |
SetFloat64(size_t idx, double value) | 设置 64 位浮点数元素 |
SetString(size_t idx, const std::string& value) | 设置字符串元素 |
SetBytes(size_t idx, const std::vector<uint8_t>& value) | 设置二进制元素 |
SetDate(size_t idx, const Date& value) | 设置日期元素 |
SetTime(size_t idx, const Time& value) | 设置时间元素 |
SetTimestampNtz(size_t idx, const Timestamp& value) | 设置不带时区的 timestamp 元素 |
SetTimestampLtz(size_t idx, const Timestamp& value) | 设置带时区的 timestamp 元素 |
SetDecimal(size_t idx, const std::string& value) | 从字符串设置 decimal 元素 |
SetArray(size_t idx, ArrayWriter&& nested) | 设置嵌套 array 元素(消耗 nested) |
SetRow(size_t idx, GenericRow&& row) | 设置 ROW 元素(消耗该 row) |
SetMap(size_t idx, MapWriter&& map) | 设置 MAP 元素(消耗该 map) |
MapWriter
map 列值的只写构建器。以 key/value 类型构造,然后对每个条目设置 key 和 value 并调用 Commit()。key 不能为 null;未设置的 value 默认为 null。仅可移动 —— 由 GenericRow::SetMap()、ArrayWriter::SetMap() 和 MapWriter::SetValueMap() 消耗。对于自身为 ROW / MAP / ARRAY 的 key/value,请从 Arrow 类型构造并使用 SetValue{Row,Map,Array} setter。
| 方法 | 描述 |
|---|---|
MapWriter(size_t capacity, DataType key_type, DataType value_type) | 创建一个 map writer;key/value 可为复杂类型(DataType::Map/Row/Array) |
MapWriter(size_t capacity, std::shared_ptr<arrow::DataType> key_type, std::shared_ptr<arrow::DataType> value_type) | key/value 类型的 Arrow 逃生通道 |
SetKey{Bool,Int32,Int64,Float32,Float64,String,Bytes,Date,Time,Timestamp,Decimal}(...) | 暂存条目 key(NTZ 还是 LTZ 由声明的 key 类型决定) |
SetValueNull() | 暂存一个 null 条目 value |
SetValue{Bool,Int32,Int64,Float32,Float64,String,Bytes,Date,Time,Timestamp,Decimal}(...) | 暂存条目 value(NTZ 还是 LTZ 由声明的 value 类型决定) |
SetValueRow(GenericRow&&) / SetValueMap(MapWriter&&) / SetValueArray(ArrayWriter&&) | 暂存一个复合条目 value(消耗该 writer) |
Commit() | 将暂存的 key/value 作为一个 map 条目追加 |
Value
复杂(ARRAY / MAP / ROW)列值的只读句柄,可通过 RowView::GetValue()、LookupResult::GetValue() 或 PrefixRowView::GetValue() 获取。用 At / KeyAt / ValueAt / Field 导航到子节点(每个都返回一个子 Value);用 Get*() 方法读取叶子值。对 null 或类型错误的值进行带类型读取会抛出异常。仅可移动;持有一个 Rust 句柄,在析构时释放。
叶子读取
| 方法 | 描述 |
|---|---|
Type() -> TypeId | 该值的类型 |
IsNull() -> bool | 该值是否为 null |
GetBool() -> bool | 读取一个 boolean 叶子 |
GetInt32() -> int32_t | 读取一个 32 位整数叶子 |
GetInt64() -> int64_t | 读取一个 64 位整数叶子 |
GetFloat32() -> float | 读取一个 32 位浮点数叶子 |
GetFloat64() -> double | 读取一个 64 位浮点数叶子 |
GetString() -> std::string | 读取一个字符串叶子 |
GetBytes() -> std::vector<uint8_t> | 读取一个二进制叶子 |
GetDate() -> Date | 读取一个日期叶子 |
GetTime() -> Time | 读取一个时间叶子 |
GetTimestamp() -> Timestamp | 读取一个 timestamp 叶子 |
GetDecimalString() -> std::string | 将 decimal 叶子读取为字符串 |
导航
| 方法 | 描述 |
|---|---|
Size() -> size_t | 元素数量(ARRAY)/ 条目数(MAP) |
FieldCount() -> size_t | 字段数量(ROW) |
At(size_t i) -> Value | ARRAY 元素 i |
KeyAt(size_t i) -> Value | 条目 i 处的 MAP key |
ValueAt(size_t i) -> Value | 条目 i 处的 MAP value |
Field(size_t i) -> Value | ROW 字段 i |
Field(const std::string& name) -> Value | 按名称获取 ROW 字段 |
TablePath
| 方法 / 字段 | 描述 |
|---|---|
TablePath(const std::string& database, const std::string& table) | 创建一个表路径 |
database_name -> std::string | 数据库名 |
table_name -> std::string | 表名 |
ToString() -> std::string | 字符串表示 |
TableInfo
| 字段 | 类型 | 描述 |
|---|---|---|
table_id | int64_t | 表 ID |
schema_id | int32_t | Schema ID |
table_path | TablePath | 表路径 |
created_time | int64_t | 创建时间戳 |
modified_time | int64_t | 最后修改时间戳 |
primary_keys | std::vector<std::string> | 主键列 |
bucket_keys | std::vector<std::string> | Bucket key 列 |
partition_keys | std::vector<std::string> | 分区键列 |
num_buckets | int32_t | Bucket 数量 |
has_primary_key | bool | 表是否有主键 |
is_partitioned | bool | 表是否分区 |
properties | std::unordered_map<std::string, std::string> | 表属性 |
custom_properties | std::unordered_map<std::string, std::string> | 自定义属性 |
comment | std::string | 表注释 |
schema | Schema | 表 schema |
时间类型
Date
| 方法 | 描述 |
|---|---|
Date::FromDays(int32_t days) | 从自 epoch 起的天数创建 |
Date::FromYMD(int year, int month, int day) | 从年、月、日创建 |
Year() -> int | 获取年 |
Month() -> int | 获取月 |
Day() -> int | 获取日 |
Time
| 方法 | 描述 |
|---|---|
Time::FromMillis(int32_t millis) | 从自午夜起的毫秒数创建 |
Time::FromHMS(int hour, int minute, int second) | 从时、分、秒创建 |
Hour() -> int | 获取小时 |
Minute() -> int | 获取分钟 |
Second() -> int | 获取秒 |
Millis() -> int64_t | 获取秒内的毫秒分量(0-999) |
Timestamp
| 方法 | 描述 |
|---|---|
Timestamp::FromMillis(int64_t millis) | 从自 epoch 起的毫秒数创建 |
Timestamp::FromMillisNanos(int64_t millis, int32_t nanos) | 从毫秒和纳秒创建 |
Timestamp::FromTimePoint(std::chrono::system_clock::time_point tp) | 从时间点创建 |
PartitionInfo
| 字段 | 类型 | 描述 |
|---|---|---|
partition_id | int64_t | 分区 ID |
partition_name | std::string | 分区名 |
bucket_count | int32_t | 分区的 bucket 数 |
DatabaseDescriptor
| 字段 | 类型 | 描述 |
|---|---|---|
comment | std::string | 数据库注释 |
properties | std::unordered_map<std::string, std::string> | 自定义属性 |
DatabaseInfo
| 字段 | 类型 | 描述 |
|---|---|---|
database_name | std::string | 数据库名 |
comment | std::string | 数据库注释 |
properties | std::unordered_map<std::string, std::string> | 自定义属性 |
created_time | int64_t | 创建时间戳 |
modified_time | int64_t | 最后修改时间戳 |
LakeSnapshot
| 字段 | 类型 | 描述 |
|---|---|---|
snapshot_id | int64_t | 快照 ID |
bucket_offsets | std::vector<BucketOffset> | 所有 bucket 的 offset |
BucketOffset
| 字段 | 类型 | 描述 |
|---|---|---|
table_id | int64_t | 表 ID |
partition_id | int64_t | 分区 ID |
bucket_id | int32_t | Bucket ID |
offset | int64_t | Offset 值 |
OffsetSpec
| 方法 | 描述 |
|---|---|
OffsetSpec::Earliest() | 查询最早可用的 offset |
OffsetSpec::Latest() | 查询最新的 offset |
OffsetSpec::Timestamp(int64_t timestamp_ms) | 查询特定时间戳处的 offset |
常量
| 常量 | 值 | 描述 |
|---|---|---|
fluss::EARLIEST_OFFSET | -2 | 从最早可用的 offset 开始读取 |
要从最新 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]);枚举
ChangeType
| 值 | 短字符串 | 描述 |
|---|---|---|
AppendOnly | +A | 仅追加记录 |
Insert | +I | 插入的行 |
UpdateBefore | -U | 被更新行的旧值 |
UpdateAfter | +U | 被更新行的新值 |
Delete | -D | 被删除的行 |
你可以参考下面的示例,将 ChangeType 枚举转换为其短字符串表示。
inline const char* ChangeTypeShortString(ChangeType ct) {
switch (ct) {
case ChangeType::AppendOnly: return "+A";
case ChangeType::Insert: return "+I";
case ChangeType::UpdateBefore: return "-U";
case ChangeType::UpdateAfter: return "+U";
case ChangeType::Delete: return "-D";
}
throw std::invalid_argument("Unknown ChangeType");
}TypeId
| 值 | 描述 |
|---|---|
Boolean | Boolean 类型 |
TinyInt | 8 位有符号整数 |
SmallInt | 16 位有符号整数 |
Int | 32 位有符号整数 |
BigInt | 64 位有符号整数 |
Float | 32 位浮点数 |
Double | 64 位浮点数 |
String | UTF-8 字符串 |
Bytes | 二进制数据 |
Date | 日期 |
Time | 时间 |
Timestamp | 不带时区的 timestamp |
TimestampLtz | 带时区的 timestamp |
Decimal | Decimal |
Array | 元素组成的 array |
Map | key/value 对组成的 map |
Row | 字段组成的 row(struct) |
ChangeType
| 值 | 描述 |
|---|---|
AppendOnly | 仅追加记录(log table) |
Insert | 插入的行(PK table) |
UpdateBefore | 更新前的行值(PK table) |
UpdateAfter | 更新后的行值(PK table) |
Delete | 被删除的行(PK table) |
OffsetSpec
| 值 | 描述 |
|---|---|
Earliest | 最早可用的 offset |
Latest | 最新的 offset |
Timestamp | 特定时间戳处的 offset |