工作流管理平台Airflow
Airflow
1. 引言
Airflow是Airbnb开源的一个用Python写就的工作流管理平台(workflow management platform)。在前一篇文章中,介绍了如何用Crontab管理数据流,但是缺点也是显而易见。针对于Crontab的缺点,灵活可扩展的Airflow具有以下特点:
- 工作流依赖关系的可视化;
- 日志追踪;
- (Python脚本)易于扩展
对比Java系的Oozie,Airflow奉行“Configuration as code”哲学,对于描述工作流、判断触发条件等全部采用Python,使得你编写工作流就像在写脚本一样;能debug工作流(test backfill命令),更好地判别是否有错误;能更快捷地在线上做功能扩展。Airflow充分利用Python的灵巧轻便,相比之下Oozie则显得笨重厚拙太多(其实我没在黑Java~~)。《What makes Airflow great?》介绍了更多关于Airflow的优良特性;其他有关于安装、介绍的文档在这里、还有这里。
下表给出Airflow(基于1.7版本)与Oozie(基于4.0版本)对比情况:
功能 | Airflow | Oozie |
---|---|---|
工作流描述 | Python | xml |
数据触发 | Sensor | datasets, input-events |
工作流节点 | operator | action |
完整工作流 | DAG | workflow |
定期调度 | DAG schedule_interval | coordinator frequency |
任务依赖 | >> , << |
<ok to> |
内置函数、变量 | template macros | EL function, EL constants |
之前我曾提及Oozie没有能力表达复杂的DAG,是因为Oozie只能指定下流依赖(downstream)而不能指定上流依赖(upstream)。与之相比,Airflow就能表示复杂的DAG。Airflow没有像Oozie一样区分workflow与coordinator,而是把触发条件、工作流节点都看作一个operator,operator组成一个DAG。
2. 实战
下面将给出如何用Airflow完成data pipeline任务。
首先简要地介绍下背景:定时(每周)检查Hive表的partition的任务是否有生成,若有则触发Hive任务写Elasticsearch;然后等Hive任务完后,执行Python脚本查询Elasticsearch发送报表。但是,Airflow对Python3支持有问题(依赖包为Python2编写);因此不得不自己写HivePartitionSensor
:
# -*- coding: utf-8 -*-
# @Time : 2016/11/29
# @Author : rain
from airflow.operators import BaseSensorOperator
from airflow.utils.decorators import apply_defaults
from impala.dbapi import connect
import logging
class HivePartitionSensor(BaseSensorOperator):
"""
Waits for a partition to show up in Hive.
:param host, port: the host and port of hiveserver2
:param table: The name of the table to wait for, supports the dot notation (my_database.my_table)
:type table: string
:param partition: The partition clause to wait for. This is passed as
is to the metastore Thrift client,and apparently supports SQL like
notation as in ``ds='2016-12-01'``.
:type partition: string
"""
template_fields = ('table', 'partition',)
ui_color = '#2b2d42'
@apply_defaults
def __init__(
self,
conn_host, conn_port,
table, partition="ds='{{ ds }}'",
poke_interval=60 * 3,
*args, **kwargs):
super(HivePartitionSensor, self).__init__(
poke_interval=poke_interval, *args, **kwargs)
if not partition:
partition = "ds='{{ ds }}'"
self.table = table
self.partition = partition
self.conn_host = conn_host
self.conn_port = conn_port
self.conn = connect(host=self.conn_host, port=self.conn_port, auth_mechanism='PLAIN')
def poke(self, context):
logging.info(
'Poking for table {self.table}, '
'partition {self.partition}'.format(**locals()))
cursor = self.conn.cursor()
cursor.execute("show partitions {}".format(self.table))
partitions = cursor.fetchall()
partitions = [i[0] for i in partitions]
if self.partition in partitions:
return True
else:
return False
Python3连接Hive server2的采用的是impyla模块,HivePartitionSensor
用于判断Hive表的partition是否存在。写自定义的operator,有点像写Hive、Pig的UDF;写好的operator需要放在目录~/airflow/dags
,以便于DAG调用。那么,完整的工作流DAG如下:
# tag cover analysis, based on Airflow v1.7.1.3
from airflow.operators import BashOperator
from operatorUD.HivePartitionSensor import HivePartitionSensor
from airflow.models import DAG
from datetime import datetime, timedelta
from impala.dbapi import connect
conn = connect(host='192.168.72.18', port=10000, auth_mechanism='PLAIN')
def latest_hive_partition(table):
cursor = conn.cursor()
cursor.execute("show partitions {}".format(table))
partitions = cursor.fetchall()
partitions = [i[0] for i in partitions]
return partitions[-1].split("=")[1]
log_partition_value = """{{ macros.ds_add(ds, -2)}}"""
tag_partition_value = latest_hive_partition('tag.dmp')
args = {
'owner': 'jyzheng',
'depends_on_past': False,
'start_date': datetime.strptime('2016-12-06', '%Y-%m-%d')
}
# execute every Tuesday
dag = DAG(
dag_id='tag_cover', default_args=args,
schedule_interval='@weekly',
dagrun_timeout=timedelta(minutes=10))
ad_sensor = HivePartitionSensor(
task_id='ad_sensor',
conn_host='192.168.72.18',
conn_port=10000,
table='ad.ad_log',
partition="day_time={}".format(log_partition_value),
dag=dag
)
ad_hive_task = BashOperator(
task_id='ad_hive_task',
bash_command='hive -f /path/to/cron/cover/ad_tag.hql --hivevar LOG_PARTITION={} '
'--hivevar TAG_PARTITION={}'.format(log_partition_value, tag_partition_value),
dag=dag
)
ad2_hive_task = BashOperator(
task_id='ad2_hive_task',
bash_command='hive -f /path/to/cron/cover/ad2_tag.hql --hivevar LOG_PARTITION={} '
'--hivevar TAG_PARTITION={}'.format(log_partition_value, tag_partition_value),
dag=dag
)
report_task = BashOperator(
task_id='report_task',
bash_command='sleep 5m; python3 /path/to/cron/report/tag_cover.py {}'.format(log_partition_value),
dag=dag
)
ad_sensor >> ad_hive_task >> report_task
ad_sensor >> ad2_hive_task >> report_task
工作流管理平台Airflow的更多相关文章
- 灵活可扩展的工作流管理平台Airflow
1. 引言 Airflow是Airbnb开源的一个用Python写就的工作流管理平台(workflow management platform).在前一篇文章中,介绍了如何用Crontab管理数据流, ...
- 【从零开始学BPM,Day1】工作流管理平台架构学习
[课程主题] 主题:5天,一起从零开始学习BPM [课程形式] 1.为期5天的短任务学习 2.每天观看一个视频,视频学习时间自由安排. [第一天课程] Step 1 软件下载:H3 BPM10.0全开 ...
- 开源 C#工作流管理平台
{ font-family: 宋体; panose-1: 2 1 6 0 3 1 1 1 1 1 } @font-face { font-family: "Cambria Math" ...
- K2 BPM + SAP,实现全方面管理企业
K2作为专业的BPM.工作流管理平台供应商,面向庞大的SAP用户群体,除了提供产品化的SAP集成工具「K2 connect」产品之外,更拥有一套得到众多客户验证的集成解决方案. 此方案可供SAP用户或 ...
- 云时代的IT运维面临将会有哪些变化
导读 每一次IT系统的转型,运维系统和业务保障都是最艰难的部分.在当前企业IT系统向云架构转型的时刻,运维系统再一次面临着新的挑战.所以在数据中心运维的时候,运维人员应该注意哪些问题? 在云计算时代, ...
- k2系列-服务器管理篇
k2服务器即K2 WORKSPACE管理介绍: k2 管理平台统一管理基于K2开发的所有流程的跟踪调试以及基本配置信息. 具体完成的操作有以下几个部分: 1 配置K2环境相关属性.包括全局变量等 2 ...
- 数据治理方案技术调研 Atlas VS Datahub VS Amundsen
数据治理意义重大,传统的数据治理采用文档的形式进行管理,已经无法满足大数据下的数据治理需要.而适合于Hadoop大数据生态体系的数据治理就非常的重要了. 大数据下的数据治理作为很多企业的一个巨大的 ...
- 【airflow实战系列】 基于 python 的调度和监控工作流的平台
简介 airflow 是一个使用python语言编写的data pipeline调度和监控工作流的平台.Airflow被Airbnb内部用来创建.监控和调整数据管道.任何工作流都可以在这个使用Pyth ...
- 从 Airflow 到 Apache DolphinScheduler,有赞大数据开发平台的调度系统演进
点击上方 蓝字关注我们 作者 | 宋哲琦 ✎ 编 者 按 在不久前的 Apache DolphinScheduler Meetup 2021 上,有赞大数据开发平台负责人 宋哲琦 带来了平台调度系统 ...
随机推荐
- [TypeScript] Sharing Class Behavior with Inheritance in TypeScript
Typescript classes make inheritance much easier to write and understand. In this lesson we look into ...
- 一位90后程序员的自述:如何从年薪3w到30w!
初入职场之时,大多数人都应该考虑过这样的一个问题,如何找到一种实用,简化web流程的方法,在工作之中能有所提升和突破. 学好哪些?基础必须精通! 九层之塔,起于垒土;千里之行,始于足下.入门之前,这些 ...
- CImage将图片转为指定像素大小
CFileDialog fDlg(true, "jpg", "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, &q ...
- 【t048】水流
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 全球气候变暖,小镇A面临水灾.于是你必须买一些泵把水抽走.泵的抽水能力可以认为是无穷大,但你必须把泵放 ...
- [AngularJS NG-redux] Handle Asynchronous Operations with Middleware
Invariably the question that comes up when talking about Redux is how does one handle asynchronous o ...
- [Node.js] Testing ES6 Promises in Node.js using Mocha and Chai
Writing great ES6 style Promises for Node.js is only half the battle. Your great modules must includ ...
- angular的Dom操作
原文 https://www.jianshu.com/p/9d7249922bda 大纲 1.angular的DOM操作 2.为什么不能直接操作DOM? 3.三种错误操作DOM的方式 4.angula ...
- 【42.38%】【BZOJ 3196】二逼平衡树
Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1363 Solved: 579 [Submit][Status][Discuss] Descripti ...
- [Angular Router] Lazy loading Module with Auxiliary router
Found the way to handle Auxiliary router for lazy loading moudle and erge load module are different. ...
- 【60.97%】【BZOJ 1925】 [Sdoi2010]地精部落
Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1194 Solved: 728 [Submit][Status][Discuss] Descript ...