Virtual Tables
Fluss 中的虚拟表(virtual tables)是由系统生成的表,用于访问元数据、变更数据以及其他系统信息,而无需额外存储数据。通过在基础表名后追加后缀(例如 $changelog)来访问它们。
Fluss 支持以下虚拟表类型:
| 虚拟表 | 后缀 | 描述 | 支持的表 |
|---|---|---|---|
| Changelog | $changelog | 提供包含元数据的原始 changelog 流访问 | Primary Key Tables、Log Tables |
| Binlog | $binlog | 提供带有 before/after 元数据的 binlog 格式 | 仅 Primary Key Tables |
| Buckets | $buckets | 提供副本分布与 leader/ISR 元数据 | Primary Key Tables、Log Tables |
Flink 运行时模式
$changelog 和 $binlog 都可以在 Flink 流式(streaming)或批(batch)运行时下查询。 $buckets 表即使在流式运行时模式下,也始终返回一个有界的元数据快照。
| 运行时模式 | 行为 |
|---|---|
| Streaming | 从配置的起始位置开始读取,并持续等待新的变更。 |
| Batch | 读取一个有界的区间,在到达每个 bucket 的停止偏移量后终止。 |
对于批扫描,Fluss 会从 scan.startup.mode 解析起始偏移量,并在有界分片(bounded splits)初始化时,将每个 bucket 的最新偏移量捕获为停止偏移量。扫描读取区间 [起始偏移量, 停止偏移量)。在某个 bucket 的停止偏移量被捕获之后追加到该 bucket 的记录,不会被包含在此次批查询中。
说明
批扫描不会等待未来的变更。因此,scan.startup.mode = 'latest' 通常对流式查询很有用,但对于批运行时的历史重放通常没有用处。
虚拟表目前直接从 Fluss 日志读取,不会从 lake 快照重建历史变更。full 和 earliest 模式从 Fluss 中仍可用的最早日志偏移量开始。批扫描始终使用分片初始化期间捕获的最新偏移量作为停止偏移量;无法单独配置停止偏移量。
例如,下面这条查询会重放 changelog 的一段历史部分,并作为有界 Flink 作业终止:
SET 'execution.runtime-mode' = 'batch';
SELECT _change_type, _log_offset, order_id, amount
FROM orders$changelog
/*+ OPTIONS(
'scan.startup.mode' = 'timestamp',
'scan.startup.timestamp' = '1705312200000'
) */
WHERE _commit_timestamp < TO_TIMESTAMP_LTZ(1705398600000, 3)
ORDER BY _log_offset
LIMIT 100;注意
在 Flink 1.18 上,当会话时区不是 UTC 时,对 _commit_timestamp(或任何其他 TIMESTAMP_LTZ 列)的谓词会返回错误结果。Flink 1.18 在过滤器下推到 source 之后,会使用会话时区而非 UTC 重建该谓词,从而将字面量按时区偏移量平移,通常导致查询返回空结果。这是 FLINK-35318,已在 Flink 1.19.2 和 1.20.0 中修复,且从未回移植到 1.18。在 Flink 1.18 上,请避免使用 TIMESTAMP_LTZ 谓词,或将 table.local-time-zone 设置为 UTC。
Buckets Table
$buckets 虚拟表提供对表所属每个 bucket 当前副本分布与 leader/ISR 状态的只读访问。在基础表名后追加 $buckets:
SELECT * FROM orders$buckets;Schema
| Column | Type | Description |
|---|---|---|
table_path | STRING NOT NULL | 基础表路径 |
table_id | BIGINT NOT NULL | 唯一的 table ID |
partition_id | BIGINT | 分区 ID,非分区表为 NULL |
partition_name | STRING | 分区名,非分区表为 NULL |
bucket_id | INT NOT NULL | bucket ID |
leader_id | INT | leader TabletServer ID,未选举出 leader 时为 NULL |
leader_epoch | INT | leader 的 epoch,未选举出 leader 时为 NULL |
bucket_epoch | INT | leader/ISR 状态的代次(generation);NULL 表示遗留元数据,-1 表示不存在 leader/ISR 状态 |
replicas | ARRAY<INT NOT NULL> NOT NULL | 副本所在的 TabletServer ID 列表 |
isr | ARRAY<INT NOT NULL> NOT NULL | 同步副本(in-sync replica)所在的 TabletServer ID 列表 |
对于分区表,可以使用普通 SQL 谓词来筛选所需的分区或 bucket:
SELECT partition_name, bucket_id, leader_id, replicas, isr
FROM orders$buckets
WHERE partition_name = 'cn$2026-09-16';当前实现会在 Flink 计算谓词之前获取完整的 bucket 元数据快照,因此尚不支持对大型分区表进行谓词下推。
Changelog Table
$changelog 虚拟表提供对表原始 changelog 流的只读访问,让你可以审计并处理所有数据变更及其关联元数据。
访问 Changelog
要访问表的 changelog,请在表名后追加 $changelog:
SELECT * FROM my_table$changelog;Schema
changelog 虚拟表的 schema 在原始表列之前,包含了三个元数据列:
| Column | Type | Description |
|---|---|---|
_change_type | STRING NOT NULL | 变更操作的类型(参见 Change Types) |
_log_offset | BIGINT NOT NULL | 在日志中的偏移量位置 |
_commit_timestamp | TIMESTAMP_LTZ(3) NOT NULL | 变更被提交时的时间戳 |
其后跟着基础表的所有列。
Change Types
_change_type 列表示数据修改的类型:
Primary Key Tables
对于 Primary Key Tables,支持以下变更类型:
| Change Type | Description |
|---|---|
insert | 插入了一条新行 |
update_before | 被更新行的旧值(回撤) |
update_after | 被更新行的新值 |
delete | 删除了一行 |
Log Tables
对于 Log Tables(仅追加),只使用一种变更类型:
| Change Type | Description |
|---|---|
insert | 向日志中插入了一条新行 |
示例
考虑一张跟踪用户订单的 Primary Key Table:
-- Create a primary key table
CREATE TABLE orders (
order_id INT NOT NULL,
customer_name STRING,
amount DECIMAL(10, 2),
PRIMARY KEY (order_id) NOT ENFORCED
) WITH ('bucket.num' = '1');
-- Insert a record
INSERT INTO orders VALUES (1, 'Rhea', 100.00);
-- Update the record
INSERT INTO orders VALUES (1, 'Rhea', 150.00);
-- Delete the record
DELETE FROM orders WHERE order_id = 1;
-- Query the changelog
SELECT * FROM orders$changelog;Output:
+---------------+-------------+---------------------+----------+---------------+---------+
| _change_type | _log_offset | _commit_timestamp | order_id | customer_name | amount |
+---------------+-------------+---------------------+----------+---------------+---------+
| insert | 0 | 2024-01-15 10:30:00 | 1 | Rhea | 100.00 |
| update_before | 1 | 2024-01-15 10:35:00 | 1 | Rhea | 100.00 |
| update_after | 2 | 2024-01-15 10:35:00 | 1 | Rhea | 150.00 |
| delete | 3 | 2024-01-15 10:40:00 | 1 | Rhea | 150.00 |
+---------------+-------------+---------------------+----------+---------------+---------+启动模式
changelog 表支持不同的启动模式来控制读取的起始位置:
| Mode | Description |
|---|---|
full | 从日志开头开始读取(默认) |
earliest | 从日志开头开始读取 |
latest | 使用当前日志末尾作为起始位置 |
timestamp | 从指定的时间戳开始读取(自纪元起的毫秒数) |
-- Read from the beginning (default full mode)
SELECT * FROM orders$changelog;
-- Explicitly read from the earliest offset
SELECT * FROM orders$changelog /*+ OPTIONS('scan.startup.mode' = 'earliest') */;
-- Use the current log end as the starting position
SELECT * FROM orders$changelog /*+ OPTIONS('scan.startup.mode' = 'latest') */;
-- Read from a specific timestamp
SELECT * FROM orders$changelog /*+ OPTIONS('scan.startup.mode' = 'timestamp', 'scan.startup.timestamp' = '1705312200000') */;限制
- 投影(projection)、分区(partition)和谓词下推(predicate pushdown)目前尚不支持。这将在未来的版本中解决。
Binlog Table
$binlog 虚拟表提供变更数据的访问,其中每条记录都包含行的 before 和 after 镜像。它适用于以下场景:
说明
$binlog 虚拟表仅适用于 Primary Key Tables。
访问 Binlog
要访问 Primary Key Table 的 binlog,请在表名后追加 $binlog:
SELECT * FROM my_pk_table$binlog;Schema
binlog 虚拟表的 schema 包含三个元数据列,其后跟着嵌套的 before 和 after 行结构:
| Column | Type | Description |
|---|---|---|
_change_type | STRING NOT NULL | 变更操作的类型:insert、update 或 delete |
_log_offset | BIGINT NOT NULL | 在日志中的偏移量位置 |
_commit_timestamp | TIMESTAMP_LTZ(3) NOT NULL | 变更被提交时的时间戳 |
before | ROW<...> | 变更前的行值(插入时为 NULL) |
after | ROW<...> | 变更后的行值(删除时为 NULL) |
before 和 after 列是嵌套的 ROW 类型,包含基础表的所有列。
Change Typechange-types-1ypes}
| Change Type | Description | before | after |
|---|---|---|---|
insert | 插入了一条新行 | NULL | 包含新行值 |
update | 更新了一行 | 包含旧行值 | 包含新行值 |
delete | 删除了一行 | 包含被删除的行值 | NULL |
示例
考虑同一张跟踪用户订单的 Primary Key Table:
-- Create a primary key table
CREATE TABLE orders (
order_id INT NOT NULL,
customer_name STRING,
amount DECIMAL(10, 2),
PRIMARY KEY (order_id) NOT ENFORCED
) WITH ('bucket.num' = '1');
-- Insert a record
INSERT INTO orders VALUES (1, 'Rhea', 100.00);
-- Update the record
INSERT INTO orders VALUES (1, 'Rhea', 150.00);
-- Delete the record
DELETE FROM orders WHERE order_id = 1;
-- Query the binlog
SELECT * FROM orders$binlog;Output:
+--------------+-------------+---------------------+----------------------+----------------------+
| _change_type | _log_offset | _commit_timestamp | before | after |
+--------------+-------------+---------------------+----------------------+----------------------+
| insert | 0 | 2024-01-15 10:30:00 | NULL | (1, Rhea, 100.00) |
| update | 2 | 2024-01-15 10:35:00 | (1, Rhea, 100.00) | (1, Rhea, 150.00) |
| delete | 3 | 2024-01-15 10:40:00 | (1, Rhea, 150.00) | NULL |
+--------------+-------------+---------------------+----------------------+----------------------+访问嵌套字段
你可以从 before 和 after 结构中访问单个字段:
SELECT
_change_type,
_commit_timestamp,
`before`.name AS old_name,
`after`.name AS new_name
FROM users$binlog
WHERE _change_type = 'update';启动模式
binlog 表支持不同的启动模式来控制读取的起始位置:
| Mode | Description |
|---|---|
full | 从日志开头开始读取(默认) |
earliest | 从日志开头开始读取 |
latest | 使用当前日志末尾作为起始位置 |
timestamp | 从指定的时间戳开始读取(自纪元起的毫秒数) |
-- Read from the beginning (default full mode)
SELECT * FROM orders$binlog;
-- Explicitly read from the earliest offset
SELECT * FROM orders$binlog /*+ OPTIONS('scan.startup.mode' = 'earliest') */;
-- Use the current log end as the starting position
SELECT * FROM orders$binlog /*+ OPTIONS('scan.startup.mode' = 'latest') */;
-- Read from a specific timestamp
SELECT * FROM orders$binlog /*+ OPTIONS('scan.startup.mode' = 'timestamp', 'scan.startup.timestamp' = '1705312200000') */;限制
- 投影(projection)、分区(partition)和谓词下推(predicate pushdown)目前尚不支持。这将在未来的版本中解决。