Skip to content

API 参考 ​

Fluss C++ 客户端的完整 API 参考。

Result ​

字段 / 方法类型描述
error_codeint32_t0 表示成功,非 0 表示错误
error_messagestd::string人类可读的错误描述
Ok()bool操作成功(error_code == 0)时返回 true

Configuration ​

字段类型默认值描述
bootstrap_serversstd::string"127.0.0.1:9123"Coordinator server 地址
writer_request_max_sizeint32_t10485760 (10 MB)请求的最大字节数
writer_acksstd::string"all"确认设置("all"、"0"、"1" 或 "-1")
writer_retriesint32_tINT32_MAX失败时的重试次数
writer_batch_sizeint32_t2097152 (2 MB)写入批次的字节大小。开启动态调整时为上限;关闭时为固定批次大小
writer_dynamic_batch_size_enabledbooltrue启用按表动态调整批次大小:填充超过 80% 时目标增大 10%,低于 50% 时缩小 5%
writer_dynamic_batch_size_minint32_t262144 (256 KB)动态批次大小估算器的下限(禁用时忽略)
writer_batch_timeout_msint64_t100等待写入批次填满后再发送的最长时间(毫秒)
writer_bucket_no_key_assignerstd::string"sticky"无 bucket key 的表的 bucket 分配策略:"sticky" 或 "round_robin"
writer_dynamic_create_partition_enabledbooltrue写入时自动创建尚不存在的分区。为 false 时,此类写入以 PARTITION_NOT_EXISTS 失败
scanner_remote_log_prefetch_numsize_t4预取的远程 log segment 数量
remote_file_download_thread_numsize_t3远程日志下载的线程数
scanner_remote_log_read_concurrencysize_t4单个远程日志文件内的流式读取并发度
scanner_io_tmp_dirstd::string""远程日志下载的父目录;为空时使用操作系统临时目录加 /fluss
scanner_log_max_poll_recordssize_t500单次 Poll() 返回的最大记录数
scanner_log_fetch_max_bytesint32_t16777216 (16 MB)LogScanner 单次 fetch 响应的最大字节数
scanner_log_fetch_min_bytesint32_t1服务端返回 fetch 响应前必须累积的最小字节数
scanner_log_fetch_wait_max_time_msint32_t500服务端为满足最小字节数可等待的最长时间(毫秒)
scanner_log_fetch_max_bytes_for_bucketint32_t1048576 (1 MB)LogScanner 每个 bucket 单次 fetch 响应的最大字节数
scanner_log_check_crcbooltrue检查所拉取日志批次的 CRC,损坏的批次会导致扫描失败。服务端投影(projected)的批次不检查
lookup_queue_sizesize_t25600发送队列容量;队列满时新的 lookup 会等待。每个 bucket 在等待 leader 期间也最多容纳这么多个 lookup;超出该 bucket 的 lookup 会立即因缓冲区耗尽错误而失败
lookup_max_retriesint32_tINT32_MAXlookup 因可重试错误而重发的最大次数。等待 bucket 的 leader 不算重试,因此这不是 lookup 超时
lookup_retry_backoff_msuint64_t100因可重试错误而失败的 lookup 重发前的初始延迟(毫秒),该 lookup 每次重试都会翻倍;也是无已知 leader 的 bucket 再次刷新前的延迟,该 bucket 每次刷新都会翻倍并由其 lookup 共享。±20% 抖动;0 表示立即重发和刷新
lookup_retry_max_backoff_msuint64_t1000lookup 重试延迟与无已知 leader 的 bucket 刷新延迟的上限(毫秒),在抖动之前生效;必须 >= lookup_retry_backoff_ms
connect_timeout_msuint64_t15000连接建立超时(毫秒),包括 TCP 连接、API 协商和 SASL 认证
connection_max_idle_time_msuint64_t600000 (10 min)空闲的缓存连接在该毫秒数之后被关闭,仍在等待这些连接的请求会以可重试错误失败;0 表示禁用关闭空闲连接
security_protocolstd::string"PLAINTEXT""PLAINTEXT"(默认)或 "sasl"(用于 SASL 认证)
security_sasl_mechanismstd::string"PLAIN"SASL 机制(目前仅支持 "PLAIN")
security_sasl_usernamestd::string(空)SASL 用户名(协议为 "sasl" 时必填)
security_sasl_passwordstd::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 ​

