🦔

OpenMetadata上(Ingestion Container)でdbtを実行する

2023/10/01に公開

環境構築

Ingestionコンテナのパッケージ更新

rootユーザでコンテナにアクセスる

rootユーザでアクセス
docker exec -it -u 0 {container id} bash
コンテナ上で実行
sudo apt update -y

dbtファイルをIngestionコンテナに配置

git cloneがスタンダードですが、私の場合プライベートリポなので、鍵設定がめんどくさいのでdocker cpコマンドで持ってくることにしました。

Ingestionコンテナにdbt実行環境を構築

Pythonの仮想環境を準備

python3 -m venv dbt-venv
source dbt-venv/bin/activate

airflow_dbtをインストールします。

pip3 install airflow-dbt

仮想環境で実行しているけど、エラーが出ました。
このエラーって仮想環境に入らずにインストールしようとするとエラーが出るって記事を見つけたんですよね。。。

ERROR: Will not install to the user site because it will lack sys.path precedence to setuptools in /home/airflow/dbt_prj/dbt-venv/lib/python3.9/site-packages

一旦仮想環境外でインストールできたので、今回は仮想環境無しで進めます。
あとで原因調査します。

DAGの実行

DAGはシンプルにdbt runを実行するだけにしました。

from pendulum import datetime
import json
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from airflow.operators.bash_operator import BashOperator
from airflow_dbt.operators.dbt_operator import (
    DbtSeedOperator,
    DbtRunOperator,
    DbtTestOperator,
    DbtDocsGenerateOperator,
)
from airflow.utils.dates import days_ago

default_args = {
    'owner': 'airflow',
    'start_date': days_ago(0),
    'retries': 0,
    'dir': '/home/airflow/dbt_prj/snowpark2dbt'
}
with DAG(dag_id='dbt_dag', default_args=default_args, schedule_interval=None) as dag:
    dbt_run = DbtRunOperator(
        task_id='dbt_run',
        profiles_dir='/home/airflow/dbt_prj/snowpark2dbt'

    )
    dbt_run

DAGの実行エラー

DAGエラー
AIRFLOW_CTX_DAG_OWNER=airflow
AIRFLOW_CTX_DAG_ID=dbt_dag
AIRFLOW_CTX_TASK_ID=dbt_run
AIRFLOW_CTX_EXECUTION_DATE=2023-09-30T15:09:50.078234+00:00
AIRFLOW_CTX_TRY_NUMBER=1
AIRFLOW_CTX_DAG_RUN_ID=manual__2023-09-30T15:09:50.078234+00:00
[2023-10-01, 00:09:57 JST] {dbt_hook.py:117} INFO - dbt run --profiles-dir /home/airflow/dbt_prj/snowpark2dbt
[2023-10-01, 00:09:57 JST] {taskinstance.py:1909} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/home/airflow/.local/lib/python3.9/site-packages/airflow_dbt/operators/dbt_operator.py", line 98, in execute
    self.create_hook().run_cli('run')
  File "/home/airflow/.local/lib/python3.9/site-packages/airflow_dbt/hooks/dbt_hook.py", line 119, in run_cli
    sp = subprocess.Popen(
  File "/usr/local/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/lib/python3.9/subprocess.py", line 1821, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: 'dbt'
[2023-10-01, 00:09:57 JST] {taskinstance.py:1415} INFO - Marking task as FAILED. dag_id=dbt_dag, task_id=dbt_run, execution_date=20230930T150950, start_date=20230930T150957, end_date=20230930T150957
[2023-10-01, 00:09:57 JST] {standard_task_runner.py:92} ERROR - Failed to execute job 151 for task dbt_run ([Errno 13] Permission denied: 'dbt'; 39419)

エラーがErrno 13] Permission denied:なので、権限まわりをずっと調査していましたが、最終的にdbt-snowflakeがインストールされていないことが原因っぽい?です。
試しにdbt-snowflake入れたらDAG実行が成功しました。
7回も失敗した・・・

このあと仮想環境エラーの調査をしたいと思います。
結論、エラーの解決は無理でした。。。なのでマシな回避策としてグローバルにインストールし、--system-site-packagesをtrueにします。
そうすれば、仮想環境上でもグローバルにインストールしたパッケージを参照できるので。
ちなみに、グローバルにpip3 installを実施すると以下のエラーが起きました。

airflow@b40d0c50b61f:/opt/airflow$ pip3 install airflow-dbt
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
dbt-semantic-interfaces 0.2.0 requires importlib-metadata~=6.0, but you have importlib-metadata 5.2.0 which is incompatible.
dbt-semantic-interfaces 0.2.0 requires jsonschema~=3.0, but you have jsonschema 4.19.1 which is incompatible.
jsonschema-spec 0.1.6 requires jsonschema<4.18.0,>=4.0.0, but you have jsonschema 4.19.1 which is incompatible.
openapi-schema-validator 0.4.4 requires jsonschema<4.18.0,>=4.0.0, but you have jsonschema 4.19.1 which is incompatible.
openapi-spec-validator 0.5.7 requires jsonschema<4.18.0,>=4.0.0, but you have jsonschema 4.19.1 which is incompatible.

依存関係によるインストール失敗かな?と思い、インストールできねぇ〜って思ってたら、依存関係のエラーであってairflow-dbtはインストールできていました。
ならSuccessfullyとかログに出してほしい。。

つまりこういうこと

(dbt-venv) airflow@b40d0c50b61f:/opt/airflow$ pip3 freeze | grep dbt-duckdb
(dbt-venv) airflow@b40d0c50b61f:/opt/airflow$ pip3 install dbt-duckdb
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
openmetadata-ingestion 1.1.6.1 requires setuptools~=66.0.0, but you have setuptools 58.1.0 which is incompatible.
openapi-spec-validator 0.5.7 requires jsonschema<4.18.0,>=4.0.0, but you have jsonschema 3.2.0 which is incompatible.
openapi-schema-validator 0.4.4 requires jsonschema<4.18.0,>=4.0.0, but you have jsonschema 3.2.0 which is incompatible.
jsonschema-spec 0.1.6 requires jsonschema<4.18.0,>=4.0.0, but you have jsonschema 3.2.0 which is incompatible.
apache-airflow 2.6.3 requires jsonschema>=4.0.0, but you have jsonschema 3.2.0 which is incompatible.

[notice] A new release of pip is available: 23.0.1 -> 23.2.1
[notice] To update, run: pip install --upgrade pip
(dbt-venv) airflow@b40d0c50b61f:/opt/airflow$ pip3 freeze | grep dbt-duckdb
dbt-duckdb==1.6.0
(dbt-venv) airflow@b40d0c50b61f:/opt/airflow$

Discussion