airflow - Airflow :PythonOperator:为什么要包含 'ds' arg?

标签 airflow

在定义一个函数以供以后用作python_callable时,为什么将“ds”作为函数的第一个参数?

例如:

def python_func(ds, **kwargs):
    pass

我查看了Airflow文档,但找不到任何解释。

最佳答案

这与provide_context=True参数有关。根据气流文档,

if set to true, Airflow will pass a set of keyword arguments that can be used in your function. This set of kwargs correspond exactly to what you can use in your jinja templates. For this to work, you need to define **kwargs in your function header.


ds是这些关键字参数之一,并以“YYYY-MM-DD”格式表示执行日期。对于在文档中标记为(模板化)的参数,可以使用'{{ ds }}'默认变量来传递执行日期。您可以在此处阅读有关默认变量的更多信息:

https://pythonhosted.org/airflow/code.html?highlight=pythonoperator#default-variables(作废)

https://airflow.incubator.apache.org/concepts.html?highlight=python_callable

PythonOperator没有模板化参数,因此可以做类似
python_callable=print_execution_date('{{ ds }}')

将无法正常工作。要在PythonOperator的callable函数中打印执行日期,您将需要执行以下操作:
def print_execution_date(ds, **kwargs):
    print(ds)

要么
def print_execution_date(**kwargs):
    print(kwargs.get('ds'))

希望这可以帮助。

关于airflow - Airflow :PythonOperator:为什么要包含 'ds' arg?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40531952/

相关文章:

airflow - 尝试在 Airflow 上使用 SendGrid 时出现错误 400(Google Composer)

python - Airflow 如何使用 Oracle Hook 执行查询

amazon-web-services - 如何将 DBT 与 AWS Managed Airflow 结合使用?

kubernetes - 从 Airflow KubernetesPodOperator 访问 Kubernetes Secret

python-3.x - Airflow 调度程序意外关闭

flask - Airflow 插件,启用 RBAC 的蓝图不起作用

python - 将模板变量传递给 HiveOperator

python - 调用 BashOperator 时出错 : Bash command failed

python - 无法从另一个文件夹导入类

airflow - dag 中 Airflow 任务的状态