簡體   English   中英

有沒有辦法將多個 arguments 傳遞給可通過 Airflow 中的 XComArgs 調用的 python?

[英]Is there any way to pass multiple arguments to a python callable through XComArgs in Airflow?

Since version 2.0.0 of Apache Airflow, we can pass the output from one function as an input to another, easily through the decorator @task , as shown in the code below

from airflow import DAG
from airflow.decorators import task
from airflow.utils.dates import days_ago
from airflow.operators.python import PythonOperator

@task(multiple_outputs=True)
def values():
    return {
        "value_one": "I'm a value!",
        "value_two": "I'm another value!",
    }

@task
def show(value_one, value_two):
    print(value_one, value_two)

with DAG(
    dag_id="task_decorator",
    start_date=days_ago(1),
    schedule_interval=None,
) as dag:

    values = values()
    show(values["value_one"], values["value_two"])

上述代碼的類似版本,僅使用 XComArgs( @task后面的功能),可以按如下方式完成

from airflow import DAG
from airflow.utils.dates import days_ago
from airflow.operators.python import PythonOperator

def values():
    return {
        "value_one": "I'm a value!",
        "value_two": "I'm another value!",
    }

def show(value):
    print(value)

with DAG(
    dag_id="task_decorator",
    start_date=days_ago(1),
    schedule_interval=None,
) as dag:

    values_task = PythonOperator(
        task_id="values",
        python_callable=values,
    )
    show_task = PythonOperator(
        task_id="show",
        python_callable=show,
        op_args=[values_task.output],
    )
    values_task >> show_task

但是,這樣我就不能像使用@task那樣單獨將輸出作為 arguments 傳遞

那么,有沒有人知道任何解決方法來允許類似下面的代碼?

show_task = PythonOperator(
    task_id="show",
    python_callable=show,
    op_args=[values_task.output["value_one"], values_task.output["value_two"]],
)

如果您使用op_kwargs而不是op_args ,則提供的示例確實有效。 這與從values() function 的返回值推送到XCom的值有關。

from airflow import DAG
from airflow.utils.dates import days_ago
from airflow.operators.python import PythonOperator

def values():
    return {
        "value_one": "I'm a value!",
        "value_two": "I'm another value!",
    }


def show(value_one, value_two):
    print(value_one, value_two)


with DAG(
    dag_id="task_decorator",
    start_date=days_ago(1),
    schedule_interval=None,
) as dag:

    values_task = PythonOperator(
        task_id="values",
        python_callable=values
    )

    show_task = PythonOperator(
        task_id="show",
        python_callable=show,
        op_kwargs=values_task.output
    )
    values_task >> show_task

日志 output:

[2021-05-13 23:36:15,084] {logging_mixin.py:104} INFO - I'm a value! I'm another value!

如果您從第一個任務返回一個list ,那么您可以使用op_args因為它們將在您的可調用文件中解包。 我希望這對你有用!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM