【发布时间】:2021-11-10 18:59:19
【问题描述】:
我希望能够运行一个临时 python 脚本,该脚本将访问并在由 dbt 运行计算的模型上运行分析,是否有任何最佳实践?
【问题讨论】:
我希望能够运行一个临时 python 脚本,该脚本将访问并在由 dbt 运行计算的模型上运行分析,是否有任何最佳实践?
【问题讨论】:
我们最近构建了一个非常适合这种情况的工具。它利用了在 Python 领域从 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 脚本。
【讨论】:
对于生产,我建议使用编排层,例如 apache airflow。
请参阅this blog post 以开始使用,但基本上您将拥有一个编排 DAG(注意 - 不是 dbt DAG),它执行以下操作:
dbt run <with args> -> your python code
公平的警告,不过,这可能会给您的项目增加一点复杂性。
我想你可以使用 github actions 或 circleCI 等 CI/CD 工具获得类似的效果
【讨论】: