如何使 dbt 中的增量加载每次运行时都只更新上次运行时的新行?

jef*_*lls 2 jinja2 dbt

如何使 dbt 中的增量加载每次运行时都只更新上次运行时的新行?

{% if is_incremental() %}

    /* code */

    {% endif %}
Run Code Online (Sandbox Code Playgroud)

gsc*_*ott 6

用于执行此操作的两个主要资源已包含在 dbt 文档中:

  1. 如何在 dbt 中构建增量模型: https://docs.getdbt.com/docs/building-a-dbt-project/building-models/configuring-incremental-models/

  2. bigquery 特有的增量模型: https://docs.getdbt.com/reference/resource-configs/bigquery-configs/#merge-behavior-incremental-models

该模型很可能看起来像这样:

{{
    config(
        materialized='incremental'
    )
}}

select <columns>
from <my_table>
{% if is_incremental() %}
  where <my_table>.<record_update_timestamp> >= (
      select max(<my_table>.<record_update_timestamp>) from {{ this }}
  )
  {% endif %}
Run Code Online (Sandbox Code Playgroud)

文档中的完整示例: https://docs.getdbt.com/docs/building-a-dbt-project/building-models/configuring-incremental-models/#defining-a-uniqueness-constraint-optional