Airflow提供了一些通用的DAG和任务,如PythonOperator、BashOperator和EmailOperator等。使用这些操作符可以编写可以重复使用的任务并构建可扩展的DAG。
以PythonOperator为例,可以在DAG中使用PythonOperator来定义任务。在这个任务中,你可以编写Python函数来执行任意的Python代码。下面是一个示例:
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
def my_python_function():
# Your Python code here
return
default_args = {
'owner': 'airflow',
'start_date': datetime(2019, 1, 1),
'retries': 1,
'retry_delay': timedelta(minutes=5),
}
with DAG('my_dag', default_args=default_args, schedule_interval=timedelta(days=1)) as dag:
my_task = PythonOperator(
task_id='my_task',
python_callable=my_python_function
)
在上面的代码中,我们定义了一个名为'my_task”的任务,并使用PythonOperator操作符来运行我们定义的Python函数。该DAG每天将运行一次。
通过编写类似上面的代码,你可以自定义和定义Airflow的DAG/Task。