You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取DAG的启动计划日期时间?是否可通过TS实现?

如何获取Airflow DAG的启动日期时间
  • 你想的没错,Airflow里的ts变量就是用来获取DAG调度执行的启动日期时间的——也就是DAG运行的逻辑日期,和SQL脚本任务实际启动的时间完全无关。
  • ts是Airflow的内置宏变量,格式一般是YYYY-MM-DDTHH:MM:SS,对应的就是DAG实例的执行时间戳,完全符合你要的需求。
  • 要是你需要在SQL脚本里用到这个时间,直接在任务里引用宏变量就行,举个PythonOperator的例子:
    from airflow import DAG
    from airflow.operators.python import PythonOperator
    from datetime import datetime
    
    def execute_sql_script(**context):
        # 从上下文获取DAG启动时间
        dag_start_time = context['ts']
        # 这里执行你的SQL逻辑,把dag_start_time作为参数传入即可
        print(f"DAG的启动时间是: {dag_start_time}")
    
    # 定义DAG
    with DAG(
        dag_id="sql_execution_dag",
        start_date=datetime(2024, 1, 1),
        schedule_interval="@daily",
        catchup=False
    ) as dag:
        run_sql_task = PythonOperator(
            task_id="run_sql_script",
            python_callable=execute_sql_script,
            provide_context=True  # 允许传递上下文变量
        )
    
  • 另外还有个execution_date宏,和ts指向的是同一个时间,区别在于execution_date是datetime对象,ts是字符串格式,你可以根据实际场景选择使用。

内容的提问来源于stack exchange,提问作者Maria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 23:53:31