Apache Airflow Tutorial : Create Your First Dag Step By Step - 2026

Опубликовано: 25 Июль 2026
на канале: Tech With Machines
472
4

#apacheairflow #airflow #airflowdag #airflowtutorial #firstdag #learnairflow


Let's create your first Apache Airflow DAG! Below is a step-by-step guide and a simple example to help you get started.

Step 1: Prerequisites
Install Apache Airflow (if not already installed):
bash
Copy code
pip install apache-airflow
Initialize the Airflow Environment:
bash
Copy code
airflow db init
airflow users create \
--username admin \
--firstname Airflow \
--lastname Admin \
--role Admin \
--email [email protected]
Start the Airflow Web Server and Scheduler:
bash
Copy code
airflow webserver --port 8080
airflow scheduler
Access the web interface at http://localhost:8080.
Step 2: Create Your First DAG
A DAG (Directed Acyclic Graph) defines a workflow. Tasks are defined in Python and orchestrated by Airflow.

Example: My First DAG
python
Copy code
from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime

Define the DAG
default_args = {
'owner': 'airflow',
'retries': 1,
'retry_delay': timedelta(minutes=5)
}

with DAG(
dag_id='my_first_dag',
default_args=default_args,
description='My first Airflow DAG!',
schedule_interval='@daily',
start_date=datetime(2023, 1, 1),
catchup=False,
) as dag:

Task 1: Print the current date
print_date = BashOperator(
task_id='print_date',
bash_command='date'
)

Task 2: Say Hello
say_hello = BashOperator(
task_id='say_hello',
bash_command='echo "Hello, Airflow!"'
)

Task Dependencies
print_date say_hello
Step 3: Add the DAG to Airflow
Save the file as my_first_dag.py in the Airflow dags/ directory:
bash
Copy code
cp my_first_dag.py $AIRFLOW_HOME/dags/
Airflow will automatically detect the DAG and display it in the web interface under "DAGs."
Step 4: Run the DAG
In the Airflow UI, activate the DAG by toggling the "ON" switch.
Trigger the DAG manually using the play button or wait for the scheduled interval.
Explanation of the DAG
DAG Definition:

dag_id: Unique identifier for the DAG.
start_date: The date the DAG starts running.
schedule_interval: Determines how often the DAG runs (@daily, @hourly, */5 * * * *, etc.).
catchup: Ensures Airflow doesn’t backfill runs for past dates if set to False.
Tasks:

BashOperator: Executes a bash command.
print_date: Prints the current date.
say_hello: Prints "Hello, Airflow!" to the logs.
Task Dependencies:

print_date say_hello: Ensures that say_hello runs after print_date.
Would you like help with more advanced DAG features like using PythonOperators, XComs, or dynamic task generation?