![](/img/trans.png)
[英]How can I run a python script at the end of a specific DBT model, using the post_hook param within config()?
[英]How can I run python code after a DBT run (or a specific model) is completed?
我希望能够运行一个 ad-hoc python 脚本,该脚本可以访问和运行由 dbt 运行计算的模型的分析,是否有任何最佳实践?
对于生产,我建议使用编排层,例如 apache 气流。
请参阅此博客文章以开始使用,但本质上您将拥有一个编排 DAG(注意 - 不是 dbt DAG),它执行以下操作:
dbt run <with args>
-> your python code
公平的警告,但是,这可能会给您的项目增加一些复杂性。
我想您可以使用 CI/CD 工具(如 github actions 或 circleCI)获得类似的效果
我们最近构建了一个非常适合这种情况的工具。 它利用了在 Python-land 中从 dbt 引用表的便利性。 它被称为fal 。
这个想法是,您将定义要在运行 dbt 模型后运行的 Python 脚本:
# schema.yml
models:
- name: iris
meta:
owner: "@matteo"
fal:
scripts:
- "notify.py"
如果iris
模型在最后一次dbt run
运行,则调用文件 notify.py :
# notify.py
import os
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
CHANNEL_ID = os.getenv("SLACK_BOT_CHANNEL")
SLACK_TOKEN = os.getenv("SLACK_BOT_TOKEN")
client = WebClient(token=SLACK_TOKEN)
message_text = f"""Model: {context.current_model.name}
Status: {context.current_model.status}
Owner: {context.current_model.meta['owner']}"""
try:
response = client.chat_postMessage(
channel=CHANNEL_ID,
text=message_text
)
except SlackApiError as e:
assert e.response["error"]
每个脚本都使用对当前模型的引用运行,该模型在context
变量中运行。
要开始使用 fal,只需pip install fal
并开始编写您的 Python 脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.