创建生产管道

Python 中的 Apache Airflow 入门

Mike Metzger

Data Engineer

运行 Dags 和任务

从命令行运行特定任务:

airflow tasks test <dag_id> <task_id> <date>

运行整个 Dag:

airflow dags trigger --logical-date <date> <dag_id>
Python 中的 Apache Airflow 入门

任务要点回顾

  • @task - 将某个 Python 函数指定为 Airflow 任务
  • @task.bash - 将 bash 命令输出作为 Airflow 任务结果返回
  • @task.branch - 分支算子,用于在 Dag 中提供运行时选择
  • FileSensor - 需要 filepath 参数,可能还需 modepoke_interval 属性
Python 中的 Apache Airflow 入门

模板要点

  • Airflow 中许多对象可使用模板
  • 某些字段可用模板字符串,其他则不可
  • 可用内置文档检查:
  1. 打开 python3 解释器
  2. 导入必要库(如 from airflow.sdk import dag, task
  3. 在提示符运行 help(<Airflow object>),如 help(task)
  4. 查找包含"template_fields"的行,查看哪些参数可用模板
Python 中的 Apache Airflow 入门

模板文档示例

终端显示某个 Airflow 对象的 python3 帮助输出

终端输出中高亮显示 Airflow 帮助里的 template_fields 行

Python 中的 Apache Airflow 入门

使用 Airflow

创建、调度与监控工作流的示意图

Apache Airflow 徽标

Python 中的 Apache Airflow 入门

开始练习!

Python 中的 Apache Airflow 入门

Preparing Video For Download...