dbt 源

dbt 中级

Mike Metzger

Data Engineer

什么是 dbt 源?

  • 由 EL 过程加载的数据名称与描述
  • 有助于定义数据血缘
  • 测试
  • 文档

数据源

1 Photo by Mika Baumeister on Unsplash
dbt 中级

  • 提供数据血缘信息
  • 描述数据在仓库中的流向
  • 使用 Jinja {{ source() }} 函数
    • 类似 {{ ref() }} 函数
  • 简化原始数据的访问
select * 
from
  {{ source('raw', 'orders') }}
dbt 中级

定义源

  • 在 .yml 文件中
  • 文件可为 models/model_properties.yml
    • 或目录中的任意 .yml 文件
  • 位于 sources: 段下
  • - name: 为源命名
  • tables: 下用 - name: 定义各源表
  • 选项因数据仓库类型而异,请参阅 dbt 文档
version: 2

sources:
  - name: raw
    tables:
      - name: phone_orders
      - name: web_orders
dbt 中级

访问源

  • 使用 {{ source() }}
  • {{ source(source_name, table_name) }}
  • 在编译后的查询中提供正确的表名
select * from
  {{ source('raw', 'phone_orders') }}
UNION
select * from
  {{ source('raw', 'web_orders') }}
-- dbt compiled
select * from
  'raw'.'phone_orders'
UNION
select * from
  'raw'.'web_orders'
dbt 中级

测试源

  • 可对源应用测试
  • 方法与模型测试相同
  • sources: 段定义,而非 models:
  • 写在定义源的 .yml 文件中
version: 2

sources:
  - name: raw
    tables:
      - name: phone_orders
        columns:
          - name: id
            tests:
              - not_null
              - unique
      - name: web_orders
dbt 中级

Ayo berlatih!

dbt 中级

Preparing Video For Download...