Community研究与数据分析github.com

Unknown-333/building-dbt-models

构建结构良好的 dbt 模型——涵盖分层(staging/intermediate/marts)、ref() 和 source() 引用、物化方式以及带有正确策略的增量模型。

building-dbt-models 是什么?

该技能专为在 dbt 项目中创建或重构 `.sql` 模型而设计,帮助开发者做出正确的架构决策。它涵盖了 dbt 模型的核心实践:将模型正确放置在 staging(轻量清洗)、intermediate(可复用业务逻辑)和 marts(最终维度模型)三层中;强制使用 `ref()` 和 `source()` 函数,避免硬编码表名以维护 DAG 和依赖关系;根据数据规模和刷新需求选择合适的物化方式(view / table / incremental / ephemeral)。对于大型追加或更新表,技能重点讲解了增量模型的实现,包括设置 `unique_key`、使用 `is_incremental()` 过滤以及添加回看窗口(lookback)以应对延迟到达的数据,确保重跑幂等性。该技能适用于多种 dbt 平台(如 Snowflake、BigQuery、Spark),并提供了常见陷阱提醒,如缺少唯一键导致重复、没有回看窗口导致数据丢失等。配合 schema.yml 中的测试,可有效提升模型质量和可维护性。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Unknown-333/awesome-data-engineering-skills/tree/main/skills/building-dbt-models

Installed? Explore more 研究与数据分析 skills: affaan-m/uspto-database, affaan-m/scholar-evaluation, affaan-m/literature-review · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

构建 dbt 模型

使用时机

  • 在 dbt 项目中创建或重构 .sql 模型。
  • 决定物化方式(view / table / incremental / ephemeral)。
  • 构建分层结构(staging → intermediate → marts)。
  • 为大型、不断增长的表编写增量模型。
  • 不要用于测试编写(使用 testing-dbt-projects)或运行失败处理(使用 debugging-dbt-runs)。

工作流程

- [ ] 将模型放置在正确的层级(staging / intermediate / marts)
- [ ] 仅通过 ref() / source() 引用上游——绝不使用硬编码名称
- [ ] 根据数据规模和刷新需求选择物化方式
- [ ] 对于增量模型,设置 unique_key + is_incremental() 过滤器
- [ ] 添加 schema.yml 条目并包含测试
  1. 分层放置。 staging/ = 每个源表一个模型,进行轻量重命名和类型转换,物化为视图。intermediate/ = 可复用的业务逻辑。marts/ = 最终维度模型,供 BI 使用,物化为表。
  2. 正确引用。 使用 {{ ref('stg_orders') }} 和 {{ source('shop', 'orders') }},让 dbt 构建 DAG 并管理环境。永远不要编写原始 schema.table。
  3. 选择物化方式: view(廉价、始终最新、小规模)、table(快速读取、每次运行重建)、incremental(大型追加/更新表)、ephemeral(内联 CTE,无对象)。
  4. 增量模型仅处理新增或变更的行。

模式

Staging 模型——每个源一个,精简且一致:

-- models/staging/shop/stg_orders.sql
with source as (select * from {{ source('shop', 'orders') }})
select
    order_id,
    customer_id,
    cast(order_ts as timestamp) as ordered_at,
    round(amount_cents / 100.0, 2) as amount
from source

增量模型——过滤新行并设置幂等合并键:

{{ config(materialized='incremental', unique_key='order_id',
          incremental_strategy='merge') }}

select * from {{ ref('stg_orders') }}
{% if is_incremental() %}
  -- 仅选择比已加载数据更新的行,并包含回看窗口以捕获延迟数据
  where ordered_at >= (select coalesce(max(ordered_at), '1900-01-01') from {{ this }})
                      - interval '3 days'
{% endif %}

unique_key + merge 使得重新运行具有幂等性;回看窗口用于捕获延迟到达的行。在 BigQuery/Spark 上,建议在日期分区上使用 insert_overwrite。

