52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
from datetime import datetime, timedelta
|
|
from airflow import DAG
|
|
from airflow.operators.python import PythonOperator
|
|
from airflow.operators.bash import BashOperator
|
|
|
|
# 默认参数定义
|
|
default_args = {
|
|
'owner': 'xiaozhang',
|
|
'depends_on_past': False,
|
|
'email_on_failure': False,
|
|
"on_failure_callback":xx,
|
|
'retries': 1,
|
|
'retry_delay': timedelta(minutes=5),
|
|
}
|
|
|
|
# dag = DAG(
|
|
# dag_id='hello_world',
|
|
# default_args=default_args,
|
|
# start_date=datetime(2023, 1, 1),
|
|
# schedule_interval='@daily',
|
|
# catchup=False,
|
|
# tags= ["demo"]
|
|
# )
|
|
|
|
# 定义任务
|
|
def print_hello():
|
|
print("Hello, World!")
|
|
|
|
# 实例化DAG
|
|
with DAG(
|
|
dag_id='hello_world',
|
|
default_args=default_args,
|
|
description="这个是一个样例",
|
|
start_date=datetime(2024, 10, 1),
|
|
schedule_interval='@daily',
|
|
catchup=True,
|
|
tags= ["demo","xiaozhang"]
|
|
) as dag:
|
|
|
|
task1 = PythonOperator(
|
|
task_id='print_hello',
|
|
python_callable=print_hello
|
|
)
|
|
|
|
task2 = BashOperator(
|
|
task_id='run_bash_command',
|
|
bash_command='echo "This is a Bash command"',
|
|
)
|
|
|
|
# 定义依赖关系
|
|
task1 >> task2 # task2将在task1完成后执行
|
|
# task1.set_downstream(task2) # 这种写法也可以,但是不太推荐,因为不够直观 |