dbtプロジェクトオブジェクトのデプロイ

In dbt Projects on Snowflake, deploying a dbt project object means copying your dbt project code into Snowflake to create the object or update it with a new version. You do this with Snowsight, CREATE DBT PROJECT or ALTER DBT PROJECT SQL commands, or the snow dbt deploy command in the Snowflake CLI.

Snowsight を使用してdbtプロジェクトオブジェクトをデプロイする

Deploying a dbt project object in Snowsight takes the dbt code in your workspace and creates a new or updates an existing dbt project object.

Snowsight でdbtプロジェクトオブジェクトをデプロイするには、 dbt depsコマンドを実行 してから、次のステップを完了します。

  1. Snowsight にサインインします。

  2. ナビゲーションメニューで Projects » Workspaces を選択します。

  3. Workspaces メニューで、dbtプロジェクトを含むワークスペースを選択します。

  4. dbtファイルが所定の場所にあることを確認します。

    機能することを確認するため、次のように dbt compiledbt run 、または dbt build コマンドを実行します。

    1. ワークスペースエディターの下で Output タブを開くと、ワークスペースからdbtコマンドを実行した後に標準出力が表示されます。

    2. ワークスペースエディターの上にあるメニューバーで、正しい ProjectProfile が選択されていることを確認します。

    3. コマンドリストから、 dbt compiledbt rundbt build のいずれかを選択してから、実行ボタンを選択します。このステップでは、プロジェクトを解析します。

  5. ワークスペースの右上から、 Connect を選択してから、次のいずれかを選択します。

    • Deploy dbt project で新しいdbtプロジェクトに接続します。最初のデプロイ時、これによりスキーマレベルのdbtプロジェクトオブジェクトが作成されます。

    • Existing dbt deployment to connect to an existing dbt project. Deploying adds a new version to the existing dbt project object (equivalent to ALTER DBT PROJECT ADD VERSION FROM 'snow://workspace/…/versions/last').

  6. Deploy dbt project ポップアップウィンドウで、次を選択します。

    • Select location の下で、データベースとスキーマを選択します。

    • Under Select or Create dbt project, select Create dbt project.

    • 名前と説明を入力します。

    • Optionally, enter a default target to choose which profile will be used for compilation and subsequent runs (for example, prod). The target of a dbt project object execution can still be overridden with --target in ARGS.

    • オプションで、Run dbt deps を選択してから、デプロイ中に自動的に dbt deps を実行する外部アクセス統合を選択します。

  7. Deploy を選択します。

    その Output タブには、Snowflakeで実行されるコマンドが表示されます。これは次の例と同様です。

    CREATE DBT PROJECT mydb.my_dbt_projects_schema.my_dbt_project
      FROM 'snow://workspace/mydb.my_dbt_projects_schema.sales_model/versions/version$2'
      EXTERNAL_ACCESS_INTEGRATIONS = ();
    
    my_dbt_project successfully created.
    

    Connect メニューには、作成したdbtプロジェクトオブジェクトの名前と、次のオプションが表示されるようになります。

    • Redeploy dbt project: Updates the dbt project object with the current workspace version of the project by using ALTER. This increments the version of the dbt project object by one. For more information, see Versions for dbt project objects and files.

    • Disconnect:dbtプロジェクトオブジェクトからワークスペースを切断しますが、dbtプロジェクトオブジェクトは削除しません。

    • Edit project:dbtプロジェクトオブジェクトのコメント、デフォルトターゲット、外部アクセス統合を更新します。

    • View project:オブジェクトエクスプローラーでdbtプロジェクトオブジェクトを開きます。そこにはdbtプロジェクトオブジェクトの CREATE DBT PROJECT コマンドとプロジェクトの実行履歴が表示されます。

    • Create schedule: Provides options for you to create a task that runs the dbt project object on a schedule. For more information, see Schedule execution of dbt project objects on Snowflake.

    • View schedules:dbtプロジェクトオブジェクトを実行するスケジュール(タスク)のリストを開きます。オブジェクトエクスプローラーでタスクの詳細を表示するオプションがあります。

  8. Optionally, confirm your dbt project object exists by running the SHOW DBT PROJECTS command in a worksheet, for example:

    SHOW DBT PROJECTS IN DATABASE mydb;
    

SQL コマンドを使用してdbtプロジェクトオブジェクトをデプロイする

CREATE DBT PROJECT コマンドと ALTER DBT PROJECT コマンドは、それぞれ、ステートメントの FROM 句で指定されたファイルをコピーして、dbtプロジェクトオブジェクトを作成するか、新しいバージョンを追加します。

