Using SqlOperator in Airflow to Run Database Queries
📖 Scenario: You work as a data engineer and need to automate running SQL queries on a database using Airflow. Airflow helps schedule and run tasks automatically.In this project, you will create a simple Airflow DAG that uses SqlOperator to run a SQL query on a database.
🎯 Goal: Build an Airflow DAG that runs a SQL query using SqlOperator. You will define the DAG, set the SQL query, and print the query execution result.
📋 What You'll Learn
Create an Airflow DAG named example_sql_dag
Use SqlOperator to run a SQL query
Define a SQL query string variable sql_query with the exact query SELECT * FROM users WHERE active = 1;
Set the DAG schedule interval to @daily
Print a message after the SQL task runs
💡 Why This Matters
🌍 Real World
Automating database queries is common in data pipelines. Airflow helps schedule and run these queries regularly without manual work.
💼 Career
Data engineers and DevOps professionals use Airflow and SqlOperator to build reliable data workflows and automate database tasks.
Progress0 / 4 steps
1
Create the Airflow DAG and SQL query variable
Create an Airflow DAG named example_sql_dag with dag_id='example_sql_dag' and schedule_interval='@daily'. Also create a variable called sql_query and set it to the string "SELECT * FROM users WHERE active = 1;".
Apache Airflow
Hint
Use DAG from airflow and set dag_id and schedule_interval. Define sql_query as the exact SQL string.
2
Import SqlOperator and create the SQL task
Import SqlOperator from airflow.providers.common.sql.operators.sql. Then create a task called run_sql using SqlOperator inside the example_sql_dag. Set task_id='run_sql', sql=sql_query, and dag=example_sql_dag.
Apache Airflow
Hint
Import SqlOperator and create a task with the exact task_id, sql, and dag parameters.
3
Set task dependencies and add a print statement
Add a Python function called print_done that prints "SQL query task completed.". Then create a PythonOperator task called print_task with task_id='print_done' and python_callable=print_done inside example_sql_dag. Finally, set the task order so run_sql runs before print_task.
Apache Airflow
Hint
Define a function that prints the message. Use PythonOperator to create a task with that function. Use >> to set task order.
4
Print the DAG tasks to verify setup
Add a print statement that prints the list of task IDs in example_sql_dag using list(example_sql_dag.task_ids).
Apache Airflow
Hint
Use print(list(example_sql_dag.task_ids)) to show the task IDs in the DAG.
Practice
(1/5)
1. What is the primary purpose of the SqlOperator in Airflow?
easy
A. To run SQL commands as part of an Airflow task
B. To schedule Python scripts
C. To monitor server health
D. To send email notifications
Solution
Step 1: Understand the role of SqlOperator
The SqlOperator is designed to execute SQL commands within Airflow tasks, automating database operations.
Step 2: Compare with other options
Scheduling Python scripts, monitoring servers, and sending emails are handled by other operators or components, not SqlOperator.
Final Answer:
To run SQL commands as part of an Airflow task -> Option A