使用Apache Airflow調(diào)度Glue任務(wù)

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è)基本功能。


準(zhǔn)備工作

建立一個(gè)Glue任務(wù),完成基本的ETL工作:




安裝Airflow

export AIRFLOW_HOME=~/airflow

python3 -m pip install apache-airflow

airflow initdb

airflow webserver -p8080 &

airflow scheduler


安裝GlueOperator

根據(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è)Dag腳本

創(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


設(shè)置AWSconnection

具體需求可以參考: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)行。

結(jié)論

使用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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容