2
0

More than 1 year has passed since last update.

dbt(CLI)をセットアップしてみる(BigQuery)

Last updated at Posted at 2022-04-01

dbtとは

公式Docによると、
ELTのTをしてくれるツール。

select文を書くだけでWHのデータを変換できる。
dbtはselect文をテーブルやビューに変換する処理をする。

テストとかもできるらしい。

要するに、テンプレート化したSQLをコンパイル、DWHへ問い合わせ、反映(テーブル、ビューの作成)、またそのテストができる。

プロジェクトファイルとしてymlファイル、モデルとしてSQLファイルの準備が必要とのこと。

インストール

  • 環境 : Windows10 , bash

Web版は有料みたいなので、CLI版を使用する。
事前準備としては、

  • BigQuery(PostgreSQL等の方も読み替えても問題ないです)
  • Git
  • Python(pipを使用するため)

下記でdbt-coreとdbt-bigqueryがインストールできる。

pip install dbt-bigquery
$ dbt --version
installed version: 1.0.4
   latest version: 1.0.4

Up to date!

Plugins:
  - bigquery: 1.0.0

Macでhomebrewを使う方、Dockerイメージ、ソースから使いたい方は公式Docへ

profileの構成

「~/.dbt」に「profiles.yml」を作成する。(dbt initの際に対話形式でも作成できる)

~/.dbt/profiles.yml
my-bigquery-db:
  target: dev
  outputs:
    dev:
      type: bigquery
      method: service-account
      project: [GCP project id]
      dataset: [the name of your dbt dataset]
      threads: [1 or more]
      keyfile: [/path/to/bigquery/keyfile.json]
      <optional_config>: <value>

また、サービスアカウントにジョブユーザーのロール(roles/bigquery.jobUser、roles/bigquery.user、roles/bigquery.dataEditor)を与えておく必要があるので、下記参照
※roles/bigquery.jobUserだけだと、その後のdbt runとかでテーブル作れないため、要確認。

BigQueryのprofileは下記参照

プロジェクトを作成してみる

dbt init [プロジェクト名]で作成できる。
※また、profiles.ymlが未作成の場合、ここで対話的に聞かれるので、そこで答えることでprofiles.ymlが作成される。

$ dbt init dbt_test

デバッグ

$ dbt debug
08:45:52  Running with dbt=1.0.4
dbt version: 1.0.4
python version: 3.9.9
python path: C:\Users\user\AppData\Local\Programs\Python\Python39\python.exe
os info: Windows-10-10.0.19044-SP0
Using profiles.yml file at C:\Users\user\.dbt\profiles.yml
Using dbt_project.yml file at C:\Users\user\Desktop\abcd\dbt_test\dbt_project.yml

Configuration:
  profiles.yml file [OK found and valid]
  dbt_project.yml file [OK found and valid]

Required dependencies:
 - git [OK found]

Connection:
  method: service-account
  database: test1234
  schema: test_test
  location: None
  priority: None
  timeout_seconds: 300
  maximum_bytes_billed: None
  execution_project: test1234
  Connection test: [OK connection ok]

All checks passed!

これで接続が確認できた。

続き

2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0