iT邦幫忙

2024 iThome 鐵人賽

DAY 16
0
DevOps

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

【Day 16】用 Astronomer Cosmos 結合 dbt 和 Airflow - 專案結構與環境

  • 分享至 

  • xImage
  •  

ㄧ、前言

今天會介紹一下整體專案的結構和環境,接著看下去吧/images/emoticon/emoticon08.gif

二、專案結構

{cosmos project}
├── dags
│   └── demo_dag.py
├── dbt
│   └── jaffle_shop
│       └── ...{dbt project here}
├── plugins
│   └── include
│       ├── constants.py
│       └── profiles.py
├── docker-compose.yaml
├── Dockerfile
└── requirements.txt
  • dags/ : 其實就和過去的 airflow dags 資料夾一樣
  • dbt/ : 前幾天 dbt 的整個專案都能直接放到 {cosmos project} 裡面
  • plugins/ : 插件的資料夾算是最關鍵的,主要是設定 dbt 的 config,包含 dbt 專案目錄、profile 檔案、dbt 執行路徑等等
  • docker-compose.yaml : 也和過去的 airflow 環境一樣,都有些地方需要調整,繼續看下去~
  • Dockerfilerequirements.txt : 兩個檔案都是為了讓專案安裝模組

三、環境說明

1. 修改 docker-compose.yml

docker-compose.yml 可以去官方的介紹查看,或是直接執行下面這行:

curl -LfO 'https://airflow.apache.org/docs/apache-airflow/2.10.2/docker-compose.yaml'

(1) 安裝模組

安裝模組的方式需要透過 Dockerfile,所以在 docker-compose.yml 當中必須將預設的 image 改成用 Dockerfile 來 build,詳細的說明可以到去年的 急!在線等!求解20 點!Airflow 安裝 Python 模組 查看。

結論就是將 image: ${AIRFLOW_IMAGE_NAME:-apache/airflow:2.10.2} 註解,再將 build: . 的註解解開,這樣再 Build 的時候就會再當前目錄下抓 Dockerfile。

---
x-airflow-common:
  &airflow-common
  #image: ${AIRFLOW_IMAGE_NAME:-apache/airflow:2.10.2}
  build: .
  environment:
    &airflow-common-env

(2) 新增 dbt 資料夾

volumes 的區塊要加上 ${AIRFLOW_PROJ_DIR:-.}/dbt:/opt/airflow/dbt 才能將 dbt 資料夾內的東西放到 docker 環境中執行

  environment:
    &airflow-common-env
    AIRFLOW__CORE__EXECUTOR: CeleryExecutor
    ...
  volumes:
    ...
    - ${AIRFLOW_PROJ_DIR:-.}/plugins:/opt/airflow/plugins
    - ${AIRFLOW_PROJ_DIR:-.}/dbt:/opt/airflow/dbt

2. Dockerfile

FROM apache/airflow:2.10.2
COPY requirements.txt .
RUN python3 -m pip install --upgrade pip && \
    python3 -m pip install -r requirements.txt
  1. FROM apache/airflow:2.10.2:就是用apache/airflow:2.10.2 當作基礎映像檔(base image),
  2. COPY requirements.txt . : 將專案的 requirements.txt 檔案複製到 Docker 容器的根目錄 (/)
  3. RUN .... : 執行後方的指令,就是更新 pip 並安裝 requirements.txt 當中的檔案

3. requirements.txt

astronomer-cosmos
dbt-core
dbt-postgres

requirements.txt 中會包含 Python 專案中需要安裝的套件,也可以直接指定要安裝的版本,例如 astronomer-cosmos==1.0.2

astronomer-cosmos 模組連結

四、plugins/include 中的關鍵少數檔案

1. constants.py

這個檔案設定了 dbt 專案的路徑和執行設定,確保 Cosmos 可以正確執行 dbt 專案內的轉換工作。

from pathlib import Path
from cosmos import ExecutionConfig

jaffle_shop_path = Path("/opt/airflow/dbt/jaffle_shop")
dbt_executable = Path("/opt/airflow/dbt")

venv_execution_config = ExecutionConfig(
    dbt_executable_path=str(dbt_executable)
)
  • jaffle_shop_path: 這是一個 Path 物件,指定了 dbt 專案的路徑,這裡指向的是 docker 容器當中的 /opt/airflow/dbt/jaffle_shop,即 dbt 專案的根目錄。
  • dbt_executable: 定義了 dbt 的執行路徑,指向 /opt/airflow/dbt,這是 docker 容器中 dbt 的安裝位置,這樣 Airflow 就知道在哪裡執行 dbt 命令。
  • venv_execution_config : 這是 ExecutionConfig 物件,定義了 dbt 執行的基本資訊。這裡的 dbt_executable_path 將指向前面定義的 dbt 路徑,Airflow 才能正確執行的 dbt。

2. profiles.py

主要是用來定義 dbt 的資料庫連線設定,將 dbt 與 Airflow 資料庫連結。

實務上當然是不建議用 metadata_db,測試或練習就沒關係吧/images/emoticon/emoticon16.gif

from cosmos import ProfileConfig
from cosmos.profiles import PostgresUserPasswordProfileMapping

airflow_db = ProfileConfig(
    profile_name="airflow_db",
    target_name="dev",
    profile_mapping=PostgresUserPasswordProfileMapping(
        conn_id="airflow_metadata_db",
        profile_args={"schema": "dbt"}
    )
)
  • airflow_db: 這是一個 ProfileConfig 物件,用來定義 dbt profiles 的基本設定。這裡的 profile_name 是 airflow_db,target_name 是 dev,可以對應到前幾天說的 profiles.yml
  • PostgresUserPasswordProfileMapping : 這個物件負責將 Airflow 的 conn_id 對應(mapping)到 dbt 的資料庫設定。這裡的使用的是 airflow_metadata_db Airflow 的預設資料庫,但是 schema 的部分是改用 dbt

五、結語

~~~~明天有颱風假嗎?~~~~/images/emoticon/emoticon07.gif/images/emoticon/emoticon68.gif


上一篇
【Day 15】用 Astronomer Cosmos 結合 dbt 和 Airflow - 背景介紹
下一篇
【Day 17】用 Astronomer Cosmos 結合 dbt 和 Airflow - 再戰 Jaffle Shop
系列文
我獨自升級:從水管工走向 DataOps21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言