创建脚本
在设置的dag目录下(/root/airflow/dags),新建.py文件
使用官方文档的脚本
from datetime import timedelta,datetime
# The DAG object; we'll need this to instantiate a DAG
from airflow import DAG
# Operators; we need this to operate!
from airflow.operators.bash_operator import BashOperator
from airflow.utils.dates import days_ago
# These args will get passed on to each operator
# You can override them on a per-task basis during operator initialization
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'start_date': datetime(2020,9,13),#任务启动时间点,须小于实际执行的时间的下一个间隔
'email': ['airflow@example.com'],
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5),
# 'queue': 'bash_queue',
# 'pool': 'backfill',
# 'priority_weight': 10,
# 'end_date': datetime(2016, 1, 1),
# '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(
'tutorial',
default_args=default_args,
description='A simple tutorial DAG',
schedule_interval=timedelta(days=1),
)
# t1, t2 and t3 are examples of tasks created by instantiating operators
t1 = BashOperator(
task_id='print_date',
bash_command='date',
dag=dag,
)
t2 = BashOperator(
task_id='sleep',
depends_on_past=False,
bash_command='sleep 5',
retries=3,
dag=dag,
)
dag.doc_md = __doc__
t1.doc_md = """\
#### Task Documentation
You can document your task using the attributes `doc_md` (markdown),
`doc` (plain text), `doc_rst`, `doc_json`, `doc_yaml` which gets
rendered in the UI's Task Instance Details page.
![img](http://montcs.bloomu.edu/~bobmon/Semesters/2012-01/491/import%20soul.png)
"""
templated_command = """
{% for i in range(5) %}
echo "{{ ds }}"
echo "{{ macros.ds_add(ds, 7)}}"
echo "{{ params.my_param }}"
{% endfor %}
"""
t3 = BashOperator(
task_id='templated',
depends_on_past=False,
bash_command=templated_command,
params={'my_param': 'Parameter I passed in'},
dag=dag,
)
t1 >> [t2, t3]
保存后,在web管理界面,既能看到tutorial
点开dag可看到该组的具体任务
坑点
airflow执行任务的时间,是设置时间段的下一个间隔
比如:设置每天固定时间 0 6 * * *,每天6点执行,如果想在15号6点执行,起始时间必须小于15号.
如果设置14号,15号会开始执行,如果设置大于1的间隔,就会立即执行,有几个间隔就执行几次。
Last_Run显示的正是比预期少一个间隔。
如果设置每隔30分钟执行一次 schedule_interval */30 * * * * ,在Last_Run看到时间是比实际执行时间少半个小时。
参数
'depends_on_past':True #是否依赖上次任务执行的结果,如果设置True,则如果上次执行的任务失败,该工作流将不再执行。False,则该次流程不受上次执行结果的影响,正常执行
'ignore_first_depends_on_past':True #设置第一次执行不依赖上次执行结果
'retries':3 #设置失败后重试次数
'retry_delay': timedelta(minutes=1):#失败后1分钟后重试
'concurrency': 15, # 每个dag运行过程中最大可同时运行的task实例数
'max_active_runs': 1, # 同一时间可以运行的最多的dag runs 数量
设置邮箱通知
dag参数增加
'email': ['admin@admin.com'],
'email_on_failure': True,
'email_on_retry': False,
airflow.cfg修改
smtp_host = 127.0.0.1 #smtp服务器
smtp_starttls = False
smtp_ssl = False
#Example: smtp_user = airflow
smtp_user =admin #smtp服务器 用户
# Example: smtp_password = airflow
smtp_password =123232 #smtp服务器用户对应的密码
smtp_port = 25
smtp_mail_from = admin@163.com #发送邮件地址