Schedule execution of dbt project objects on Snowflake¶
You can use Snowflake tasks to schedule execution of a dbt project object with the EXECUTE DBT PROJECT command. From a workspace, you can quickly create and schedule a user-managed task to execute a connected dbt project object. You can also use SQL commands to create a task directly. If a workspace is connected to a dbt project object, you can view all tasks that run the EXECUTE DBT PROJECT command for that object.
您必须创建一个任务,该任务在与 dbt 项目对象相同的数据库和架构中运行 EXECUTE DBT PROJECT 命令。
备注
Serverless tasks can't be used to execute dbt project objects. You must specify a user-managed warehouse when creating a task that executes the EXECUTE DBT PROJECT command.
从工作区中创建任务¶
When you create a schedule from within a workspace, Snowflake creates a user-managed task in the same database and schema as the dbt project object. The task runs with the privileges of the task owner, but task runs are not associated with a user.
从工作区创建用于计划 dbt 项目对象执行的任务:
From the dbt project menu on the right side of the project pane, under Scheduled runs, choose Create schedule.
在 Schedule a dbt run 对话框中,执行以下操作:
对于 Schedule name,输入任务的名称。
对于 Frequency,使用 at 限定符选择介于 Hourly 到 Monthly 之间的频率,或者选择 Custom 并输入 Cron 表达式。有关计划任务的更多信息,请参阅 CREATE TASK 命令参考中的 SCHEDULE = ...。
在 dbt properties 下:
对于 Operation,选择要按计划执行的 dbt 命令。有关支持的命令列表,请参阅 支持的 dbt 命令和标志。
对于 Profile,选择在 dbt 项目的
profiles.yml文件中定义的配置文件之一。对于 Additional flags,输入 dbt 命令的任何其他 命令行选项 (https://docs.getdbt.com/reference/global-configs/about-global-configs#available-flags)。
选择 Create。
Snowflake 会创建一个任务,该任务使用您指定的参数运行 EXECUTE DBT PROJECT 命令。
从工作区中查看任务¶
From within a workspace, you can view all tasks in the database and schema that execute the connected dbt project object. You can choose a task to view its details in the object explorer, including the task definition, the run history of the task, and the task graph.
从工作区查看与 dbt 项目对象关联的任务:
从 dbt 项目菜单中,选择 View schedules,然后从列表中选择您的计划(任务)。
该任务的 Task Details 将在对象资源管理器中打开。任务详细信息、构成任务定义的 SQL 语句以及授予该任务对象的权限均会显示。
选择 Run History 选项卡可查看任务运行历史记录,或者选择 Task Graph 选项卡可查看此任务与 :doc:`任务图 </user-guide/tasks-graphs>`(如果适用)中其他任务的关系。
有关更多信息,请参阅 View tasks and task graphs in Snowsight。
使用 SQL 创建任务¶
您可以使用 CREATE TASK 命令来创建任务以运行 EXECUTE DBT PROJECT 命令。使用 SQL 创建任务来通过不同 dbt CLI 选项执行不同的 dbt 命令,为 Snowflake 中的 dbt 部署提供了强大的编排能力。
以下 SQL 示例会为生产 dbt 目标创建任务,该任务每隔六小时执行一次 dbt run 命令。
然后,以下 SQL 会创建一个任务,该任务在每次完成前一个 run_dbt_project 任务后执行 dbt test 命令。