Apache Airflow 是Airbnb开源的一款数据流程工具,目前是Apache孵化项目。以非常灵活的方式来支持数据的ETL过程,同时还支持非常多的插件来完成诸如HDFS监控、邮件通知等功能。Airflow支持单机和分布式两种模式,支持Master-Slave模式,支持Mesos等资源调度,有非常好的扩展性。
AWS Glue 是一项完全托管的提取、转换和加载 (ETL) 服务,让客户能够轻松准备和加载数据进行分析。您只需在 AWS 管理控制台中单击几次,即可创建并运行 ETL 作业。您只需将 AWS Glue 指向存储在 AWS 上的数据,AWS Glue 便会发现您的数据,并将关联的元数据(例如表定义和架构)存储到 AWS Glue 数据目录中。存入目录后,您的数据可立即供 ETL 搜索、查询和使用。
AWS Glueb本身已经自带Workflow功能可以调度ETL任务,但某些场景用户希望延续现有Airflow架构来调度AWS Glue,本文就尝试实现了这个基本功能。
建立一个Glue任务,完成基本的ETL工作:
安装Airflow
export AIRFLOW_HOME=~/airflow
python3 -m pip install apache-airflow
airflow initdb
airflow webserver -p8080 &
airflow scheduler
根据下面链接要求,需要Python3.6以上版本。
https://github.com/apache/airflow/tree/master/airflow/providers/amazon#installation
python3 -m pip install apache-airflow-backport-providers-amazon
这也意味着Airflow需要使用Python3版本安装,否则无法使用。
创建一个Dag脚本
创建一个glue-jobs.py:
from datetime import datetime
from airflow import DAG
fromairflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operatorimport PythonOperator
fromairflow.providers.amazon.aws.operators.glue import AwsGlueJobOperator
def print_hello():
return 'Hello hello!'
dag = DAG('glue_jobs',description='Simple glue DAG',
schedule_interval=None,
start_date=datetime(2019, 6, 28),catchup=False)
awsGlueOperator =AwsGlueJobOperator(task_id='glue-job',job_name='beta-glue-4', dag=dag)
hello_operator =PythonOperator(task_id='hello_task', python_callable=print_hello, dag=dag)
awsGlueOperator >>hello_operator
具体需求可以参考:https://airflow.apache.org/docs/stable/howto/connection/aws.html
触发Dag任务
从日志上可以看到,GlueOperator会检查Glue任务是否存在,如果不存在,会创建一个任务如果存在,则使用存在的任务来运行。
使用Airflow调度AWS Glue是可行的。需要Airflow 2.0版本,代码由社区维护,逻辑简单,便于修改优化。
Glue在较少运算量的情况下经济性较好。
Glue可以减少EMR运维的工作量。
参考链接:
##connections
https://airflow.apache.org/docs/stable/howto/connection/aws.html
## glue operators
https://airflow.readthedocs.io/en/latest/_api/airflow/providers/amazon/aws/operators/glue/index.html
https://github.com/apache/airflow/tree/master/airflow/providers/amazon
https://github.com/apache/airflow/blob/master/airflow/providers/amazon/aws/operators/glue.py