如何在气流中使用默认变量

时间:2018-04-05 06:01:36

标签: airflow

我们如何使用airflow中显示在下方链接中的默认变量 https://airflow.apache.org/code.html#default-variables

我在我的代码中使用了这个,如下所示:

def decide_which_task():

    if {{ dag_run.task_id }}  is  "Move_file":    
            return "move_file"    
        else:    
            return "push_to_db"

但我在日志文件中收到错误

  

NameError:未定义全局名称'dag_run'

1 个答案:

答案 0 :(得分:2)

您尝试使用的注释是Jinja在字符串中模板化的注释。

要在任务中使用相同的变量,您需要:

  • 使用provide_context=True
  • 中的参数PythonOperator
  • 将功能签名更改为def decide_which_task(**context):
  • 然后从上下文中访问变量,例如mytask = context['task_id']

代码:

def decide_which_task(**context):

if context['task_id']  is  "Move_file":    
        return "move_file"    
    else:    
        return "push_to_db"