👓

【GCP】dbt-bigqueryをローカルで使ってみる

2023/08/01に公開

目的

ローカルのMacからdbtでbigqueryの操作を試してみます。

方法

インストールはpipやdockerでも可能でしたが、今回はhomebrewでやりました。
インストールの時間は3〜40分程度かかりました。
dbtからGCPへの認証は、oauthで試しました。

先にいれておくもの

必要になるので入れてない場合は先にinstallします。

brew install git
brew install gcc
brew install --cask google-cloud-sdk ※サイトからのDLでも可

google-cloud-sdk

別途設定が必要です。
https://cloud.google.com/sdk/docs/install?hl=ja

dbtインストール

brew tap dbt-labs/dbt  #これ忘れるとエラー出ます
brew install dbt-bigquery

Macでのインストール時にxcrunでエラーが出る場合

以下を実行してmacにコマンドラインツールをいれます。

xcode-select --install

インストール後の確認

dbt --version
Core:
  - installed: 1.5.4
  - latest:    1.5.4 - Up to date!

Plugins:
  - bigquery: 1.5.3 - Up to date!

dbtプロジェクト作成

実行したカレントディレクトリ直下にdbtプロジェクトのディレクトリができます。
対話形式でprofile.ymlの設定も実施できます。
 →profile.ymlは~/.dbt配下にできるので注意

dbt init dbt_test

profile.ymlの例

dbt_test:
  outputs:
    dev:
      dataset: dbt_test
      job_execution_timeout_seconds: 300
      job_retries: 1
      location: US
      method: oauth ※oauthやサービスアカウントを選択可能です
      priority: interactive
      project: <GCPプロジェクトID>
      threads: 1
      type: bigquery
  target: dev

dbtでoauth通すためのgcp login

ローカルのdbtからbigqueryにアクセスできるよう、GCPに認証を通します。
initでprofile.ymlにoauthを設定した場合に必要となります。

gcloud auth application-default login

PJ配下にディレクトリ移動

cd  ./dbt_test
XXXXX dbt_test % ls
README.md	analyses	dbt_project.yml	logs		macros		models		seeds		snapshots	tests

疎通

ファイルチェックとBQへの接続チェックを実施します

dbt debug
08:48:44  Running with dbt=1.5.4
08:48:44  dbt version: 1.5.4
08:48:44  python version: 3.9.17
08:48:44  python path: /usr/local/Cellar/dbt-bigquery/1.5.3/libexec/bin/python3.9
08:48:44  os info: macOS-13.4.1-x86_64-i386-64bit
08:48:44  Using profiles.yml file at /Users/XXXXXXX/.dbt/profiles.yml
08:48:44  Using dbt_project.yml file at /Users/XXXXXXX/dbt_test/dbt_project.yml
08:48:44  Configuration:
08:48:45    profiles.yml file [OK found and valid]
08:48:45    dbt_project.yml file [OK found and valid]
08:48:45  Required dependencies:
08:48:45   - git [OK found]

08:48:45  Connection:
08:48:45    method: oauth
08:48:45    database: <GCPのプロジェクトID>
08:48:45    schema: dbt_test
08:48:45    location: US
08:48:45    priority: interactive
08:48:45    timeout_seconds: 300
08:48:45    maximum_bytes_billed: None
08:48:45    execution_project: <GCPのプロジェクトID>
08:48:45    job_retry_deadline_seconds: None
08:48:45    job_retries: 1
08:48:45    job_creation_timeout_seconds: None
08:48:45    job_execution_timeout_seconds: 300
08:48:45    gcs_bucket: None
08:48:45  Registered adapter: bigquery=1.5.3
08:48:49    Connection test: [OK connection ok]

08:48:49  All checks passed!

実行

dbt_project.ymlに実行したいモデルをコーディングして実行します。
今回はプロジェクト作成時にサンプルで生成されるdbt_project.ymlをそのまま使います。
実行するとbigqueryにテーブルができます。

dbt run
08:54:30  Running with dbt=1.5.4
08:54:31  Registered adapter: bigquery=1.5.3
08:54:31  Unable to do partial parsing because saved manifest not found. Starting full parse.
08:54:31  Found 2 models, 4 tests, 0 snapshots, 0 analyses, 357 macros, 0 operations, 0 seed files, 0 sources, 0 exposures, 0 metrics, 0 groups
08:54:31
08:54:36  Concurrency: 1 threads (target='dev')
08:54:36
08:54:36  1 of 2 START sql table model dbt_test.my_first_dbt_model ....................... [RUN]
08:54:39  1 of 2 OK created sql table model dbt_test.my_first_dbt_model .................. [CREATE TABLE (2.0 rows, 0 processed) in 3.26s]
08:54:39  2 of 2 START sql view model dbt_test.my_second_dbt_model ....................... [RUN]
08:54:40  2 of 2 OK created sql view model dbt_test.my_second_dbt_model .................. [CREATE VIEW (0 processed) in 1.30s]
08:54:40
08:54:40  Finished running 1 table model, 1 view model in 0 hours 0 minutes and 8.98 seconds (8.98s).
08:54:40
08:54:40  Completed successfully
08:54:40
08:54:40  Done. PASS=2 WARN=0 ERROR=0 SKIP=0 TOTAL=2

BQ画面

以下のようにサンプルのdbt_project.ymlに沿って作成されます。

テスト

テストを実行したい場合は以下のコマンドから可能です。

dbt test

ドキュメント作成とdocサーバ起動

ドキュメントを作成できます。

dbt docs generate
10:39:54  Running with dbt=1.5.4
10:39:55  Registered adapter: bigquery=1.5.3
10:39:55  Found 2 models, 4 tests, 0 snapshots, 0 analyses, 357 macros, 0 operations, 0 seed files, 0 sources, 0 exposures, 0 metrics, 0 groups
10:39:55
10:39:58  Concurrency: 1 threads (target='dev')
10:39:58
10:39:58  Building catalog
10:40:03  Catalog written to /Users/XXXXX/dbt_test/target/catalog.json
XXXXX dbt_test % view /Users/XXXXX/dbt_test/target/catalog.json

また、dbt-coreでも、docs serveコマンドでローカルホストにてドキュメントはGUI化できます。
dbt-cloudのようにGUIでdbtコマンドの実行などはできません。

dbt docs serve
10:43:47  Running with dbt=1.5.4
Serving docs at 8080
To access from your browser, navigate to: http://localhost:8080
Press Ctrl+C to exit.
127.0.0.1 - - [31/Jul/2023 19:43:49] "GET / HTTP/1.1" 200 -

Discussion