[英]pull xcom value inside custom operator airflow
I wrote a custom operator called HadoopPutHdfs
in Airflow, so I need to pass xxx
parameter to HadoopPutHdfs
and I need to fill xxx
with the return value from the generate_file_path
task我在 Airflow 中编写了一个名为
HadoopPutHdfs
的自定义操作符,所以我需要将xxx
参数传递给HadoopPutHdfs
并且我需要用generate_file_path
任务的返回值填充xxx
with DAG(dag_id='my_custom_operator_dag', schedule_interval='1 * * * *', default_args=default_args, catchup=False) as dag:
generate_file_path = PythonOperator(
task_id='generate_file_path',
python_callable=generate_file_path_func,
dag=dag,
)
put_to_hdfs = HadoopPutHdfs(
task_id='put_to_hdfs',
headers={'Content-Type': 'text/plain'},
hdfs_path='webhdfs/v1/user/hive/13.zip',
hadoop_host='10.10.10.146',
hadoop_port=9870,
source_path='/opt/airflow/dags/1.zip',
dag=dag,
xxx= "{{ ti.xcom_pull(task_ids=['generate_file_path']) }}",
)
this line not work ,这条线不行,
xxx= "{{ ti.xcom_pull(task_ids=['generate_file_path']) }}"
How can I pass the amount of generate_file_path
function to xxx
perameter?我怎样才能量传递
generate_file_path
功能xxx
perameter?
Sounds like you are missing the definition of xxx
as a template_field
in your custom operator.听起来您在自定义运算符中缺少
xxx
作为template_field
的定义。 For example:例如:
class CustomDummyOperator(BaseOperator):
template_fields = ('msg_from_previous_task',)
@apply_defaults
def __init__(self,
msg_from_previous_task,
*args, **kwargs) -> None:
super(CustomDummyOperator, self).__init__(*args, **kwargs)
self.msg_from_previous_task = msg_from_previous_task
def execute(self, context):
print(f"Message: {self.msg_from_previous_task}")
DAG: DAG:
def return_a_str():
return "string_value_from_op1"
task_1 = PythonOperator(
task_id='task_1',
dag=dag,
python_callable=return_a_str,
)
task_2 = CustomDummyOperator(
task_id='task_2',
dag=dag,
msg_from_previous_task="{{ ti.xcom_pull(task_ids='task_1') }}"
)
The output from task_2
is: Message: string_value_from_op1
task_2
的输出是: Message: string_value_from_op1
You could use XcomArg for a cleaner syntax:您可以使用XcomArg来获得更简洁的语法:
task_2 = CustomDummyOperator(
task_id='task_2',
dag=dag,
msg_from_previous_task=task_1.output
# msg_from_previous_task="{{ ti.xcom_pull(task_ids='task_1') }}"
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.