关于 dbt-transformation-patterns
面向使用 dbt 构建和维护分析管道的团队,介绍数据转换项目的组织方式,涵盖模型分层、测试、文档和增量处理。
主要功能
- 01按暂存、中间层和集市组织模型
- 02按模型层应用命名约定
- 03规划数据质量测试
- 04编写模型和血缘文档
- 05对大型数据集使用增量模型
适用场景
- 01搭建 dbt 项目结构
- 02构建分层数据转换管道
- 03为分析模型添加测试和文档
- 04增量处理大型表
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ wshobson/ agents --skill dbt-transformation-patterns安装后在 Claude Code 中输入/dbt-transformation-patterns确认已加载。
附带文件references/details.md
详细介绍
dbt 转换模式
适用于生产环境的 dbt(data build tool)模式,涵盖模型组织、测试策略、文档和增量处理。
何时使用本技能
- 使用 dbt 构建数据转换管道
- 将模型组织为 staging、intermediate 和 marts 层
- 实施数据质量测试
- 为大型数据集创建增量模型
- 记录数据模型和血缘关系
- 搭建 dbt 项目结构
核心概念
1. 模型分层(Medallion 架构)
sources/ Raw data definitions
↓
staging/ 1:1 with source, light cleaning
↓
intermediate/ Business logic, joins, aggregations
↓
marts/ Final analytics tables
2. 命名约定
| 层 | 前缀 | 示例 |
|---|---|---|
| Staging | stg_ | stg_stripe__payments |
| Intermediate | int_ | int_payments_pivoted |
| Marts | dim_、fct_ | dim_customers、fct_orders |
快速开始
# dbt_project.yml
name: "analytics"
version: "1.0.0"
profile: "analytics"
model-paths: ["models"]
analysis-paths: ["analyses"]
test-paths: ["tests"]
seed-paths: ["seeds"]
macro-paths: ["macros"]
vars:
start_date: "2020-01-01"
models:
analytics:
staging:
+materialized: view
+schema: staging
intermediate:
+materialized: ephemeral
marts:
+materialized: table
+schema: analytics
# Project structure
models/
├── staging/
│ ├── stripe/
│ │ ├── _stripe__sources.yml
│ │ ├── _stripe__models.yml
│ │ ├── stg_stripe__customers.sql
│ │ └── stg_stripe__payments.sql
│ └── shopify/
│ ├── _shopify__sources.yml
│ └── stg_shopify__orders.sql
├── intermediate/
│ └── finance/
│ └── int_payments_pivoted.sql
└── marts/
├── core/
│ ├── _core__models.yml
│ ├── dim_customers.sql
│ └── fct_orders.sql
└── finance/
└── fct_revenue.sql
详细模式与完整示例
详细的模式文档位于 references/details.md。当上述导航层级不够用时,请阅读该文件。
最佳实践
推荐做法
- 使用 staging 层 - 数据只清洗一次,处处可用
- 积极测试 - 非空、唯一、关系
- 记录一切 - 列描述、模型描述
- 使用增量 - 适用于超过 100 万行的表
- 版本控制 - 将 dbt 项目纳入 Git
避免做法
- 不要跳过 staging - 原始数据 → mart 是技术债务
- 不要硬编码日期 - 使用
{{ var('start_date') }} - 不要重复逻辑 - 提取到宏中
- 不要在生产环境测试 - 使用 dev target
- 不要忽视新鲜度 - 监控源数据
评论
支持 Markdown