如何设置DagRun的execution_date?

时间:2018-03-10 19:40:23

标签: airflow

如果DAG的{​​{1}}在特定日期运行,那么相应start_date的{​​{1}}如何定义?

我已阅读documentation,但有一个例子令我困惑:

execution_date

假设DAGRun在2016-01-02上午6点运行,则第一个""" Code that goes along with the Airflow tutorial located at: https://github.com/airbnb/airflow/blob/master/airflow/example_dags/tutorial.py """ from airflow import DAG from airflow.operators.bash_operator import BashOperator from datetime import datetime, timedelta default_args = { 'owner': 'airflow', 'depends_on_past': False, 'start_date': datetime(2015, 12, 1), 'email': ['airflow@example.com'], 'email_on_failure': False, 'email_on_retry': False, 'retries': 1, 'retry_delay': timedelta(minutes=5), 'schedule_interval': '@hourly', } dag = DAG('tutorial', catchup=False, default_args=default_args) 将有DAG的2016-01-01,如上所述文档

  

下一个将在早上午夜之后创建   2016-01-03,执行日期为2016-01-02

以下是我设置DAGRun

的方法

execution_dateexecution_date设置为每小时并在2016-01-02上午6点运行,第一个DAG的{​​{1}}将是设置为2016-01-02在上午7点,第二个到2016-01-02在上午8点...等等。

2 个答案:

答案 0 :(得分:3)

我们的分析师从编写气流障碍中得到了很多问题。

每次dag运行都包含一个 时间段 ,并带有一个开始和结束。

开始=执行日期

结束=创建并执行dag运行时( next_execution_date

应该有用的示例:

Schedule interval: '0 0 * * *' (run daily at 00:00:00 UTC)
Start date: 2019-10-01 00:00:00

10/1 00:00           10/2 00:00
*<------------------>*
 < your 1st dag run >
^ execution_date
  next_execution_date^
                     ^when this 1st dag run is actually created by the scheduler

正如@simond在评论中指出的那样,“ execution_date”对于这个变量来说是一个不好的名字。它既不是日期,也不代表执行日期。 las,我们对气流创建者提供给我们的东西感到困惑...如果我想让dag运行的日期时间执行我的代码,我可以使用next_execution_date很有帮助。

答案 1 :(得分:2)

这就是Airflow中的调度工作方式。我认为,当您考虑正常ETL批处理过程如何运行以及如何使用execution_date来获取已更改的增量记录时,以Airflow的方式执行此操作是有意义的。

假设我们要安排批处理作业每晚运行以从某个源数据库中提取新记录。我们想要从1/1/2018起改变所有记录(我们希望所有记录也在1日更改)。为此,您需要将DAG的start_date设置为1/1/2018,调度程序将运行多次,但是当它到达2/1/2018(或很快之后)时,它将运行我们的DAG {1/1/2018的execution_date

现在我们可以将一个SQL语句发送到源数据库,该数据库使用execution_date作为SQL的一部分,使用JINJA模板。 SQL看起来像:

SELECT row1, row2, row3
FROM table_name
WHERE timestamp_col >= {{ execution_date }} and timestamp_col < {{ next_execution_date }}

我认为当你以这种方式看待它时会更有意义,尽管我承认我在开始时难以理解这一点。

以下是文档https://airflow.apache.org/scheduler.html的引用:

  

调度程序在开始日期之后,在期间结束时运行您的作业一个schedule_interval。

另外值得注意的是,您从文档中查看的示例是在禁用回填时描述计划的行为。如果启用了回填,则在2015年1月12日之间每隔1小时创建一次DAG运行,如果之前从未运行过DAG,则会创建当前日期。

相关问题