-
官网
-
http://airflow.apache.org/
Airflow 是什么
-
Airflow是一个以编程方式创作,安排和监控工作流程的平台。
原理
-
动态: 使用代码(Python)来配置pipelines,允许动态生成,可以写动态实例化pipelines
-
扩展: 轻松自定义算子,执行器,使其符合 适合你环境抽象的级别。
-
优雅的: pipelines 精简、明确, 使用功能强大的Jinja模版引擎将参数脚本置于ariflow的核心
-
可扩展性: 具有模块化结构,并且使用消息队列来协调任意数量的works.
安装
-
pip install apache-airflow
关健概念
-
http://airflow.apache.org/concepts.html
-
DAGs:即有向无环图(Directed Acyclic Graph),将所有需要运行的tasks按照依赖关系组织起来,描述的是所有tasks执行的顺序。
-
Operators:描述了DAG中一个具体的task具体要做的事。其中,airflow内置了很多operators,EmailOperator 用于发送邮件,HTTPOperator 用于发送HTTP请求, SqlOperator 用于执行SQL命令。同时,用户可以自定义Operator,这给用户提供了极大的便利性。
- BashOperator:- 执行bash命令(executes a bash command)
- BranchPythonOperator:- 跟据条件执行下游任务,如果条件不符合,则不执行下游
- PythonOperator:- 执行任意python 函数(calls an arbitrary Python function)
- DummyOperator:什么都不操作,用来做分支使用
- ExternalTaskSensor:依赖上游DAG,判依赖的上游DAG任务是否已经产生,没执行完,不执行下游任务
-
Tasks:Task 是 Operator的一个实例,也就是DAGs中的一个node。
-
Task Instance:task的一次运行。task instance 有自己的状态,包括"running", “success”, “failed”, “skipped”, "up for retry"等。
-
Task Relationships:DAGs中的不同Tasks之间可以有依赖关系,如 TaskA >> TaskB,表明TaskB依赖于TaskA。
调度时间:http://airflow.apache.org/faq.html
-
-
schedule_interval:“0 20 * * *”(小时、日、周、月、年),每天 20 点。
preset | meaning | cron |
---|---|---|
None | Don’t schedule, use for exclusively “externally triggered” DAGs | |
@once |
Schedule once and only once | |
@hourly |
Run once an hour at the beginning of the hour | 0 * * * * |
@daily |
Run once a day at midnight | 0 0 * * * |
@weekly |
Run once a week at midnight on Sunday morning | 0 0 * * 0 |
@monthly |
Run once a month at midnight of the first day of the month | 0 0 1 * * |
@yearly |
Run once a year at midnight of January 1 | 0 0 1 1 * |
默认变量
Variable | Description | 更多详细信息 |
---|---|---|
{ { ds }} | the execution date as YYYY-MM-DD | http://airflow.apache.org/macros.html |
pipeline 定义样例
"""
Code that goes along with the Airflow tutorial located at:
https://github.com/apache/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, 6, 1),
'email': ['airflow@example.com'],
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5),
# 'queue': 'bash_queue',
# 'pool': 'backfill',
# 'priority_weight': 10,
# 'end_date': datetime(2016, 1, 1),
}
dag = DAG('tutorial', default_args=default_args, schedule_interval=timedelta(days=1))
# t1, t2 and t3 are examples of tasks created by instantiating operators
t1 = BashOperator(
task_id='print_date',
bash_command='date',
dag=dag)
t2 = BashOperator(
task_id='sleep',
bash_command='sleep 5',
retries=3,
dag=dag)
templated_command =