简体   繁体   English

DBT运行(或特定模型)完成后如何运行python代码?

[英]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.

相关问题 如何使用 config() 中的 post_hook 参数在特定 DBT 模型的末尾运行 python 脚本? - How can I run a python script at the end of a specific DBT model, using the post_hook param within config()? 如何在特定时间间隔后自动运行我的 python model? - How can i run my python model automatically after specific time interval? 装饰功能完成后,如何让Python装饰器运行? - How can I get a Python decorator to run after the decorated function has completed? 如何从 python 命令行运行 DBT 模型? - How do I run DBT models from python command line? python flask app.run 我如何在 python 代码中自动停止 Z319C3206A7F10C17C3B91116DDA4 - python flask app.run how do I automatically in python code stop flask after all tests are completed 如何在Ruby中运行Python代码? - How can I run Python code in Ruby? 如何在 Python 中的每个单元测试之前和之后运行特定代码 - How to a run specific code before & after each unit test in Python 如何解决在本地运行我的 python 代码? - How can I solve to run my python code run localy? 如何在Selenium中运行代码以在一天中的特定时间在python脚本而非cron中执行功能 - How can I run code in selenium to execute a function at a specific hour of the day within the python script not cron 如何在Python中的特定时间运行某个函数? - How can I run a certain function for a specific time in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM