what is airflow task decorator
In Apache Airflow, the task decorator is a Python decorator used to define tasks within a Directed Acyclic Graph (DAG). Airflow is an open-source platform used to programmatically author, schedule, and monitor workflows. Each task within a DAG represents a unit of work that needs to be executed, such as running a script, executing a SQL query, or any other operation.
The task decorator in Airflow allows you to define a task using a Python function. When you decorate a function with @task
, it becomes an instance of the airflow.decorators.TaskDecorator
class. The decorator sets properties and attributes on the function to provide additional metadata to Airflow about the task.
Here’s a basic example of how you can use the @task
decorator:
from airflow import DAG
from airflow.decorators import task
from datetime import datetime
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'start_date': datetime(2023, 1, 1),
}
dag = DAG(
'my_dag',
default_args=default_args,
schedule_interval='@daily',
catchup=False
)
@task
def task_1():
# Do some work here
print("Executing task 1")
@task
def task_2():
# Do some work here
print("Executing task 2")
@task
def task_3():
# Do some work here
print("Executing task 3")
# Define the task dependencies
task_1() >> task_2() >> task_3()
In this example, we define three tasks using the @task
decorator: task_1
, task_2
, and task_3
. The >>
operator is used to set the dependencies between tasks. In this case, task_2
depends on the successful completion of task_1
, and task_3
depends on the successful completion of task_2
.
Each task, when decorated with @task
, becomes an instance of the airflow.decorators.TaskDecorator
class and is automatically added to the Airflow DAG. Airflow will use this metadata to create and manage the execution order of tasks based on their dependencies.
Keep in mind that this is just a basic example, and Airflow offers many more features and functionalities to create complex workflows with different types of tasks and scheduling options.