Airflow官网+自测源代码举例
官方网站地址:
https://airflow.apache.org/docs/apache-airflow/2.7.2/,本文是基于
2.7.2
版本进行的说明。
1.准备
1.1 安装
上一篇的 Quick Start 有详细的安装过程,这里做最简安装启动:
# 1.设置安装目录
export AIRFLOW\_HOME=~/airflow
# 2.创建虚拟环境并安装
# 创建并切换到airflow虚拟环境
conda create -n airflow python=3.8
conda activate airflow
pip install "apache-airflow==2.7.2"
# 3.前台启动【在虚拟环境下】
airflow standalone
1.2 查询DAG目录
- dags_folder目录将Python文件放置到
[root@tcloud airflow]# cat airflow.cfg | grep dags\_folder
dags_folder = /root/airflow/dags
2.官方
- 安装任务所需的依赖【代码执行所需要的依赖】
conda install scikit-learn
- 官方举例文件
demo.py
放置到dags_folder
路径下
文件内容如下:
from datetime import datetime
from airflow import DAG
from airflow.decorators import task
from airflow.operators.bash import BashOperator
# A DAG represents a workflow, a collection of tasks
with DAG(dag_id="demo", start_date=datetime(2022, 1, 1), schedule="0 0 \* \* \*") as dag:
# Tasks are represented as operators
hello = BashOperator(task_id="hello", bash_command="echo hello")
@task()
def airflow():
print("airflow")
# Set dependencies between tasks
hello >> airflow()
- 刷新页面即可看到DAG,点击即可执行,效果如下:
3.自测
- 创建测试文件
airflow_test.py
并放置到dags_folder
目录下
文件内容如下:
from datetime import timedelta
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.utils.dates import days_ago
# DAG属性定义
default_args = {
'owner': 'airFlowTest',
'depends\_on\_past': False,
'start\_date': days_ago(31),
# 填入邮箱,方便失败、重试时发送邮件
'email': ['xxxxx@qq.com'],
# 失败时发邮件告警
'email\_on\_failure': True,
'email\_on\_retry': False,
# 重试次数
'retries': 1,
'retry\_delay': timedelta(minutes=2),
# 'queue': 'bash\_queue',
# 'pool': 'backfill',
# 'priority\_weight': 10,
# 'end\_date': datetime(2016, 1, 1),bu
# 'wait\_for\_downstream': False,
# 'dag': dag,
# 'sla': timedelta(hours=2),
# 'execution\_timeout': timedelta(seconds=300),
# 'on\_failure\_callback': some\_function,
# 'on\_success\_callback': some\_other\_function,
# 'on\_retry\_callback': another\_function,
# 'sla\_miss\_callback': yet\_another\_function,
'trigger\_rule': 'all\_success'
}
# 定义dag
dag = DAG(
'air\_test',
default\_args=default_args,
description='A simple airflow test',
schedule\_interval=timedelta(days=1),
)
step_one = BashOperator(
task\_id='step\_one',
**先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7**
**深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
**因此收集整理了一份《2024年最新网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**






**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上网络安全知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以点击这里获取](https://bbs.csdn.net/topics/618540462)**
升的进阶课程,涵盖了95%以上网络安全知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以点击这里获取](https://bbs.csdn.net/topics/618540462)**