字段类型描述
idint32_tServer 节点 ID
hoststd::stringServer 的主机名
portuint32_t端口号
server_typestd::stringServer 类型("CoordinatorServer"、"TabletServer" 或 "Unknown")
uidstd::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) -> ResultAppendArrowBatch 期望的 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) -> ResultUpsert 一行(发后即忘)
Upsert(const GenericRow& row, WriteResult& out) -> ResultUpsert 一行并等待写入确认
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,可以先全部发起,再逐个等待:

cpp
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 -> boollookup 仍在运行时为 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 数据的所有权。

字段类型描述
offsetint64_t记录在日志中的 offset
timestampint64_t记录时间戳
change_typeChangeType变更类型(AppendOnly、Insert、UpdateBefore、UpdateAfter、Delete)
rowRowView行数据(值类型,通过引用计数共享所有权)

ScanRecords ​

扁平访问 ​

方法描述
Count() -> size_t所有 bucket 中的记录总数
IsEmpty() -> bool检查是否为空
begin() / end()支持基于范围的 for 循环的迭代器

对所有记录(不分 bucket)的扁平迭代:

cpp
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_tBucket 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_tBucket 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_tbatch 数量
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_tROW 字段数量(否则为 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) -> ValueARRAY 元素 i
KeyAt(size_t i) -> Value条目 i 处的 MAP key
ValueAt(size_t i) -> Value条目 i 处的 MAP value
Field(size_t i) -> ValueROW 字段 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_idint64_t表 ID
schema_idint32_tSchema ID
table_pathTablePath表路径
created_timeint64_t创建时间戳
modified_timeint64_t最后修改时间戳
primary_keysstd::vector<std::string>主键列
bucket_keysstd::vector<std::string>Bucket key 列
partition_keysstd::vector<std::string>分区键列
num_bucketsint32_tBucket 数量
has_primary_keybool表是否有主键
is_partitionedbool表是否分区
propertiesstd::unordered_map<std::string, std::string>表属性
custom_propertiesstd::unordered_map<std::string, std::string>自定义属性
commentstd::string表注释
schemaSchema表 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_idint64_t分区 ID
partition_namestd::string分区名
bucket_countint32_t分区的 bucket 数

DatabaseDescriptor ​

字段类型描述
commentstd::string数据库注释
propertiesstd::unordered_map<std::string, std::string>自定义属性

DatabaseInfo ​

字段类型描述
database_namestd::string数据库名
commentstd::string数据库注释
propertiesstd::unordered_map<std::string, std::string>自定义属性
created_timeint64_t创建时间戳
modified_timeint64_t最后修改时间戳

LakeSnapshot ​

字段类型描述
snapshot_idint64_t快照 ID
bucket_offsetsstd::vector<BucketOffset>所有 bucket 的 offset

BucketOffset ​

字段类型描述
table_idint64_t表 ID
partition_idint64_t分区 ID
bucket_idint32_tBucket ID
offsetint64_tOffset 值

OffsetSpec ​

方法描述
OffsetSpec::Earliest()查询最早可用的 offset
OffsetSpec::Latest()查询最新的 offset
OffsetSpec::Timestamp(int64_t timestamp_ms)查询特定时间戳处的 offset

常量 ​

常量值描述
fluss::EARLIEST_OFFSET-2从最早可用的 offset 开始读取

要从最新 offset(仅新记录)开始读取,请在订阅前通过 ListOffsets 解析当前 offset:

cpp
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 枚举转换为其短字符串表示。

cpp
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 ​

值描述
BooleanBoolean 类型
TinyInt8 位有符号整数
SmallInt16 位有符号整数
Int32 位有符号整数
BigInt64 位有符号整数
Float32 位浮点数
Double64 位浮点数
StringUTF-8 字符串
Bytes二进制数据
Date日期
Time时间
Timestamp不带时区的 timestamp
TimestampLtz带时区的 timestamp
DecimalDecimal
Array元素组成的 array
Mapkey/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

Fluss 中文文档