如何用 DBT-Presto 插件实现增量 Materialized View?
解读
国内数据团队普遍把 DBT 当成“ETL 编排器”,但 Presto/Trino 本身并不支持真正的“增量物化视图(Incremental Materialized View)”。因此面试官想确认三件事:
- 你是否知道 Presto 的存储引擎层(Hive、Iceberg、Delta)才有“表格式版本”能力,而 Presto 引擎层只有 INSERT OVERWRITE 或 MERGE 语义;
- 你是否能用 DBT 的 incremental 策略,把“增量分区”或“变更文件”翻译成 Presto 可执行的 SQL,并保证幂等、可重跑;
- 你是否能在高并发场景下解决“小文件、元数据膨胀、一致性”问题,达到金融级要求。
一句话:让 DBT 在 Presto 上“模拟”增量刷新,而不是依赖数据库原生的 MV 功能。
知识点
- Presto 增量写语义
- 对 Hive 表:只能 INSERT OVERWRITE PARTITION,或 INSERT INTO 追加;
- 对 Iceberg:支持 MERGE INTO、DELETE、UPDATE,且支持 snapshot 隔离;
- 对 Delta Lake:通过 manifest 表亦可 MERGE,但需先注册 manifest 文件。
- DBT incremental 模型
- 策略:append、merge、insert_overwrite;
- 变量:is_incremental()、this、ref()、adapter.get_incremental_strategy();
- 钩子:pre_hook、post_hook、on_schema_change。
- 分区裁剪与文件裁剪
- 用 where 子句显式绑定分区列,防止全表扫描;
- Iceberg 可用 “_file” 元数据列做文件级去重。
- 并发与一致性
- Presto 无事务,需要靠表格式或外部锁(Hive Metastore 锁、Ranger 锁);
- 金融级要求:双跑校验、回滚分区、数据质量 gate。
- 小文件治理
- dbt run 后加 dbt-labs 的“dbt-trino-utils”宏,自动触发 Iceberg expire_snapshots 与 rewrite_files;
- 设置 writer_min_size=128 MB,writer_max_memory=2 GB,减少碎片化。
答案
以 Iceberg 表为例,给出生产级增量模型模板,可直接放进 dbt-pre-trino 项目。
步骤 1:profiles.yml 里把 session 属性写死,保证 MERGE 语义可用
presto_prod:
target: prod
outputs:
prod:
type: trino
method: ldap
user: dbt_prod
password: "{{ env_var('DBT_PRESTO_PWD') }}"
host: presto-broker.xxx.com
port: 443
http_scheme: https
catalog: iceberg
schema: dwh
session_properties:
merge_cardinality_check: 'false'
iceberg.merge_mode: 'merge-on-read'
步骤 2:models/marts/fact_transaction_incr.sql
{{
config(
materialized='incremental',
unique_key='transaction_id',
incremental_strategy='merge',
on_schema_change='sync_all_columns',
partition_by=['dt'],
pre_hook="
{% if is_incremental() %}
-- 1. 先拿到上次最大 dt,防止重复写
set session last_max_dt = (
select max(dt) from {{ this }}
);
{% endif %}
",
post_hook="
-- 2. 合并后立刻清理小文件
call iceberg.system.rewrite_files(
schema => '{{ this.schema }}',
table_name => '{{ this.name }}',
strategy => 'binpack',
target_file_size_mb => 128
);
"
)
}}
with delta as (
select
transaction_id,
amount,
dt,
etl_time
from {{ source('ods', 'transaction_delta') }} -- Kafka->Iceberg 的增量表
{% if is_incremental() %}
where dt > date '{{ var("last_max_dt") }}' -- 分区增量
and etl_time > (select max(etl_time) from {{ this }}) -- 行级去重
{% endif %}
)
select * from delta
步骤 3:命令行调用
dbt run --select fact_transaction_incr --vars '{"last_max_dt": 2024-06-18}'
说明
- 采用 merge 策略,Presto 会生成 MERGE INTO SQL,Iceberg 在 snapshot 级别保证读写隔离;
- pre_hook 把“上次最大分区”写进 session 变量,实现幂等重跑;
- post_hook 立即触发 rewrite_files,防止小文件堆积;
- unique_key=transaction_id 让 DBT 自动生成 update 匹配条件,达到“增量更新”效果;
- 若源端为 Hive,则把 incremental_strategy 改成 insert_overwrite,并在 SQL 里显式覆盖分区:
insert overwrite table {{ this }} partition(dt)
select ... from delta;
即可。
拓展思考
- 如果源端是 MySQL binlog->Kafka->Iceberg,且要求分钟级延迟,可把 DBT 的调度粒度降到 5 min,但需解决“迟到数据”问题:在模型里增加“水位线”字段,每次 merge 时往前多取 30 min 分区,再用 etl_time 去重。
- 当并发写入同一 Iceberg 表时,Presto 的 MERGE 可能报“concurrent modification”异常。生产上可用 Hive Metastore 锁或 AWS Glue pessimistic lock,也可把 dbt 任务拆成“写临时表→原子交换”两步,降低锁冲突。
- 国内金融公司常要求“双跑对比”:增量模型跑完后,自动把当天分区与离线 T+1 全量表做 checksum。可在 post_hook 里调用自己开发的 UDF:
select
sum(crc32(cast(transaction_id as varchar))) as chk1,
sum(amount) as chk2
from {{ this }} where dt = current_date
对比结果落库,diff>0 即报警。 4. 未来 Trino 社区正在做“true incremental materialized view”(FLIP-279),把增量状态保存在 catalog 里。作为 PrestoDB 开发,你可以提前参与:在 trino-parser 里新增 REFRESH INCREMENTAL MATERIALIZED VIEW 语法,在 trino-main 里实现 IncrementalRefreshNode,把变更文件直接映射成 PlanNode,绕过 DBT 层的 MERGE,性能可再提升 3-5 倍。面试时抛出这一路线图,可瞬间拉高技术深度。