CREATE DBT PROJECT コマンドは、以下に示すように、単一の初期バージョンで新しいオブジェクトを作成します(例: VERSION$1)。

CREATE DBT PROJECT mydb.my_dbt_projects_schema.my_dbt_project
  FROM '@sales_db.integrations_schema.sales_dbt_git_stage/branches/main'
  DEFAULT_TARGET = 'prod'
  EXTERNAL_ACCESS_INTEGRATIONS = my_dbt_ext_access
  COMMENT = 'Generates sales data models.';

ALTER DBT PROJECT コマンドは、一意のインクリメントされたバージョン番号で、既存のオブジェクト内に新しいバージョンを作成します(例: VERSION$2VERSION$3 など)。

-- Update the Git repository object to fetch the latest code
ALTER GIT REPOSITORY sales_db.integrations_schema.sales_dbt_git_stage FETCH;

-- Add a new version to the dbt project object based on the updated Git repository object
ALTER DBT PROJECT mydb.my_dbt_projects_schema.my_dbt_project
  ADD VERSION
  FROM '@sales_db.integrations_schema.sales_dbt_git_stage/branches/main/sales_dbt_project';

Snowflake CLI を使用してdbtプロジェクトオブジェクトをデプロイする

snow dbt deploy コマンドは、仮ステージにローカルファイルをアップロードし、新しいdbtプロジェクトオブジェクトを作成し、新しいバージョンを作成して更新するか、完全に再作成します。有効なdbtプロジェクトには、2つのファイルが含まれている必要があります。

  • dbt_project.yml:使用するプロファイルを指定する標準のdbt構成ファイル。

  • profiles.yml:dbt_project.yml で参照されるdbt接続プロファイル定義。 profiles.yaml は、データベース、ロール、スキーマ、およびタイプを定義する必要があります。

    • デフォルトでは、 dbt Projects on Snowflake は、dbt環境またはプロファイルから指定されたターゲットスキーマ(target.schema)を使用します。dbt Coreの動作とは異なり、 profiles.yml ファイルで指定されたターゲットスキーマは、正常にコンパイルまたは実行するためには、dbtプロジェクトを作成する前に存在している必要があります。

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

次の例は、 snow dbt deploy コマンドの使用方法を説明しています。

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

    snow dbt deploy jaffle_shop
    
  • 指定されたディレクトリから jaffle_shop という名前のプロジェクトをデプロイし、dbtプロジェクトオブジェクトがすでに存在するかどうかに応じて、新しいバージョンを作成するか追加します。

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

    snow dbt deploy jaffle_shop --source /path/to/dbt/directory
    --profiles-dir ~/.dbt/
    --default-target prod
    --dbt-version 1.10.15
    --external-access-integration dbthub-integration
    --external-access-integration github-integration
    --force
    
  • Deploy a project named jaffle_shop and set a specific version for the dbt project object:

    snow dbt deploy jaffle_shop --dbt-version '1.10.15'
    

ソースファイルの場所

The dbt project object source files can be in any one of the following locations:

  • Gitリポジトリステージ。たとえば:

    '@my_db.my_schema.my_git_repository_stage/branches/my_branch/path/to/dbt_project_or_projects_parent'

    SnowflakeでGitリポジトリオブジェクトを作成し、Gitリポジトリを dbt Projects on Snowflake のワークスペースに接続する方法については、 label-dbt_create_git_integrated_workspace`をご参照ください。ワークスペースを使用せずにGitリポジトリオブジェクトとステージを作成および管理する方法の詳細については、 :doc:/developer-guide/git/git-overview` および :doc:`/sql-reference/sql/create-git-repository`をご参照ください。

  • 既存のdbtプロジェクトステージ。例えば:

    'snow://dbt/my_db.my_schema.my_existing_dbt_project_object/versions/last'

    The version specifier is required and can be last (as shown in the previous example), first, or the specifier for any existing version in the form version$<num>. For more information, see Versions for dbt project objects and files.

  • 内部の名前付きステージ。たとえば:

    '@my_db.my_schema.my_internal_named_stage/path/to/dbt_projects_or_projects_parent'

    内部ユーザーステージとテーブルステージはサポートされていません。

  • Snowflake上のdbtのワークスペース。たとえば:

    'snow://workspace/user$.public."my_workspace_name"/versions/live/path/to/dbt_projects_or_projects_parent'

    ワークスペース名は大文字と小文字が区別され、特殊文字を含めることができるため、ワークスペース名を二重引用符で囲むことをお勧めします。

    The version specifier is required and can be last, first, live, or the specifier for any existing version in the form version$<num>. For more information, see Versions for dbt project objects and files.