Airflowの公式ドキュメントを読んでみる What is Airflow?

Airflowのドキュメントをざっと読んでみます。
ドキュメントを読むだけだと本当にざっと通り過ぎた感じで自分の中に効率良くインプットするのが難しいので、読んだ内容を自分の言い回しに落とし込むことで理解を深めたいと思います。
airflow.apache.org

まずは上記のAirflowの概要から読みます。

以下、公式ドキュメントの文章を引用しつつ、自分の解釈を書いていきます。

What is Airflow™?

Apache Airflow™ is an open-source platform for developing, scheduling, and monitoring batch-oriented workflows. Airflow’s extensible Python framework enables you to build workflows connecting with virtually any technology. A web interface helps manage the state of your workflows. Airflow is deployable in many ways, varying from a single process on your laptop to a distributed setup to support even the biggest workflows.

  • Apache AirflowはOSSで、バッチのワークフローを開発・スケジューリング・監視するもの。
  • Airflowは拡張性のあるPythonフレームワークで仮想的に様々なテクノロジーにつながるワークフローを構築できる
  • Web UIでワークフローの状態を管理しやすい
  • Airflowは様々な方法でデプロイが可能。

Workflows as code

The main characteristic of Airflow workflows is that all workflows are defined in Python code. “Workflows as code” serves several purposes:

Dynamic: Airflow pipelines are configured as Python code, allowing for dynamic pipeline generation.

Extensible: The Airflow™ framework contains operators to connect with numerous technologies. All Airflow components are extensible to easily adjust to your environment.

Flexible: Workflow parameterization is built-in leveraging the Jinja templating engine.

Airflowのワークフローの主な特徴は全てのワークフローがPythonで定義されること。Workflows as codeはいくつかの目的を持つ。 - Dynamic: AirflowパイプラインはPythonで実装され、動的なパイプラインの生成を許容できる - Extensible: Airflowフレームワークは他の技術と接続するためのoperatorsを含んでいる。 - Flexible: ワークフローのパラメータ化はJinjaテンプレートエンジンの組み込みの機能

以下はドキュメントにあったサンプルコード

from datetime import datetime

from airflow import DAG
from airflow.decorators import task
from airflow.operators.bash import BashOperator

# A DAG represents a workflow, a collection of tasks
with DAG(dag_id="demo", start_date=datetime(2022, 1, 1), schedule="0 0 * * *") as dag:

    # Tasks are represented as operators
    hello = BashOperator(task_id="hello", bash_command="echo hello")

    @task()
    def airflow():
        print("airflow")

    # Set dependencies between tasks
    hello >> airflow()

A DAG named “demo”, starting on Jan 1st 2022 and running once a day. A DAG is Airflow’s representation of a workflow.

Two tasks, a BashOperator running a Bash script and a Python function defined using the @task decorator

>> between the tasks defines a dependency and controls in which order the tasks will be executed

  • demoという名前のDAGは2022年1月1日にスタートし、1日一回実行される。DAGはAirflowのワークフローを表すもの

  • タスクはBashスクリプトを実行するBashOperatordと@taskデコレータを使用したPythonの関数の二つがある

  • task間の>>はタスクが実行される順番や依存関係を定義している

Airflow evaluates this script and executes the tasks at the set interval and in the defined order. The status of the “demo” DAG is visible in the web interface:

Airflowはこのスクリプトを評価し、タスクを設定された感覚と決められた順番に実行する。DAG「demo」のステータスはWeb画面から確認できる。

This example demonstrates a simple Bash and Python script, but these tasks can run any arbitrary code. Think of running a Spark job, moving data between two buckets, or sending an email. The same structure can also be seen running over time:

この例ではシンプルなBashPythonスクリプトを実行しているが、タスクはもっと幅広いコードを実行できる。Sparkジョブのように、二つのS3バケット間でデータを移行させたりEmailを送るなど。

Why Airflow™?

Airflow™ is a batch workflow orchestration platform. The Airflow framework contains operators to connect with many technologies and is easily extensible to connect with a new technology. If your workflows have a clear start and end, and run at regular intervals, they can be programmed as an Airflow DAG.

実装したいワークフローが明確な開始と終了時刻が決められて固定された間隔で実行されるならAirflow DAGで実装ができる。

If you prefer coding over clicking, Airflow is the tool for you. Workflows are defined as Python code which means:

Workflows can be stored in version control so that you can roll back to previous versions

Workflows can be developed by multiple people simultaneously

Tests can be written to validate functionality

Components are extensible and you can build on a wide collection of existing components

画面をクリックするよりコーディングが良いならAirflowが向いている。Pythonでワークフローが定義されるということは

  • ワークフローはバージョン管理下におくことができ、以前のバージョンにロールバックも可能
  • ワークフローは複数の開発者で開発できる
  • 機能を確認するためのテストが書ける
  • コンポーネントに拡張性を持たせられる

Why not Airflow™?

Airflow™ was built for finite batch workflows. While the CLI and REST API do allow triggering workflows, Airflow was not built for infinitely-running event-based workflows. Airflow is not a streaming solution. However, a streaming system such as Apache Kafka is often seen working together with Apache Airflow. Kafka can be used for ingestion and processing in real-time, event data is written to a storage location, and Airflow periodically starts a workflow processing a batch of data.

Airflowはバッチワークフローのためのものであり、イベントにより実行されるワークフローには向いていない。
Airflowはストリーミングに使われるものではない。
ただし、Apache KafkaなどのストリーミングシステムはしばしばAirflowと組み合わせて使われる。

If you prefer clicking over coding, Airflow is probably not the right solution. The web interface aims to make managing workflows as easy as possible and the Airflow framework is continuously improved to make the developer experience as smooth as possible. However, the philosophy of Airflow is to define workflows as code so coding will always be required.

コーディングよりも画面をクリックする方がよいならAirflowはおそらく向いていない。
Web画面によりワークフローの管理などはできるものの、Airflowの哲学はワークフローをコードで定義することであり、常にコーディングが必要となる。