Airflow 简介及原理

Airflow 简介及原理

Airflow 一个用于编排复杂计算工作流和数据处理流水线的开源工具,通常可以解决一些复杂超长 Cron 脚本任务或者大数据的批量处理任务。

其工作流的设计是基于有向无环图 (Directed Acyclical Graphs, DAG) ,用于设置任务依赖关系和时间调度。

简单来说,在编写工作流时,尽量考虑如何将一个大型的任务拆分为多个可独立执行的原子任务,再将这些任务合并为一个逻辑整体。

这里简单介绍一些常见的基本概念及其使用方法。

常见名词解释

DAG

代表一个工作流,一个 DAG 中包含多个 Task,多个 Task 组成一个有向无环图。DAG 可以配置调度时间。如下图是一个 DAG,其中有 6 个 Task 按顺序执行。

[dag(img-sTI43mk5-1587706757700)(../images/graph.png)]

DAG 通过 Python 定义,如下

# -*- coding: utf-8 -*-
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

from builtins import range
from datetime import timedelta

import airflow
from airflow.models import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.operators.dummy_operator import DummyOperator

args = {
    'owner': 'Airflow',
    'start_date': airflow.utils.dates.days_ago(2),
}

dag = DAG(
    dag_id='example_bash_operator',
    default_args=args,
    schedule_interval='0 0 * * *',
    dagrun_timeout=timedelta(minutes=60),
)

run_this_last = DummyOperator(
    task_id='run_this_last',
    dag=dag,
)

# [START howto_operator_bash]
run_this = BashOperator(
    task_id='run_after_loop',
    bash_command='echo 1',
    dag=dag,
)
# [END howto_operator_bash]

run_this >> run_this_last

for i in range(3):
    task = BashOperator(
        task_id='runme_' + str(i),
        bash_command='echo "{{ task_instance_key_str }}" && sleep 1',
        dag=dag,
    )
    task >> run_this

# [START howto_operator_bash_template]
also_run_this = BashOperator(
    task_id='also_run_this',
    bash_command='echo "run_id={{ run_id }} | dag_run={{ dag_run }}"',
    dag=dag,
)
# [END howto_operator_bash_template]
also_run_this >> run_this_last

if __name__ == "__main__":
    dag.cli()

Task

Airflow 任务中的最小执行单元,Operator 的实例。,Airflow 中有很多 Operator 供选择,常用的有 BashOperator,用来执行 bash 命令的 Operator。如下表示一个 Task

run_this = BashOperator(
    task_id='run_after_loop',
    bash_command='echo 1',
    dag=dag,
)

Schedule

调度时间,类似于 crontab。用法跟 crontab 一样

Execution_date

执行日期,指当前调度的调度日期。

Airflow 核心组件

Airflow 核心组件有 Web Server,Scheduler,Metadata Database, Executor, Worker。

  • webserver : 提供图形界面,可以监控 dag 运行状态,也可以对 dag 操作。需要注意的一点是,airflow 的 webserver 使用的是 Gunicorn 框架,所以 webserver 只能运行在 Linux/Unix 内核的操作系统中。
  • Scheduler:调度器,你的 dag 是否该执行由调度器管理。
  • metadata database:元数据库,默认为 SQLite,可以支持 MySQL,PostgreSQL。需要注意的是,如果使用 SQLite,并发数只能为 1,所以一般情况下都不会选用SQLite。
  • Executor:定义任务的执行。
  • Worker:用来执行 Executor 接收的任务。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值