![](/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.