Apache Airflow?是Airbnb開源的一款數(shù)據(jù)流程工具,目前是Apache孵化項(xiàng)目。以非常靈活的方式來支持?jǐn)?shù)據(jù)的ETL過程,同時(shí)還支持非常多的插件來完成諸如HDFS監(jiān)控、郵件通知等功能。Airflow支持單機(jī)和分布式兩種模式,支持Master-Slave模式,支持Mesos等資源調(diào)度,有非常好的擴(kuò)展性。
AWS Glue 是一項(xiàng)完全托管的提取、轉(zhuǎn)換和加載 (ETL) 服務(wù),讓客戶能夠輕松準(zhǔn)備和加載數(shù)據(jù)進(jìn)行分析。您只需在 AWS 管理控制臺(tái)中單擊幾次,即可創(chuàng)建并運(yùn)行 ETL 作業(yè)。您只需將 AWS Glue 指向存儲(chǔ)在 AWS 上的數(shù)據(jù),AWS Glue 便會(huì)發(fā)現(xiàn)您的數(shù)據(jù),并將關(guān)聯(lián)的元數(shù)據(jù)(例如表定義和架構(gòu))存儲(chǔ)到 AWS Glue 數(shù)據(jù)目錄中。存入目錄后,您的數(shù)據(jù)可立即供 ETL 搜索、查詢和使用。
AWS Glueb本身已經(jīng)自帶Workflow功能可以調(diào)度ETL任務(wù),但某些場(chǎng)景用戶希望延續(xù)現(xiàn)有Airflow架構(gòu)來調(diào)度AWS Glue,本文就嘗試實(shí)現(xiàn)了這個(gè)基本功能。
建立一個(gè)Glue任務(wù),完成基本的ETL工作:



安裝Airflow
export AIRFLOW_HOME=~/airflow
python3 -m pip install apache-airflow
airflow initdb
airflow webserver -p8080 &
airflow scheduler
根據(jù)下面鏈接要求,需要Python3.6以上版本。
https://github.com/apache/airflow/tree/master/airflow/providers/amazon#installation
python3 -m pip install apache-airflow-backport-providers-amazon
這也意味著Airflow需要使用Python3版本安裝,否則無法使用。
創(chuàng)建一個(gè)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

觸發(fā)Dag任務(wù)


從日志上可以看到,GlueOperator會(huì)檢查Glue任務(wù)是否存在,如果不存在,會(huì)創(chuàng)建一個(gè)任務(wù)如果存在,則使用存在的任務(wù)來運(yùn)行。
使用Airflow調(diào)度AWS Glue是可行的。需要Airflow 2.0版本,代碼由社區(qū)維護(hù),邏輯簡單,便于修改優(yōu)化。
Glue在較少運(yùn)算量的情況下經(jīng)濟(jì)性較好。
Glue可以減少EMR運(yùn)維的工作量。
參考鏈接:
##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