常见陷阱

  • 硬编码表名而非 ref()/source()——这会破坏 DAG、血缘关系和环境切换。
  • 增量模型缺少 unique_key——重新运行会追加重复数据。
  • 增量过滤使用 max(id) 且无回看窗口——静默丢弃延迟数据。
  • 在 staging 层加入业务逻辑——保持 staging 层精简;连接和聚合应放在 intermediate/marts 层。
  • 所有模型都物化为 table——浪费仓库时间;小/廉价模型使用视图,大型模型使用增量。
  • 一个巨型模型——拆分为中间步骤以提高可测试性和复用性。

参考

Individual skills in this repo

This repo contains 9 individual skills — each has its own dedicated page.

Unknown-333/authoring-airflow-dags

Write production-grade Apache Airflow DAGs using the TaskFlow API — idempotent tasks, correct scheduling and catchup, retries/SLAs, connections/variables, and avoiding top-level code. Use when creating or reviewing Airflow DAGs, scheduling pipelines, wiring task dependencies, configuring retries/backfills, or fixing non-idempotent tasks.

Unknown-333/building-dagster-assets

Build Dagster pipelines using software-defined assets — asset dependencies, partitions, resources and IO managers, asset checks, and schedules/sensors. Use when creating Dagster assets or jobs, modeling data as assets, adding partitions or backfills, wiring resources/IO managers, or migrating from task-based orchestration to assets.

Unknown-333/building-feature-pipelines

Build ML feature pipelines and feature stores — point-in-time-correct joins to avoid label leakage, offline/online parity, feature freshness and backfills, and materialization with tools like Feast. Use when engineering features for ML, preventing train/serve skew or data leakage, building a feature store, or backfilling historical features for training.

Unknown-333/building-iceberg-tables

Design and operate Apache Iceberg tables — partitioning and hidden partitioning, partition/schema evolution, snapshots and time travel, compaction and small-file cleanup, and MERGE/upsert for lakehouse tables on Spark, Flink, Trino, or Snowflake. Use when creating or maintaining Iceberg tables, choosing partitioning, evolving schema/partitions, or fixing small-file and metadata bloat.

Unknown-333/building-ingestion-pipelines

Build batch and incremental data ingestion (extract-load) pipelines — full vs incremental extraction, change data capture (CDC), watermarks and high-water marks, API pagination and rate limits, and choosing managed EL tools (Fivetran, Airbyte) vs custom code. Use when ingesting data from databases, APIs, files, or SaaS into a warehouse/lake, or designing incremental extraction and CDC.

Unknown-333/building-kafka-consumers

Build reliable Apache Kafka consumers and producers — consumer groups and partition assignment, offset commit strategy, at-least-once vs exactly-once, idempotent/transactional producers, rebalancing, and dead-letter handling. Use when writing Kafka consumers/producers, configuring offset commits or consumer groups, tuning throughput, or handling rebalances and poison messages.

Unknown-333/debugging-data-pipelines

Systematically root-cause data pipeline failures and data incidents — job errors, wrong or missing data, duplicates, and freshness misses — by tracing lineage upstream, isolating the failing stage, reconciling against source, and planning a safe fix and backfill. Use when a pipeline fails, numbers look wrong, data is missing or duplicated, a dashboard is stale, or a stakeholder reports a data discrepancy.

Unknown-333/designing-backfills-and-replays

Plan and run safe data backfills and replays — idempotent reprocessing of historical windows, partition-by-partition execution, isolating backfill compute from production, verifying results, and avoiding double-counting or changed history. Use when backfilling a new or fixed model, reprocessing after a bug, replaying events, or loading history for a new pipeline without corrupting existing data.

Unknown-333/designing-data-contracts

Define and enforce data contracts between producers and consumers — explicit schema, semantics, ownership, SLAs, and versioning — to prevent silent upstream changes from breaking downstream pipelines. Use when a producer schema change could break consumers, defining an interface between teams/services and the warehouse, or adding schema enforcement at ingestion.

相关技能