Skip to main content
Apache Airflow. Airflow enables orchestration and scheduling of data workflows with the database using DAGs (Directed Acyclic Graphs) and SQL operators. Use Airflow for data loading and transformation using the 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.
Verify the installation by checking the installed providers:
This should list 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

  1. Navigate to Admin > Connections
  2. Click the + button to add a new connection
  3. 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
All examples use the crash data tables described in the quickstart guide.

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
Stream Load provides better performance than INSERT INTO VALUES statements for large datasets and includes built-in features like error tolerance. Note that this does require the CSV file is accessible on the Airflow worker’s filesystem.

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
MySqlHook provides full programmatic access to query results, enabling sophisticated data transformations and analysis within your DAG.

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
This pattern uses staging tables and timestamp-based filtering to ensure only new records are loaded, making it ideal for scheduled batch updates.

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.
Note that the task name is unique in the database, so future runs may need a qualifier (such as uuid).

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
Materialized views in the database refresh automatically or on-demand, keeping aggregated data fresh while dramatically improving query performance.

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
Airflow’s built-in retry mechanisms handle most transient errors.

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