Skip to content

分区表 ​

分区表根据分区列的值将数据分布到各个分区中,从而实现高效的数据组织与查询。Log table 和 primary key table 都支持分区。

分区 Log Table ​

创建分区 Log Table ​

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

auto descriptor = fluss::TableDescriptor::NewBuilder()
    .SetSchema(schema)
    .SetPartitionKeys({"dt", "region"})
    .SetBucketCount(3)
    .Build();

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

写入分区 Log Table ​

在每一行中包含分区列的值,客户端会自动将记录路由到正确的分区。写入一个不存在的分区时会自动创建该分区,除非 writer_dynamic_create_partition_enabled 为 false,此时该写入会以 PARTITION_NOT_EXISTS 失败。服务端仍会校验被创建的分区,例如其取值,以及从 Fluss 1.0 起是否位于自动分区表的保留期内。在启用幂等性(默认)的情况下,一条已发出的写入若其目标分区在此期间被删除,会以 PARTITION_NOT_EXISTS 失败而非重新创建该分区;再次写入则会创建该分区。

注意

在 Fluss 1.0 之前,服务端也会在自动分区表的保留期之外创建分区。写入会成功,但该分区及其数据行会在下一次自动分区检查(auto-partition.check.interval,默认 10 分钟)时被删除。

说明

与 Java 客户端不同,本客户端尚不支持历史分区访问。在启用 table.datalake.historical-partition.enabled 的情况下,对已过期分区的写入不会被路由到表的 __historical__ 分区,而是按其他缺失分区同样处理。

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

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

fluss::GenericRow row;
row.SetInt32(0, 1);
row.SetString(1, "user_login");
row.SetString(2, "2024-01-15");
row.SetString(3, "US");
writer.Append(row);
writer.Flush();

从分区 Log Table 读取 ​

对于分区表,请使用分区感知的订阅方法。

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

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

// Subscribe to individual partitions
for (const auto& pi : partition_infos) {
    scanner.SubscribePartitionBuckets(pi.partition_id, 0, 0);
}

fluss::ScanRecords records;
scanner.Poll(5000, records);

for (const auto& rec : records) {
    std::cout << "bucket_id=" << rec.bucket_id
              << " offset=" << rec.offset << std::endl;
}

// Or batch-subscribe to all partitions at once
fluss::LogScanner batch_scanner;
table.NewScan().CreateLogScanner(batch_scanner);

std::vector<fluss::PartitionBucketSubscription> subs;
for (const auto& pi : partition_infos) {
    subs.push_back({pi.partition_id, 0, 0});
}
batch_scanner.SubscribePartitionBuckets(subs);

取消订阅某个 partition bucket:

cpp
// Stop receiving records from a specific partition bucket
scanner.UnsubscribePartition(partition_infos[0].partition_id, 0);

管理分区 ​

cpp
// Create a partition
admin.CreatePartition(table_path, {{"dt", "2024-01-15"}, {"region", "EMEA"}}, true);

// List partitions
std::vector<fluss::PartitionInfo> partition_infos;
admin.ListPartitionInfos(table_path, partition_infos);

// Query partition offsets
std::vector<int32_t> bucket_ids = {0, 1, 2};
std::unordered_map<int32_t, int64_t> offsets;
admin.ListPartitionOffsets(table_path, "2024-01-15$US",
                           bucket_ids, fluss::OffsetSpec::Latest(), offsets);

分区 Primary Key Table ​

分区 KV table 将分区与 primary key 操作结合在一起。分区列必须是主键的一部分。

创建分区 Primary Key Table ​

cpp
auto schema = fluss::Schema::NewBuilder()
    .AddColumn("user_id", fluss::DataType::Int())
    .AddColumn("region", fluss::DataType::String())
    .AddColumn("zone", fluss::DataType::BigInt())
    .AddColumn("score", fluss::DataType::BigInt())
    .SetPrimaryKeys({"user_id", "region", "zone"})
    .Build();

auto descriptor = fluss::TableDescriptor::NewBuilder()
    .SetSchema(schema)
    .SetPartitionKeys({"region", "zone"})
    .SetBucketCount(3)
    .Build();

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

写入分区 Primary Key Table ​

与 log table 相同,默认情况下 upsert 一个不存在的分区会自动创建它。

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

// 可选:默认情况下 upsert 会创建缺失的分区
admin.CreatePartition(table_path, {{"region", "APAC"}, {"zone", "1"}}, true);
admin.CreatePartition(table_path, {{"region", "EMEA"}, {"zone", "2"}}, true);
admin.CreatePartition(table_path, {{"region", "US"}, {"zone", "3"}}, true);

fluss::UpsertWriter writer;
table.NewUpsert().CreateWriter(writer);

auto row = table.NewRow();
row.Set("user_id", 1001);
row.Set("region", "APAC");
row.Set("zone", static_cast<int64_t>(1));
row.Set("score", static_cast<int64_t>(1234));
writer.Upsert(row);
writer.Flush();

在分区表中查找记录 ​

Lookup 需要包含分区列在内的所有主键列。

注意: 不支持对分区 primary key table 进行扫描。请改用 lookup 操作。

cpp
fluss::Lookuper lookuper;
table.NewLookup().CreateLookuper(lookuper);

auto pk = table.NewRow();
pk.Set("user_id", 1001);
pk.Set("region", "APAC");
pk.Set("zone", static_cast<int64_t>(1));

fluss::LookupResult result;
lookuper.Lookup(pk, result);
if (result.Found()) {
    std::cout << "score=" << result.GetInt64(3) << std::endl;
}

Fluss 中文文档