【问题标题】:Dynamically passing parameters to an airflow operator through a dictionary通过字典将参数动态传递给气流算子
【发布时间】:2021-12-10 21:47:37
【问题描述】:

例如使用 BashOperator:

bash_task = BashOperator(
     task_id="bash_task",
    bash_command="echo \"here is the message: '$message'\"",
    env={"message": '{{ dag_run.conf["message"] if dag_run else "" }}'},
)

我不想对运算符的参数进行“硬编码”,即“task_id”、“bash_command”等。

而是传递一个字典,例如:

arguments = {
    "task_id": "Bash_task",
    "bash_command": "echo \"here is the message: '$message'\"",
}

bash_task = BashOperator(
    arguments=arguments
)

就我而言,我正在使用更复杂的运算符,并且需要根据某些条件动态设置参数。我的理解是,您可以使用 'params' 参数来做到这一点,但它并没有像我想的那样起作用。我能够通过例如 yaml 文件动态传递参数值,但不能动态选择要设置的参数,以及它们的值将通过例如字典。

这是否可能或最好的方法是什么?

【问题讨论】:

    标签: python templates dynamic operators airflow


    【解决方案1】:

    这是标准 Python 功能(** 运算符)。

    使用**arguments 将字典扩展为关键字参数Converting Python dict to kwargs?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多