dbt 來源

dbt 中級

Mike Metzger

Data Engineer

什麼是 dbt source?

  • EL 載入資料的名稱與說明
  • 有助定義資料血緣
  • 測試
  • 文件

資料來源

1 Photo by Mika Baumeister on Unsplash
dbt 中級

Sources

  • 提供資料血緣資訊
  • 描述資料在倉儲中的流向
  • 使用 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 中級

一起來練習吧!

dbt 中級

Preparing Video For Download...