【dbt】dbt ~ 入門編 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2023/06/30/000000

の続き。

dbt に関するお仕事が入ったので、急遽メモ。

目次

【1】dbt
【2】インストール
【3】QuickStart
 0)前提条件
 1)作業ディレクトリを作成する
 2)テスト用DB環境を準備する
 3)DBへの接続設定を行う
 4)dbt initの実行
 5)dbt debugの実行
 6)dbt runの実行
 7)dbt docs generateの実行
 8)dbt docs serveの実行

【1】dbt

* dbt = data build tool
* ELT(抽出, 変換, 格納)の Transform(変換)を担うツール
* 詳細は、以下の関連記事を参照のこと

dbt ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2023/06/30/000000

【2】インストール

* 以下の関連記事を参照のこと

dbt ~ 環境設定編 ~
https://dk521123.hatenablog.com/entry/2023/12/16/152147

【3】QuickStart

* 以下を参考に、Hello world的なことをやってみる

https://medium.com/everything-full-stack/first-steps-with-dbt-over-postgres-db-f6b350bf4526

* VS Code でやった方がやりやすかった。

0)前提条件

* 以下がインストールされていること
 + dbt
 + PostgreSQL 

1)作業ディレクトリを作成する

# dbt コマンドが使える状態を確認
dbt --version

# Hello World用作業ディレクトリを作成
mkdir hello_world
cd hello_world

2)テスト用DB環境を準備する

-- PostgreSQL内での作業

-- テスト用スキーマ
create schema hello;

-- テスト用テーブル
CREATE TABLE hello.users (
 id bpchar(36) NULL,
 user_name varchar(60) null,
 email varchar(60) null
);

-- テストデータ
INSERT INTO hello.users
(id, user_name, email)
VALUES('1', 'Mike', 'mike@xxx.com');
INSERT INTO hello.users
(id, user_name, email)
VALUES('1', 'Tom', 'tom@xxx.com');

3)dbt initの実行

[1] 「dbt init <プロジェクト名>」を実行する

# プロジェクト名 = usersで実行
# エラーの場合、
# 後述「1)「dbt init users」実行時に「Error: Invalid value for '--profiles-dir'」が表示」参照
$ dbt init users
07:19:57  Running with dbt=1.5.0
Which database would you like to use?
[1] postgres

(Don't see the one you want? https://docs.getdbt.com/docs/available-adapters)

Enter a number: 1
07:20:08  Profile users written to C:\Users\user\.dbt\profiles.yml using target's sample configuration. Once updated, you'll be able to start developing with dbt.
07:20:08
Your new dbt project "users" was created!

For more information on how to configure the profiles.yml file,
please consult the dbt documentation here:

  https://docs.getdbt.com/docs/configure-your-profile

One more thing:

Need help? Don't hesitate to reach out to us via GitHub issues or on Slack:

  https://community.getdbt.com/

Happy modeling!

3)DBへの接続設定を行う

* .dbt/profiles.yml の修正を行う

修正前

users:
  outputs:

    dev:
      type: postgres
      threads: [1 or more]
      host: [host]
      port: [port]
      user: [dev_username]
      pass: [dev_password]
      dbname: [dbname]
      schema: [dev_schema]

    prod:
      type: postgres
      threads: [1 or more]
      host: [host]
      port: [port]
      user: [prod_username]
      pass: [prod_password]
      dbname: [dbname]
      schema: [prod_schema]

  target: dev

修正後

# 修正後 (PostgreSQLの接続情報で修正する)
users: # ←ここは、プロジェクト名を指定する(今回は「users」)
  outputs:
    dev:
      type: postgres
      threads: 1
      host: localhost
      port: 5432
      user: postgres
      pass: password
      dbname: postgres
      schema: hello

  target: dev

4)「dbt debug」の実行

$ cd users
$ dbt debug
07:31:24  Running with dbt=1.5.0
07:31:24  dbt version: 1.5.0
07:31:24  python version: 3.10.0
・・・略・・・
07:31:25    Connection test: [OK connection ok]

07:31:25  All checks passed!

5)dbt runの実行

[1] モデルの作成

# Under models directory create a new directory named users
mkdir models\users
# この配下に「users_model.sql」を作成する

models\users\users_model.sql (「;」とか要らない)

select id, user_name from users

models\users\scheme.yml

version: 2

sources:
  - name: hello
    tables:
      - name: users

[2] 「dbt run」を実行する

$ dbt run
07:46:40  Running with dbt=1.5.0
07:46:41  Unable to do partial parsing because saved manifest not found. Starting full parse.
・・・略・・・
07:46:43  Completed successfully
07:46:43
07:46:43  Done. PASS=2 WARN=0 ERROR=0 SKIP=0 TOTAL=2

7)dbt docs generateの実行

* dbt docs generate コマンドで、ドキュメントを生成する

https://dev.classmethod.jp/articles/dbt-documentation/

dbt docs generate

08:59:38  Running with dbt=1.5.0
・・・略・・・
08:59:39  Catalog written to C:\xxxx\hello_world\users\target\catalog.json

8)dbt docs serveの実行

* ブラウザが立ち上がり、作成したドキュメントを閲覧できる

https://docs.getdbt.com/reference/commands/cmd-docs

dbt docs serve

関連記事

dbt ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2023/06/30/000000
dbt ~ 環境設定編 ~
https://dk521123.hatenablog.com/entry/2023/12/16/152147
dbt ~ 基本編 / Model ~
https://dk521123.hatenablog.com/entry/2023/11/25/231128
dbt ~ Model あれこれ ~
https://dk521123.hatenablog.com/entry/2023/11/26/224522
dbt ~ 基本編 / Source ~
https://dk521123.hatenablog.com/entry/2023/12/08/111012
dbt ~ 基本編 / Seed ~
https://dk521123.hatenablog.com/entry/2023/11/20/230946
dbt ~ Macro ~
https://dk521123.hatenablog.com/entry/2023/11/29/003751
dbt ~ Hooks ~
https://dk521123.hatenablog.com/entry/2023/12/11/040122
dbt ~ aliases ~
https://dk521123.hatenablog.com/entry/2023/12/13/031018
dbt ~ 更新 / 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2023/12/07/060129
dbt ~ 更新 / Update or Insert ~
https://dk521123.hatenablog.com/entry/2023/12/19/224453
dbt ~ 更新 / Delete and Insert ~
https://dk521123.hatenablog.com/entry/2023/12/20/000104
dbt ~ 更新 / DROP + CTAS ~
https://dk521123.hatenablog.com/entry/2023/12/04/000000
dbt ~ ドキュメント化 / dbt docs ~
https://dk521123.hatenablog.com/entry/2023/12/10/125512
dbt に関するトラブル
https://dk521123.hatenablog.com/entry/2023/06/19/000000