Skip to content

Log Table ​

Log table 是无主键的仅追加表,适用于事件流场景。

创建 Log Table ​

cpp
auto schema = fluss::Schema::NewBuilder()
    .AddColumn("event_id", fluss::DataType::Int())
    .AddColumn("event_type", fluss::DataType::String())
    .AddColumn("timestamp", fluss::DataType::BigInt())
    .Build();

auto descriptor = fluss::TableDescriptor::NewBuilder()
    .SetSchema(schema)
    .Build();

fluss::TablePath table_path("fluss", "events");
admin.CreateTable(table_path, descriptor, true);

写入 Log Table ​

cpp
fluss::Table table;
conn.GetTable(table_path, table);

fluss::AppendWriter writer;
table.NewAppend().CreateWriter(writer);

fluss::GenericRow row;
row.SetInt32(0, 1);           // event_id
row.SetString(1, "user_login");  // event_type
row.SetInt64(2, 1704067200000L); // timestamp
writer.Append(row);

writer.Flush();

从 Log Table 读取 ​

cpp
fluss::LogScanner scanner;
table.NewScan().CreateLogScanner(scanner);

auto info = table.GetTableInfo();
for (int b = 0; b < info.num_buckets; ++b) {
    scanner.Subscribe(b, 0);
}

fluss::ScanRecords records;
scanner.Poll(5000, records);  // timeout in ms

for (const auto& rec : records) {
    std::cout << "event_id=" << rec.row.GetInt32(0)
              << " event_type=" << rec.row.GetString(1)
              << " timestamp=" << rec.row.GetInt64(2)
              << " @ offset=" << rec.offset << std::endl;
}

// Or per-bucket access
for (const auto& bucket : records.Buckets()) {
    auto view = records.Records(bucket);
    std::cout << "Bucket " << bucket.bucket_id << ": "
              << view.Size() << " records" << std::endl;
    for (const auto& rec : view) {
        std::cout << "  event_id=" << rec.row.GetInt32(0)
                  << " event_type=" << rec.row.GetString(1)
                  << " @ offset=" << rec.offset << std::endl;
    }
}

持续轮询:

cpp
while (running) {
    fluss::ScanRecords records;
    scanner.Poll(1000, records);
    for (const auto& rec : records) {
        process(rec);
    }
}

跨多次 poll 累积记录:

ScanRecord 是值类型 —— 它可以自由拷贝、存储和累积。底层数据通过引用计数保持存活(零拷贝)。

cpp
std::vector<fluss::ScanRecord> all_records;
while (all_records.size() < 1000) {
    fluss::ScanRecords records;
    scanner.Poll(1000, records);
    for (const auto& rec : records) {
        all_records.push_back(rec);  // ref-counted, no data copy
    }
}
// all_records is valid — each record keeps its data alive

批量订阅:

cpp
std::vector<fluss::BucketSubscription> subscriptions;
subscriptions.push_back({0, 0});    // bucket 0, offset 0
subscriptions.push_back({1, 100});  // bucket 1, offset 100
scanner.Subscribe(subscriptions);

取消订阅某个 bucket:

cpp
// Stop receiving records from bucket 1
scanner.Unsubscribe(1);

Arrow RecordBatch 轮询(高性能):

cpp
#include <arrow/record_batch.h>

fluss::LogScanner arrow_scanner;
table.NewScan().CreateRecordBatchLogScanner(arrow_scanner);

for (int b = 0; b < info.num_buckets; ++b) {
    arrow_scanner.Subscribe(b, 0);
}

fluss::ArrowRecordBatches batches;
arrow_scanner.PollRecordBatch(5000, batches);

for (size_t i = 0; i < batches.Size(); ++i) {
    const auto& batch = batches[i];
    if (batch->Available()) {
        auto arrow_batch = batch->GetArrowRecordBatch();
        std::cout << "Batch " << i << ": " << arrow_batch->num_rows() << " rows"
                  << ", partition_id=" << batch->GetPartitionId()
                  << ", bucket_id=" << batch->GetBucketId() << std::endl;
    }
}

列投影 ​

cpp
// Project by column index
fluss::LogScanner projected_scanner;
table.NewScan().ProjectByIndex({0, 2}).CreateLogScanner(projected_scanner);

// Project by column name
fluss::LogScanner name_projected_scanner;
table.NewScan().ProjectByName({"event_id", "timestamp"}).CreateLogScanner(name_projected_scanner);

// Arrow RecordBatch with projection
fluss::LogScanner projected_arrow_scanner;
table.NewScan().ProjectByIndex({0, 2}).CreateRecordBatchLogScanner(projected_arrow_scanner);

Limit Scan ​

要对单个 bucket 进行最多 n 行的有界读取,请使用 batch scanner 而不是订阅。它只发起一次请求;NextBatch 只产出该 batch 一次,之后即报告为空。

cpp
int64_t table_id = table.GetTableInfo().table_id;
fluss::TableBucket bucket{table_id, 0};

fluss::BatchScanner scanner;
table.NewScan().Limit(10).CreateBucketBatchScanner(bucket, scanner);

fluss::ArrowRecordBatches batches;
scanner.NextBatch(batches);  // or CollectAllBatches(batches)
for (const auto& batch : batches) {
    std::cout << "rows: " << batch->NumRows() << std::endl;
}

该 limit 是按 bucket 生效的;要覆盖多 bucket 的表,请扫描每个 bucket。

Fluss 中文文档