SQLExecuteQueryOperator and MySQLHook without any implementation or complex configuration.
Apache Airflow GitHub repo.
Supported features
- SQL Execution through MySQL protocol
- Connection management
- Transaction support
- Parameterized queries
- Task dependencies
- Retry logic
Installation
Prerequisites
- Apache Airflow 2.0+ or 3.0+
- Python 3.8+
- Access to a cluster (see the quickstart guide)
Install
The MySQL provider package is required to use the database as the database uses MySQL protocol.apache-airflow-providers-mysql in the output.
Configuration
Create a database Connection
Create a database connection in the Airflow UI or via environment variable. The name of the connection will be used by the DAGs later.Via Airflow UI
- Navigate to Admin > Connections
- Click the + button to add a new connection
- Configure the connection:
- Connection Id:
starrocks_default - Connection Type: MySQL
- Host:
your-starrocks-host.com - Schema:
your_database - Login:
your_username - Password:
your_password - Port:
9030
Via Airflow CLI
Usage Examples
These examples demonstrate common patterns for integrating the database with Airflow. Each example builds on core concepts while showcasing different approaches to data loading, transformation, and workflow orchestration. What You’ll Learn:- Data Loading: Efficiently load data from CSV files and cloud storage into the database
- Data Transformation: Execute SQL queries and process results with Python
- Advanced Patterns: Implement incremental loading, async operations, and query optimization
- Production Best Practices: Handle errors gracefully and build resilient pipelines
Data Loading
Stream Data Loading
Load large CSV files efficiently using Stream Load API. Stream Load is the recommended approach for:- High-throughput data loading (supports parallel loads)
- Loading data with column transformations and filtering
Insert From Files
Use the database’ FILES() table function to load data directly from files. This approach is ideal for:- Loading data from S3, HDFS, Google Cloud Storage
- One-step data ingestion with transformations applied during load
- Ad-hoc data loads from various file sources
FILES() supports multiple file formats and storage systems, making it a flexible alternative to Stream Load for certain use cases. The data is read and inserted in a single SQL statement.
Data Transformation
Execute SQL queries against the database for table creation and data insertion. This is useful for:- Setting up database schema
- Loading small datasets
- Running ad-hoc queries
More complex operations with MySqlHook
Use MySqlHook for advanced data analysis and processing within Python tasks. This approach is useful for:- Running analytical queries and processing results in Python
- Combining the database queries with Python libraries (pandas, numpy, etc.)
- Implementing complex business logic that requires both SQL and Python
- Creating data quality checks and validation workflows
Advanced Patterns
Incremental Data Loading
Load data incrementally to avoid reprocessing existing records. Incremental loading is essential for:- Efficiently updating tables with new data only
- Reducing processing time and resource usage
- Managing large datasets that grow over time
- Maintaining data freshness without full reloads
Asynchronous large-scale jobs with SUBMIT TASK
Use SUBMIT TASK for long-running queries that shouldn’t block the Airflow task. This pattern is beneficial for:
- Complex analytical queries that take minutes or hours
- Large-scale data transformations (table copies, aggregations)
- Resource-intensive operations that might timeout in synchronous mode
- Parallel execution of multiple heavy queries
- Separating job submission from completion monitoring
SUBMIT TASK allows Airflow to monitor long-running the database operations without holding database connections open, improving resource efficiency and reliability.
Materialized Views
Create and manage materialized views for accelerated query performance. Materialized views are ideal for:- Pre-computing complex aggregations for dashboards
- Accelerating frequently run analytical queries
- Maintaining summary tables that update automatically
- Reducing compute costs by avoiding repeated calculations
- Serving real-time analytics from pre-aggregated data
Error Handling
Implement robust error handling for production reliability. Proper error handling is critical for:- Automatically recovering from transient failures (network issues, timeouts)
- Preventing data pipeline disruptions from temporary problems
- Providing visibility into failure patterns
Troubleshooting
- Verify that port 9030 is accessible from within the Airflow instance
- Test the connection (if enabled) from the Airflow UI
- If using localhost, use 127.0.0.1 instead