iT邦幫忙

2024 iThome 鐵人賽

DAY 13
0
DevOps

我獨自升級:從水管工走向 DataOps系列 第 13

【Day 13】DBT 經典案例 Jaffle Shop (2) - 轉換步驟指南

  • 分享至 

  • xImage
  •  

前言

從昨天的 jaffle 專案環境建置中,應該可以發現整個專案是在 venv 虛擬環境中運行,但 postgres 卻是在 docker 當中,主要是根據原來官方提供的 jaffle shop,盡可能做最小的改動以順利運行即可。
其實我和官方的差異只是開一個 db,整體流程都一樣,就是將 seeds 資料導入,經過 dbt 的資料轉換(transform),產出需要的 view 和 table,轉換過程 staging 和 marts 層都是在等等開的 postgresql 當中,就讓我們接著下去吧~

要將所有環境都開在 docker 當然也是沒問題的~/images/emoticon/emoticon07.gif

docker-compose.yaml 說明

version: "3.9"

services:
  postgres:
    image: postgres:13
    environment:
      POSTGRES_USER: dbt
      POSTGRES_PASSWORD: dbt
      POSTGRES_DB: dbt
    volumes:
      - postgres-db-volume:/var/lib/postgresql/dbt_data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5
volumes:
  postgres-db-volume:
  • services 定義了唯一的服務 postgres,使用 官方 PostgreSQL 13 版 作為 Docker image
  • environment 區塊設定 PostgreSQL 的環境變數,用來配置資料庫使用者、密碼和資料庫名稱
  • volume 用來持久化資料庫的數據。卷確保即使容器停止或刪除,數據也不會丟失。
  • posts 是為了對應容器的連接埠,格式是 "外部埠:內部埠"
  • healthcheck 檢查服務健康狀況的配置
    • interval:5s:每 5 秒檢查一次 PostgreSQL 的健康狀態。
    • timeout:5s:每次健康檢查的超時時間設為 5 秒。如果 PostgreSQL 在 5 秒內沒有回應,則視為健康檢查失敗。
    • retries:5:如果健康檢查連續失敗 5 次,則判斷 PostgreSQL 服務不健康。

https://ithelp.ithome.com.tw/upload/images/20240927/20135427eMjawoiadZ.png

可以從 docker ps 看到 status 欄位是 healthy

dbt 資料轉換步驟

1. dbt debug:檢查環境連接

目的是檢查 dbt 的連接配置、資料庫連接、憑證、資料源等是否設置正確。

執行結果

dbt debug

https://ithelp.ithome.com.tw/upload/images/20240927/20135427IdTz8Ty5sr.png

從圖片可以確認 configuration 和 Connection 都沒問題!

2. dbt seed:載入靜態資料

將本地的 CSV 文件上傳到資料庫,作為靜態資料來源。這些文件稱為 "seed" 資料,用來在模型中參考。

執行結果

dbt seed

https://ithelp.ithome.com.tw/upload/images/20240927/20135427mxfdATrRMN.png

我通常實務上比較少用,通常是 local 測試或是針對沒有測試環境 DB 的情況下才會用 dbt seed 載入資料

3. dbt compile:運行模型並寫入資料

將包含 Jinja 模板語法的 dbt 模型編譯成 SQL 查詢且不執行,有助於檢查 SQL 的正確性。

執行結果

dbt compile

https://ithelp.ithome.com.tw/upload/images/20240927/20135427gd1WApJKrq.png

執行完 compile 後,也可以直接在 target/ 資料夾中,找到 compiled/ 資料夾,在其中查看編譯過後的 SQL 檔案
https://ithelp.ithome.com.tw/upload/images/20240927/20135427RTpAWaJrLn.png

4. dbt run:運行模型並寫入資料

在運行模型或更新資料庫時,執行 dbt run,實際執行 SQL 轉換,並將轉換後的結果寫入資料庫。

執行結果

dbt run

https://ithelp.ithome.com.tw/upload/images/20240927/20135427xwVtLjf0VA.png

5. dbt test:驗證資料正確性

可以在 model 當中的 schema.yml 中查看 test 設定的細節,以下方 stg_orders 為例,針對 order_id 欄位檢查 uniquenot_null ,針對 status 欄位檢查值是否都是 values 列表當中的這五種。

  - name: stg_orders
    columns:
      - name: order_id
        tests:
          - unique
          - not_null
      - name: status
        tests:
          - accepted_values:
              values: ['placed', 'shipped', 'completed', 'return_pending', 'returned']

執行結果

dbt test

https://ithelp.ithome.com.tw/upload/images/20240927/2013542797N2x2GJ07.png

6. dbt docs generate : 生成文件

可以生成資料庫表和模型的文件,幫助你或團隊理解的資料流程,生成後的文件和關聯圖,可以通過 dbt docs serve 來查看。

通常會稱為 dbt lineage,lineage 是家系、沿襲的意思,就是透過這個圖可以知道資料上下游關係,其中類型也是 airflow 有提到的 DAGs (directed acyclic graphs,有向無環圖),就是單一方向的節點連結

執行結果

dbt docs generate

https://ithelp.ithome.com.tw/upload/images/20240927/20135427NC8qE0Uz5U.png

7. dbt docs serve:local 開啟檔案查看

直接執行就會開在 localhost 的 8080 port,在正式開發通常會另外用一個靜態服務的網站,後續方便查看和追蹤目前運行中的 dbt 流程內容,也能確認資料欄位和細節。

執行結果

dbt docs serve

https://ithelp.ithome.com.tw/upload/images/20240927/20135427oHYqVuUPN9.png

  • 開啟 http://localhost:8080/#!/overview
    https://ithelp.ithome.com.tw/upload/images/20240927/20135427d47Iz2phdN.png
  • 點擊右下方就能查看圖表
    https://ithelp.ithome.com.tw/upload/images/20240927/20135427Y1gPl7lZrL.png
  • 直接點擊 model 或 seeds 可查看欄位和 SQL 語法
    https://ithelp.ithome.com.tw/upload/images/20240927/20135427eR5pc5i3YX.pnghttps://ithelp.ithome.com.tw/upload/images/20240927/20135427iGkyOluHr4.png

其他 dbt 語法

  • dbt clean:清理 target/,刪除生成的文件和資料,通常是重新執行前會需要清除舊的紀錄。
  • dbt snapshot:如果你使用快照功能,可以在這裡拍攝資料的歷史快照,通常用於緩慢變更維度(SCD)。
  • dbt deps:如果你使用 dbt packages,這個命令可以安裝套件。

結語

明天還會再說明一下 jaffle shop 轉換後的資料,和如何查找 postgresql 資料,後天就會開始將 airflow 搭配 dbt 使用啦~/images/emoticon/emoticon41.gif


上一篇
【Day 12】DBT 經典案例 Jaffle Shop (1) - 環境安裝與 model 分層
下一篇
【Day 14】DBT 經典案例 Jaffle Shop (3) - DB CLI 和 GUI 檢查資料
系列文
我獨自升級:從水管工走向 DataOps21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言