iT邦幫忙

2024 iThome 鐵人賽

DAY 11
0
DevOps

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

【Day 11】DBT 新手村入門:環境設定介紹

  • 分享至 

  • xImage
  •  

先決條件

  • Python 環境 : 建議使用 Python 3.7 以上的版本

虛擬環境設定

在專案中,我們通常會使用虛擬環境確保每個專案不會互相影響。

以下是在本地創建一個虛擬環境的步驟:

1. 創建專案資料夾

mkdir my_dbt_project

2. 進入專案資料夾

cd my_dbt_project

3. 創建虛擬環境 dbt-env

python3 -m venv dbt-env

4. 進入虛擬環境 dbt-env

source dbt-env/bin/activate

安裝 dbt-core

python3 -m pip install dbt-core

如果安裝過程中遇到錯誤,通常是因為 pip 版本過舊。你可以嘗試升級 pip 再重新安裝:python3 -m pip install --upgrade pip
升級 pip 後,再次嘗試安裝 dbt-core。這樣可以確保安裝的是與你系統相容的最新版本。

確認是否安裝成功

安裝完成後,輸入以下指令檢查 dbt 是否安裝成功:

dbt --version

應該會看到 dbt 的版本資訊,代表已經正確安裝

初始化 DBT 專案

完成 dbt 的安裝後,我們可以透過以下指令來初始化 dbt 專案:

dbt init dbt_demo

這個指令會創建一個名為 dbt_demo 的資料夾,裡面包含了基礎的專案結構。

其中有幾個重要的檔案需要特別說明一下:dbt_project.ymlprofiles.yml

dbt_project.yml

dbt_project.yml 是 dbt 專案的主要設定,從專案名稱、資料庫、資料模型位置等等。

name: 'dbt_demo'            
version: '1.0'              
config-version: 2

profile: 'dbt_demo_profile' 
target-path: "target/"      
clean-targets:             
  - "target"
  - "dbt_modules"

model-paths: ["models"]     
snapshot-paths: ["snapshots"] 
analysis-paths: ["analyses"]  
test-paths: ["tests"]         
seed-paths: ["seeds"]         
  1. name: 專案名稱,一般與資料夾名稱相同。
  2. profile: 專案所使用的 profile,對應到 profiles.yml 中的配置。
  3. model-paths: 定義資料模型的目錄路徑,默認為 models/ 資料夾。
  4. target-path: dbt 執行後的結果會在這裡。

profiles.yml

用來定義資料庫連接的相關配置。通常放在根目錄下 ~/.dbt/profiles.yml

profiles.yml 範例

dbt_demo_profile:
  target: dev
  outputs:
    dev:
      type: postgres
      host: localhost
      user: my_username
      password: my_password
      dbname: my_database
      schema: public
      threads: 4
      port: 5432
  1. target: 定義使用的環境名稱,這裡是 dev(開發環境)。
  2. type: 資料庫類型,例如 PostgreSQL、BigQuery、Snowflake 等。
  3. host: 資料庫伺服器的主機位址(例如 localhost)。
  4. user: 資料庫的使用者名稱。
  5. password: 資料庫的密碼。
  6. dbname: 連接到的資料庫名稱。
  7. schema: 資料表所屬的 schema 名稱
  8. threads: 執行 dbt 模型時使用的併發執行緒數量。

profiles.yml 設定多個環境

dbt_demo_profile:
  target: dev
  outputs:
    dev:
      ...

    prod:
      ...

這樣的設定方便我們切換到生產環境,只需要在 dbt_project.yml 中將 profile 的 target 改為 prod,即可快速切換到另一個環境,或是針對不同環境執行不同的測試。

dbt model

執行完 dbt init 後,models/ 資料夾會有範例 sql,專案結構如下:

models/
├── example/
|   ├── schema.yml
|   └── my_first_dbt_model.sql

my_first_dbt_model.sql

{{ config(materialized='table') }}

with source_data as (
    select 1 as id
    union all
    select null as id
)

select *
from source_data
where id is not null
  • {{ config(materialized='table') }}: 指定模型的 materializationtable,也可以設定為 view

dbt 較新的版本,可以使用 materialize_view/images/emoticon/emoticon12.gif

  • 透過這個 dbt model ,執行後會產生一個轉換後的 table / view

schema.yml

schema.yml 文件用來定義 model 的結構、欄位的描述、以及數據品質測試。

version: 2

models:
  - name: my_first_dbt_model
    description: "A starter dbt model"
    columns:
      - name: id
        description: "The primary key for this table"
        data_tests:
          - unique
          - not_null

  - name: my_second_dbt_model
    description: "A starter dbt model"
    columns:
      - name: id
        description: "The primary key for this table"
        data_tests:
          - unique
          - not_null
  1. version: 2:這是 schema.yml 的格式版本號,version: 2 是目前 dbt 使用的格式。
  2. models:定義模型清單。在這裡定義了兩個模型:my_first_dbt_modelmy_second_dbt_model
  3. models > name:這個 model 名稱必須與專案中 SQL 檔案的名稱一致,每個 model 都有一個 description 欄位,用來描述這個 model 的用途和內容。
  4. columns:columns 就是 model 中的欄位,這裡定義了 id 欄位,並提供了描述與測試的內容。
  5. columns > name:這是欄位的名稱,也有description來描述欄位是資料表的主鍵,在使用 model 時可以更清楚。
  6. columns > data_tests: test 部分會定義該欄位的自動化測試,常見的有 unique 檢查欄位中是否唯一,not_null 檢查欄位中的值是否有空值 null。

data_tests 也是新版才用的名稱,以前是 tests

結語

今天就先這樣吧~明天會直接開始帶 Jaffle Shop,應該會直接用 docker 來安裝,會直接提供官方的 repo ,也會直接在本地開一個 db 來測試/images/emoticon/emoticon41.gif


上一篇
【Day 10】DBT 新手村入門:架構說明
下一篇
【Day 12】DBT 經典案例 Jaffle Shop (1) - 環境安裝與 model 分層
系列文
我獨自升級:從水管工走向 DataOps21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言