[英]How can I run python code after a DBT run (or a specific model) is completed?
我希望能够运行一个 ad-hoc python 脚本,该脚本可以访问和运行由 dbt 运行计算的模型的分析,是否有任何最佳实践?
For production, I'd recommend an orchestration layer such as apache airflow.对于生产,我建议使用编排层,例如 apache 气流。
See this blog post to get started, but essentially you'll have an orchestration DAG (note - not a dbt DAG) that does something like:请参阅此博客文章以开始使用,但本质上您将拥有一个编排 DAG(注意 - 不是 dbt DAG),它执行以下操作:
dbt run <with args>
-> your python code
dbt run <with args>
-> your python code
Fair warning, though, this can add a bit of complexity to your project.公平的警告,但是,这可能会给您的项目增加一些复杂性。
I suppose you could get a similar effect with a CI/CD tool like github actions or circleCI我想您可以使用 CI/CD 工具(如 github actions 或 circleCI)获得类似的效果
We recently built a tool that could that caters very much to this scenario.我们最近构建了一个非常适合这种情况的工具。 It leverages the ease of referencing tables from dbt in Python-land.
它利用了在 Python-land 中从 dbt 引用表的便利性。 It's called fal .
它被称为fal 。
The idea is that you would define the python scripts you would like to run after your dbt models are run:这个想法是,您将定义要在运行 dbt 模型后运行的 Python 脚本:
# schema.yml
models:
- name: iris
meta:
owner: "@matteo"
fal:
scripts:
- "notify.py"
And then the file notify.py is called if the iris
model was run in the last dbt run
:如果
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"]
Each script is ran with a reference to the current model for which it is running in a context
variable.每个脚本都使用对当前模型的引用运行,该模型在
context
变量中运行。
To start using fal, just pip install fal
and start writing your python scripts.要开始使用 fal,只需
pip install fal
并开始编写您的 Python 脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.