[新機能]dbt Cloudの操作をローカルのCLIから行える「dbt Cloud CLI」を試してみた

2023.10.18

さがらです。

現地時間10月16日~10月19日で、dbt Coalesceが開催されています。

基調講演で発表された新機能については、下記のブログが参考になります。

この基調講演で、dbt Cloudの操作をローカルのCLIから行える「dbt Cloud CLI」の発表がありました。(2023年10月18日時点ではパブリックプレビュー)

早速試してみたので、本記事でその内容をまとめてみます。

公式Doc

検証環境

  • OS:Ubuntu 20.04 LTS(WSL2を使用)
  • dbt Cloud:Enterpriseエディション

事前準備

事前に、dbt Cloud上で1つdbt projectを作っておき、認証に用いるユーザーでdevelopment credentialsを作っておきましょう。

この上で、ローカル上で対象のリポジトリをクローンしておきます。このクローンしたディレクトリ上で作業をしていきます。

インストール

以下のリポジトリを確認して、最新のリソースのバージョンを確認してダウンロードします。

以下はdbt Cloud CLIの0.34.2をダウンロードするコマンドの例です。

wget https://github.com/dbt-labs/dbt-cli/releases/download/v0.34.2/dbt_0.34.2_linux_amd64.tar.gz

ダウンロードしたファイルに対して、以下のtarコマンドを実行します。

tar -xf dbt_0.34.2_linux_amd64.tar.gz

./dbtコマンドを実行して、以下のように表示されれば無事にインストール完了となります。

$ ./dbt
The dbt Cloud CLI - an ELT tool for running SQL transformations and data models in dbt Cloud.   For more documentation on these commands, visit: docs.getdbt.com

Usage:
  dbt [command]

Available Commands:
  build         Run all seeds, models, snapshots, and tests in DAG order
  cancel        Cancel the most recent invocation
  clean         Delete all folders in the clean-targets list (usually the dbt_packages and target directories.)
  clone         Create clones of selected nodes based on their location in the manifest provided to --state.
  compile       Generates executable SQL from source, model, test and analysis files.
  deps          Pull the most recent version of the dependencies listed in packages.yml
  docs          Generate or serve the documentation website for your project
  help          Help about any command
  list          List the resources in your project
  parse         Parses the project and provides information on performance
  reattach      Reattach to the most recent invocation to retrieve logs and artifacts
  retry         Retry the nodes that failed in the previous run.
  run           Compile SQL and execute against the current target database.
  run-operation Run the named macro with any supplied arguments.
  seed          Load data from csv files into your data warehouse.
  show          Generates executable SQL for a named resource or inline query, runs that
SQL, and returns a preview of the results. Does not materialize anything to
the warehouse.
  sl            Query metrics or metadata against your semantic layer.
  snapshot      Execute snapshots defined in your project
  source        Manage your project's sources
  test          Runs tests on data in deployed models.

Flags:
  -h, --help   help for dbt

Use "dbt [command] --help" for more information about a command.

dbt Cloudの認証情報のセットアップ

まず、2023年10月18日時点ではdbt Cloud CLIはBeta機能のため、対象のdbt CloudアカウントでBeta機能を有効化する必要があります。

dbt Cloud右上の歯車マークからProfile Settingsを押し、一番下のExperimental FeaturesBetaを有効化します。

続いて、dbt Cloud上で必要なファイルを取得します。

2023年10月18日時点では、dbt Cloudにログインすると画面最上部にTry Cloud CLI(beta)というボタンがあると思います。これをクリックします。

リンク先の「Step 2: Configure Cloud authentication」にDownload CLI configuration fileというボタンがあるので、これを押します。すると、dbt_cloud.ymlというファイルがダウンロードできます。

ダウンロードしたファイルを、ローカルの~/.dbt/上に移動させます。下図のようになっていればOKです。

ローカル環境からdbt Cloudに接続

クローンしたディレクトリに移動し、dbt_project.ymlを開きます。

次にこのdbt_project.ymlに記述する内容を確認したいのですが、dbt Cloud上で接続したいdbt projectを選択した上でTry Cloud CLI(beta)を押していると、「Step 3: Link your local project to a dbt Cloud project」に、記入すべきproject-idまで含まれたyamlのコードが表示されているはずです。

このyamlのコードのうち、下記のようなdbt-cloudのところだけをコピーして、dbt_project.ymlに貼り付けて保存します。

dbt-cloud: 
    project-id: <project-id5~6桁>

これでdbtコマンド経由で対象のdbt projectを実行できるようになりました!

試しに対象のディレクトリ上で./dbt compileを実行すると、下記のように実行されました。

$ ./dbt compile
Created task id 5bc08d56-def8-4cd0-abdb-f38aaeeffb13
01:53:20 Running with dbt=1.6.6
01:53:22 Registered adapter: snowflake=1.6.4
01:53:22 Unable to do partial parsing because saved manifest not found. Starting full parse.
01:53:23 Found 3 models, 1 snapshot, 13 tests, 3 sources, 0 exposures, 0 metrics, 376 macros, 0 groups, 0 semantic models
01:53:26 Concurrency: 4 threads (target='dev')

Artifacts have been downloaded

また、./dbt buildコマンドを実行してみると、下記のように実行されました!

$ ./dbt build
Created task id 20d02008-bf17-4635-aa51-5fc9e11cfbf2
01:56:21 Running with dbt=1.6.6
01:56:22 Registered adapter: snowflake=1.6.4
01:56:22 Found 3 models, 1 snapshot, 13 tests, 3 sources, 0 exposures, 0 metrics, 376 macros, 0 groups, 0 semantic models
01:56:25 Concurrency: 4 threads (target='dev')
01:56:25 1 of 17 START test source_not_null_jaffle_shop_customers_id .................... [RUN]
01:56:25 2 of 17 START test source_not_null_jaffle_shop_orders_id ....................... [RUN]
01:56:25 3 of 17 START test source_not_null_snapshot_test_FOR_DBT_SNAPSHOT_TEST_uuid .... [RUN]
~中略~
01:56:25 4 of 17 START test source_unique_jaffle_shop_customers_id 
01:56:37 Finished running 13 tests, 2 view models, 1 snapshot, 1 table model in 0 hours 0 minutes and 15.13 seconds (15.13s).
01:56:37 Completed successfully
01:56:37 Done. PASS=17 WARN=0 ERROR=0 SKIP=0 TOTAL=17

Artifacts have been downloaded

接続先のSnowflakeを見ると、開発用のスキーマに各オブジェクトが作られていました!

最後に

簡単ではありますが、公式のやり方に沿ってdbt Cloud CLIを試してみました。

本当だったら、dbt Cloud CLIのパスをちゃんと通して./dbtとしなくても実行できるようにするとか、VSCodeから実行するとか、色々やっておきたいことはあるのですが、とりあえずは公式Docに記載のやり方に沿って試してみましたw

さくっと試す分には簡単に試せるので、ぜひ試してみてください!