部署 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 rundbt build 命令,如下所示:

    1. 在工作区编辑器下方,打开 Output 选项卡,以便在从工作区运行 dbt 命令后可以看到标准输出。

    2. 从工作区编辑器上方的菜单栏中,确认已选择正确的 ProjectProfile

    3. 从命令列表中选择 dbt compiledbt rundbt build,然后选择执行按钮。此步骤将解析您的项目。

  5. 从工作区的右上角选择 Connect,然后选择以下选项之一:

    • 用于连接新的 dbt 项目的 Deploy dbt project。首次部署时,这将创建一个架构级 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 PROJECTALTER 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 项目必须包含两个文件:

  • dbt_project.yml:选择使用 时默认使用的角色和仓库。标准 dbt 配置文件,用于指定要使用的配置文件。

  • profiles.yml:选择使用 时默认使用的角色和仓库。dbt_project.yml 中引用的 dbt 连接配置文件定义。profiles.yaml 必须定义数据库、角色、架构和类型。

    • 默认情况下,dbt Projects on Snowflake 使用在您的 dbt 环境或配置文件中指定的目标架构 (target.schema)。与 dbt Core 行为不同,在创建 dbt 项目之前,在 profiles.yml 文件中指定的目标架构必须存在,才能成功编译或执行。

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

以下示例说明了如何使用 snow dbt deploy 命令:

  • 部署名为 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 中为 dbt Projects on Snowflake 创建将 Git 存储库连接到工作区的 Git 存储库对象的更多信息,请参阅 创建连接到 Git 存储库的工作区。有关在不使用工作区的情况下创建和管理 Git 存储库对象和暂存区的更多信息,请参阅 在 Snowflake 中使用 Git 存储库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.