Snowflake CLI を使用した |sf-dbt|の管理

注釈

The dbt Projects on Snowflake features in Snowflake CLI are available only in version 3.13.0 or later.

You can use Snowflake CLI to manage dbt projects with the following operations:

Deploying a dbt project object

The snow dbt deploy command uploads local files to a temporary stage and creates a new dbt project object, updates it by making a new version, or completely recreates it. A valid dbt project must contain two files:

  • dbt_project.yml: A standard dbt configuration file that specifies the profile to use.

  • profiles.yml: A dbt connection profile definition referenced in dbt_project.yml. profiles.yaml must define the database, role, schema, and type.

    • By default, dbt Projects on Snowflake uses your target schema (target.schema) specified from your dbt environment or profile. Unlike dbt Core behavior, the target schema specified in the profiles.yml file must exist before you create your dbt Project in order for it to compile or execute successfully.

    <profile_name>:
    target: dev
    outputs:
      dev:
        database: <database_name>
        role: <role_name>
        schema: <schema_name>
        type: snowflake
    
    Copy

The following examples illustrate how to use the snow dbt deploy command:

  • :codenowrap:`jaffle_shop`という名前のdbtプロジェクトオブジェクトをデプロイします。

    snow dbt deploy jaffle_shop
    
    Copy
  • Deploy a project named jaffle_shop from a specified directory and create or add a new version depending on whether the dbt project object already exists:

    snow dbt deploy jaffle_shop --source /path/to/dbt/directory --profiles-dir ~/.dbt/ --force
    
    Copy
  • Deploy a project named jaffle_shop from a specified directory using a custom profiles directory and enabling external access integrations:

    snow dbt deploy jaffle_shop --source /path/to/dbt/directory
    --profiles-dir ~/.dbt/ --default-target dev
    --external-access-integration dbthub-integration
    --external-access-integration github-integration
    --force
    
    Copy

Listing all available dbt project objects

The snow dbt list command lists all available dbt project objects on Snowflake.

The following examples illustrate how to use the snow dbt list command:

  • 使用可能なすべてのdbtプロジェクトオブジェクトを一覧表示します。

    snow dbt list
    
    Copy
  • :codenowrap:`product`データベース内の :codenowrap:`JAFFLE`で始まる名前を持つdbtプロジェクトオブジェクトを一覧表示します。

    snow dbt list --like JAFFLE% --in database product
    
    Copy

Executing a dbt project object command

The snow dbt execute command executes one of the following dbt commands on a Snowflake dbt project object:

dbtコマンドの使用の詳細については、dbtコマンドリファレンス をご参照ください。

The following examples illustrate how to use the snow dbt execute command:

  • dbt test コマンドを実行します。

    snow dbt execute jaffle_shop test
    
    Copy
  • run dbtコマンドを非同期で実行します。

    snow dbt execute --run-async jaffle_shop run --select @source:snowplow,tag:nightly models/export
    
    Copy

Describing a dbt project object

The snow dbt describe command describes a dbt project object on Snowflake.

The following example describes the dbt project object named my_dbt_project on Snowflake:

snow dbt describe my_dbt_project
Copy

Dropping a dbt project object

The snow dbt drop command deletes a dbt project object on Snowflake.

The following example deletes the dbt project object named my_dbt_project on Snowflake:

snow dbt drop my_dbt_project
Copy

Use snow dbt commands in a CI/CD workflow

注釈

CI/CD ワークフローの構築中に必要なのは、Githubなどのgitサーバーや |sf-cli|だけです。Gitリポジトリオブジェクトは必要ありません。

CI/CD パイプラインを構築するためには、Snowflake CLI でdbtコマンドを実行できます。これらのパイプラインは通常、新しいプルリクエストなどの新しいコードをテストしたり、何かがメインブランチにマージされるたびに実稼働アプリケーションを更新したりするために使用されます。

snow dbt コマンドを使用して CI/CD ワークフローを構築するには、次のステップに従います。

  1. dbtプロジェクトを準備します。

    1. dbtプロジェクトをダウンロードするか、新しいプロジェクトを開始してください。

      • メインプロジェクトディレクトリに dbt_project.yml ファイルおよび profiles.yml ファイルが含まれていることを確認します。

      • dbt_project.yml で参照されているプロファイル名が profiles.yml で定義されていることを確認します。

        注釈

        Snowflakeのdbtプロジェクトオブジェクトはパスワードを必要としないため、profiles.yml にパスワードが含まれている場合は、それが削除されるまでデプロイが停止します。

  2. Snowflake CLI GitHub アクションを設定します。

    Snowflake CLI の GitHub アクションの設定 および Snowflakeとの接続の確認 に関するガイドラインに従います。

  3. ワークフローを定義します。

    組織のニーズに基づいて、ワークフローが実行する必要のあるコマンドを決定します。次の例は、新しいファイルを使用して product_pipeline という名前のdbtプロジェクトオブジェクトのバージョンを更新し、変換を実行し、最後にテストを実行する CI ワークフローについて説明しています。

    - name: Execute Snowflake CLI command
      run: |
        snow dbt deploy product_pipeline
        snow dbt execute product_pipeline run
        snow dbt execute product_pipeline test
    
    Copy