什么是 Airflow 运算符中的上下文变量

What is context variable in Airflow operators

我试图了解 Airflow 运算符中称为上下文的这个变量是什么。 as example:

def execute(self, **context**).

它来自哪里?我在哪里可以设置它?何时以及如何在我的函数中使用它? 另一个问题是什么是 *context 和 **context? 我看到几个像这样使用这个变量的例子:

def execute(self, *context) / def execute(self, **context). 

有什么区别,什么时候应该使用 *context 和 **context

当 Airflow 运行任务时,它会收集多个变量并将这些变量传递给 execute() 方法的 context 参数。这些变量包含有关当前任务的信息,您可以在此处找到列表:https://airflow.apache.org/docs/apache-airflow/stable/macros-ref.html#default-variables.

上下文中的信息可用于您的任务,例如引用文件夹 yyyymmdd,其中日期是从变量 ds_nodash 中获取的,[=12= 中的变量]:

def do_stuff(**context):
    data_path = f"/path/to/data/{context['ds_nodash']}"
    # write file to data_path...

PythonOperator(task_id="do_stuff", python_callable=do_stuff)

*context**context 是不同的 Python 符号,用于在函数中接受参数。 Google“args vs kwargs”以查找有关此主题的更多信息。基本上 *context 接受非关键字参数,而 **context 接受关键字参数:

def print_context(*context_nokeywords, **context_keywords):
    print(f"Non keywords args: {context_nokeywords}")
    print(f"Keywords args: {context_keywords}")

print_context("a", "b", "c", a="1", b="2", c="3")

# Non keywords args: ('a', 'b', 'c')
# Keywords args: {'a': '1', 'b': '2', 'c': '3'}