使用 Snowflake CLI 管理 dbt Projects on Snowflake¶
备注
Snowflake CLI 中的 dbt Projects on Snowflake 功能仅在版本 3.13.0 或更高版本中可用。
您可以使用 Snowflake CLI 通过以下操作管理 dbt 项目:
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 indbt_project.yml.profiles.yamlmust define the database, role, schema, and type.默认情况下,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
Deploy a project named
jaffle_shopfrom 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
使用自定义配置文件目录并启用 外部访问集成,从指定目录部署名为
jaffle_shop的项目: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
Listing all available dbt project objects¶
The snow dbt list command lists all available dbt project objects on Snowflake.
以下示例说明了如何使用 snow dbt list 命令:
列出所有可用的 dbt 项目对象:
snow dbt list
列出
product数据库中名称以JAFFLE开头的 dbt 项目对象:snow dbt list --like JAFFLE% --in database product
Executing a dbt project object command¶
The snow dbt execute command executes one of the following dbt commands (https://docs.getdbt.com/reference/dbt-commands) on a Snowflake dbt project object:
build (https://docs.getdbt.com/reference/commands/build)
compile (https://docs.getdbt.com/reference/commands/compile)
deps (https://docs.getdbt.com/reference/commands/deps)
list (https://docs.getdbt.com/reference/commands/list)
parse (https://docs.getdbt.com/reference/commands/parse)
retry (https://docs.getdbt.com/reference/commands/retry)
run (https://docs.getdbt.com/reference/commands/run)
run-operation (https://docs.getdbt.com/reference/commands/run-operation)
seed (https://docs.getdbt.com/reference/commands/seed)
show (https://docs.getdbt.com/reference/commands/show)
snapshot (https://docs.getdbt.com/reference/commands/snapshot)
test (https://docs.getdbt.com/reference/commands/test)
有关使用 dbt 命令的更多信息,请参阅 dbt 命令参考 (https://docs.getdbt.com/reference/dbt-commands)。
以下示例说明了如何使用 snow dbt execute 命令:
执行 dbt
test命令:snow dbt execute jaffle_shop test
异步执行
rundbt 命令:snow dbt execute --run-async jaffle_shop run --select @source:snowplow,tag:nightly models/export
Describing a dbt project object¶
The snow dbt describe command describes a dbt project object on Snowflake.
以下示例描述了 Snowflake 上名为 my_dbt_project 的 dbt 项目对象:
snow dbt describe my_dbt_project
Dropping a dbt project object¶
The snow dbt drop command deletes a dbt project object on Snowflake.
以下示例删除了 Snowflake 上名为 my_dbt_project 的 dbt 项目对象:
snow dbt drop my_dbt_project
Use snow dbt commands in a CI/CD workflow¶
备注
在构建 CI/CD 工作流程时,您只需要您的 git 服务器,例如 Github 和 Snowflake CLI。不需要 Git 存储库对象。
您可以使用 Snowflake CLI 运行 dbt 命令来构建 CI/CD 管道。这些管道通常用于测试新代码,例如新的提取请求,或者在某些内容合并到主分支时更新生产应用程序。
要使用 snow dbt 命令构建 CI/CD 工作流程,请执行以下步骤:
准备您的 dbt 项目:
下载您的 dbt 项目或开始一个新项目。
确保主项目目录包含
dbt_project.yml和profiles.yml文件。验证
dbt_project.yml中引用的配置文件名称是否在profiles.yml中定义。备注
Snowflake 的 dbt 项目对象不需要密码,因此,如果
profiles.yml包含任何密码,部署将停止直至密码被移除。
设置 Snowflake CLI GitHub 操作。
定义您的工作流程。
根据组织的需求确定您的工作流程需要运行哪些命令。以下示例说明了一个 CI 工作流程,该工作流程使用新文件更新名为
product_pipeline的 dbt 项目对象的版本,运行转换,最后运行测试:- name: Execute Snowflake CLI command run: | snow dbt deploy product_pipeline snow dbt execute product_pipeline run snow dbt execute product_pipeline test