【dbt】dbt CLI ~ テーブル一覧表示 / dbt list ~

◾️はじめに

 現在扱っているテーブル一覧が欲しいってニーズがあり、
dbt model から取得しようとしたら、
dbt list / dot lsコマンドから取得できることが分かったので
調べてみた

目次

【0】何に使える?
【1】dbt list / dbt ls
【2】オプション
 1)--resource-type
 2)--select
 3)--output
 4)--output-keys
 5)--quiet
【3】サンプル
 例1:Hello world
 例2:+ --quiet
 例3:+ --output json
 例4:+ --output-keys
 例5:+ jqコマンド

【0】何に使える?

* 未使用テーブルの洗い出し
 => 以下のサイトを参照

https://qiita.com/k_0120/items/d0c54bbfa19a26ff371d

【1】dbt list / dbt ls

* テーブル一覧を取得する

【2】オプション

https://docs.getdbt.com/reference/dbt-commands#available-commands

dbt ls or dtb list
     [--resource-type {model,semantic_model,source,seed,snapshot,metric,test,exposure,analysis,default,all}]
     [--select SELECTION_ARG [SELECTION_ARG ...]]
     [--models SELECTOR [SELECTOR ...]]
     [--exclude SELECTOR [SELECTOR ...]]
     [--selector YML_SELECTOR_NAME]
     [--output {json,name,path,selector}]
     [--output-keys KEY_NAME [KEY_NAME]]

1)--resource-type

* 特定のリソースに絞って出力

取りうる値

model,semantic_model,source,seed,snapshot,metric,
test,exposure,analysis,default,all

2)--select

* 出力範囲を指定

3)--output

* 出力形式を指定

取りうる値

* json, name,path,selector

※ YAMLはサポート外

e.g. --output path

$ dbt ls --select snowplow.* --output path
models/base/snowplow_base_events.sql
models/base/snowplow_base_web_page_context.sql
models/identification/snowplow_id_map.sql
...

Tips: --output json

* jqコマンドと組み合わせれば、CSVファイルとして出力
 => jqコマンドについては、以下の関連記事を参照の事

jq コマンド ~ JSON を扱う ~
https://dk521123.hatenablog.com/entry/2020/02/01/000000

4)--output-keys

* 出力するキーを絞り込む
* --output json を選んだ場合のみ

5)--quiet

* dbt のバージョンやモデル数といった情報の出力せず、
 モデルの一覧のみを出力することができる。

【3】サンプル

* 以下の関連記事の「【3】dbt も docker compose で構築する」の
 環境で試した内容を記述する

dbt ~ 環境設定 / Docker 編 ~
https://dk521123.hatenablog.com/entry/2024/10/11/230419

例1:Hello world

$ docker exec dbt_cli /bin/sh -c "cd my_project && dbt ls"   
03:50:39  Running with dbt=1.9.0
03:50:39  Registered adapter: postgres=1.9.0
03:50:39  Found 2 models, 4 data tests, 429 macros
my_project.example.my_first_dbt_model
my_project.example.my_second_dbt_model
my_project.example.not_null_my_first_dbt_model_id
my_project.example.not_null_my_second_dbt_model_id
my_project.example.unique_my_first_dbt_model_id
my_project.example.unique_my_second_dbt_model_id

例2:+ --quiet

$ docker exec dbt_cli /bin/sh -c "cd my_project && dbt ls --quiet"
my_project.example.my_first_dbt_model
my_project.example.my_second_dbt_model
my_project.example.not_null_my_first_dbt_model_id
my_project.example.not_null_my_second_dbt_model_id
my_project.example.unique_my_first_dbt_model_id
my_project.example.unique_my_second_dbt_model_id

例3:+ --output json

$ docker exec dbt_cli /bin/sh -c "cd my_project && dbt ls --quiet --output json"
{"name": "my_first_dbt_model", "resource_type": "model", "package_name": "my_project", "original_file_path": "models/example/my_first_dbt_model.sql", "unique_id": "model.my_project.my_first_dbt_model", "alias": "my_first_dbt_model", "config": {"enabled": true, "alias": null, "schema": null, "database": null, "tags": [], "meta": {}, "group": null, "materialized": "table", "incremental_strategy": null, "batch_size": null, "lookback": 1, "begin": null, "persist_docs": {}, "post-hook": [], "pre-hook": [], "quoting": {}, "column_types": {}, "full_refresh": null, "unique_key": null, "on_schema_change": "ignore", "on_configuration_change": "apply", "grants": {}, "packages": [], "docs": {"show": true, "node_color": null}, "contract": {"enforced": false, "alias_types": true}, "event_time": null, "concurrent_batches": null, "access": "protected"}, "tags": [], "depends_on": {"macros": [], "nodes": []}}
...

例4:+ --output-keys

# docker run でもできる
$ docker run --rm  --platform linux/x86_64 \
-v $(pwd)/dbt/my_project:/usr/app/my_project -v $(pwd)/.dbt:/root/.dbt \
--net=host ghcr.io/dbt-labs/dbt-postgres:1.9.latest \
list --quiet --output json --output-keys database schema name \
 --profiles-dir="/root/.dbt" --project-dir="/usr/app/my_project"
{"database": "dbt_db", "schema": "public", "name": "my_first_dbt_model"}
{"database": "dbt_db", "schema": "public", "name": "my_second_dbt_model"}
{"database": "dbt_db", "schema": "public_dbt_test__audit", "name": "not_null_my_first_dbt_model_id"}
{"database": "dbt_db", "schema": "public_dbt_test__audit", "name": "not_null_my_second_dbt_model_id"}
{"database": "dbt_db", "schema": "public_dbt_test__audit", "name": "unique_my_first_dbt_model_id"}
{"database": "dbt_db", "schema": "public_dbt_test__audit", "name": "unique_my_second_dbt_model_id"}

例5:+ jqコマンド

# 例4の結果を、jqコマンドで
# .schema == "public"をピックアップし、sample_schemaに書き換えてソートした
$ docker run --rm  --platform linux/x86_64 \
-v $(pwd)/dbt/my_project:/usr/app/my_project -v $(pwd)/.dbt:/root/.dbt \
--net=host ghcr.io/dbt-labs/dbt-postgres:1.9.latest \
list --quiet --output json --output-keys database schema name \
--profiles-dir="/root/.dbt" --project-dir="/usr/app/my_project" \
| jq -c 'select(.schema == "public") | .schema = "sample_schema" | [.] | sort_by(.database,.schema,.name)'
[{"database":"dbt_db","schema":"sample_schema","name":"my_first_dbt_model"}]
[{"database":"dbt_db","schema":"sample_schema","name":"my_second_dbt_model"}]

参考文献

https://qiita.com/k_0120/items/d0c54bbfa19a26ff371d

関連記事

dbt ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2023/06/30/000000
dbt ~ 環境設定編 ~
https://dk521123.hatenablog.com/entry/2023/12/16/152147
dbt ~ 環境設定 / Docker 編 ~
https://dk521123.hatenablog.com/entry/2024/10/11/230419
dbt ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/05/30/151003
dbt CLI ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2024/07/21/234811
dbt CLI ~ ドキュメント化 / dbt docs ~
https://dk521123.hatenablog.com/entry/2023/12/10/125512
jq コマンド ~ JSON を扱う ~
https://dk521123.hatenablog.com/entry/2020/02/01/000000