[英]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 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"])
A similar version of the above code, using only XComArgs (feature behind @task
), can be done as follows上述代码的类似版本,仅使用 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
However, that way I can't pass the outputs as arguments separately as done using @task
但是,这样我就不能像使用
@task
那样单独将输出作为 arguments 传递
So, does anyone know any workaround to allow something like the code bellow?那么,有没有人知道任何解决方法来允许类似下面的代码?
show_task = PythonOperator(
task_id="show",
python_callable=show,
op_args=[values_task.output["value_one"], values_task.output["value_two"]],
)
The provided example does works if you use op_kwargs
instead of op_args
.如果您使用
op_kwargs
而不是op_args
,则提供的示例确实有效。 That is related to the value being pushed to XCom
from the return value of the values()
function.这与从
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
Logs output:日志 output:
[2021-05-13 23:36:15,084] {logging_mixin.py:104} INFO - I'm a value! I'm another value!
If you return a list
from the first task, then you could use op_args
since they will get unpacked in your callable.如果您从第一个任务返回一个
list
,那么您可以使用op_args
因为它们将在您的可调用文件中解包。 I hope that works for you!我希望这对你有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.