在Airflow 2中,可以通过使用'execution_date”来在任务中获取执行日期。
以下是一个示例任务,可以在其中使用'execution_date”:
from datetime import datetime
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
def my_task(execution_date, **context):
print(f"Execution date: {execution_date}")
with DAG(dag_id='my_dag', start_date=datetime(2021, 10, 1), catchup=False) as dag:
task = PythonOperator(
task_id='my_task',
python_callable=my_task,
provide_context=True
)
在上面的示例中,'my_task”'execution_date”作为参数来获取执行日期,并将其打印出来。在PythonOperator中,还需要'provide_context”设置为True,以便上下文变量传递给任务。
上一篇:Airflow2:GoogleSheetsToGCSOperator引发Negsignal.SIGKILL错误。
下一篇:Airflow: "ListObjectsV2操作: 访问被拒绝" 使用GoogleCloudStorageToS3Operator。