0
0
Apache Airflowdevops~30 mins

Creating a basic DAG file in Apache Airflow - Try It Yourself

Choose your learning style9 modes available
Creating a basic DAG file
📖 Scenario: You are working as a data engineer. You need to create a simple workflow to run tasks in Apache Airflow. This workflow is called a DAG (Directed Acyclic Graph). It helps you schedule and organize tasks.
🎯 Goal: Build a basic Airflow DAG file that defines a simple workflow with one task. This task will print a message when it runs.
📋 What You'll Learn
Create a DAG object with a specific dag_id and start_date.
Define a Python function that prints a message.
Create a PythonOperator to run the function.
Set the task in the DAG.
Print the DAG object at the end.
💡 Why This Matters
🌍 Real World
Airflow DAGs are used to automate and schedule workflows in data pipelines and other automated tasks.
💼 Career
Knowing how to create and manage DAGs is essential for data engineers and DevOps professionals working with workflow automation.
Progress0 / 4 steps
1
Create the DAG object
Import DAG from airflow and datetime from datetime. Then create a DAG object called my_dag with dag_id='my_first_dag' and start_date=datetime(2023, 1, 1).
Apache Airflow
Need a hint?

Use DAG to create the workflow object. The start_date tells Airflow when to begin scheduling.

2
Define the Python function
Define a Python function called print_hello that prints the message 'Hello from Airflow!'.
Apache Airflow
Need a hint?

Define a simple function that prints a message. This function will be run by the task.

3
Create the PythonOperator task
Import PythonOperator from airflow.operators.python. Then create a task called hello_task using PythonOperator with task_id='hello_task', python_callable=print_hello, and assign it to the DAG my_dag.
Apache Airflow
Need a hint?

Use PythonOperator to run the function as a task in the DAG.

4
Print the DAG object
Write a print statement to display the my_dag object.
Apache Airflow
Need a hint?

Use print(my_dag) to show the DAG object details.