相关疑难解决方法(0)

Python Airflow - 返回PythonOperator的结果

我编写了一个包含多个PythonOperator的DAG

task1 = af_op.PythonOperator(task_id='Data_Extraction_Environment',
                          provide_context=True,
                          python_callable=Task1, dag=dag1)

def Task1(**kwargs):
    return(kwargs['dag_run'].conf.get('file'))
Run Code Online (Sandbox Code Playgroud)

从PythonOperator我调用"Task1"方法.该方法返回一个值,该值需要传递给下一个PythonOperator.如何从"task1"变量获取值或如何获取从Task1方法返回的值?

更新 :

    def Task1(**kwargs):
          file_name = kwargs['dag_run'].conf.get[file]
          task_instance = kwargs['task_instance']
          task_instance.xcom_push(key='file', value=file_name) 
          return file_name

  t1 = PythonOperator(task_id = 'Task1',provide_context=True,python_callable=Task1,dag=dag)

  t2 =   BashOperator(
      task_id='Moving_bucket', 
      bash_command='python /home/raw.py {{ task_instance.xcom_pull(task_ids='Task1',key='file') }} ',
      dag=dag,
    )

t2.set_upstream(t1)
Run Code Online (Sandbox Code Playgroud)

python airflow

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

airflow ×1

python ×1