【dbt】dbt ~ 基本編 / Seed ~

■ はじめに

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

の続き。

dbt の シード(Seed)について、徐々に学んでみる

目次

【1】Seed
【2】Hello world
 1)前提条件
 2)実行手順
【3】ref 関数

【1】Seed

* CSV ファイルを元にテーブルを作る機能

【2】Hello world

1)前提条件

[1] dbt がインストールされていること
 => インストールされていない場合は、以下の関連記事などを参照のこと

https://dk521123.hatenablog.com/entry/2023/05/30/151003

[2] DB を用意する
 => 今回の場合は、PostgreSQL

2)実行手順

[1] dbt プロジェクトを作成する

>dbt init hello_world

13:37:37  Running with dbt=1.7.2
13:37:37  
Your new dbt project "hello_world" 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/
one you want? https://docs.getdbt.com/docs/available-adapters)

Enter a number: 1
host (hostname for the instance): localhost
port [5432]:
user (dev username): postgres
pass (dev password): 
dbname (default database that dbt will build objects in): sample_db
schema (default schema that dbt will build objects in): public
threads (1 or more) [1]: 1
13:40:26  Profile hello_world written to C:\Users\User\.dbt\profiles.yml using target's profile_template.yml and your supplied values. Run 'dbt debug' to validate the connection.

[2] CSVファイルを作成する

* CSVファイルを作成し、「seeds」フォルダに格納する

例「country_codes.csv

country_code,country_name
US,United States
CA,Canada
GB,United Kingdom

[3] 「dbt seed」を実行する

>dbt seed  

13:41:26  Running with dbt=1.7.2
13:41:26  Registered adapter: postgres=1.7.2
13:41:27  Unable to do partial parsing because saved manifest not found. Starting full parse.
13:41:29  Found 2 models, 1 seed, 4 tests, 0 sources, 0 exposures, 0 metrics, 401 macros, 0 groups, 0 semantic models
13:41:29
13:41:30  Concurrency: 1 threads (target='dev')
13:41:30
13:41:30  1 of 1 START seed file public.country_codes .................................... [RUN]
13:41:31  1 of 1 OK loaded seed file public.country_codes ................................ [INSERT 3 in 0.49s]
13:41:31  
13:41:31  Finished running 1 seed in 0 hours 0 minutes and 1.43 seconds (1.43s).    
13:41:31  
13:41:31  Completed successfully
13:41:31
13:41:31  Done. PASS=1 WARN=0 ERROR=0 SKIP=0 TOTAL=1

[4] DB内にテーブルが作成されていることを確認

* 「country_codes」テーブルを確認する

【3】ref 関数

* Seed だけではないが、ref 関数で参照可能

https://docs.getdbt.com/reference/dbt-jinja-functions/ref
例:hello_seed.sql

select
 *
from
 {{ ref('country_codes') }}

https://zenn.dev/dbt_tokyo/books/537de43829f3a0/viewer/models_doc

参考文献

https://zenn.dev/foursue/books/31456a86de5bb4/viewer/bcf0d3
https://dev.classmethod.jp/articles/dbt-analyses-and-seeds-course/
https://docs.getdbt.com/docs/build/seeds

関連記事

dbt ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2023/06/30/000000
dbt ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/05/30/151003
dbt ~ 基本編 / Model ~
https://dk521123.hatenablog.com/entry/2023/11/25/231128
dbt ~ Model あれこれ ~
https://dk521123.hatenablog.com/entry/2023/11/26/224522
dbt ~ Macro ~
https://dk521123.hatenablog.com/entry/2023/11/29/003751