Airflow 介绍

Apache Airflow 是一个用于创建、监控和调度工作流的开源平台。它旨在提供一个直观的界面来定义和管理复杂的任务依赖关系,支持多种执行环境和广泛集成的数据处理工具。通过使用Airflow,开发者可以方便地构建数据管道,实现批处理和实时任务的自动化,提高数据工作流程的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官网

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,这给用户提供了极大的便利性。

    1. BashOperator:- 执行bash命令(executes a bash command)
    2. BranchPythonOperator:- 跟据条件执行下游任务,如果条件不符合,则不执行下游
    3. PythonOperator:- 执行任意python 函数(calls an arbitrary Python function)
    4. DummyOperator:什么都不操作,用来做分支使用
    5. 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 = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值