Apache Airflow ODBC Provider

4.12.1 · active · verified Sat Apr 11

This provider package enables Apache Airflow to connect to various ODBC data sources, including MS SQL Server, to execute queries and perform database operations. It is released independently from Airflow core and follows semantic versioning, with major version upgrades indicating breaking changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Airflow DAG using the `SQLExecuteQueryOperator` to interact with an ODBC database. It assumes an Airflow ODBC connection (`my_odbc_conn`) is configured in the UI or via environment variables, including the necessary ODBC driver. Remember to install system-level ODBC drivers for your specific database.

from __future__ import annotations

import pendulum

from airflow.models.dag import DAG
from airflow.providers.common.sql.operators.sql import SQLExecuteQueryOperator

# Ensure you have an Airflow Connection named 'my_odbc_conn'
# type: ODBC
# host: your_odbc_server
# login: your_username
# password: your_password
# extra: {"driver": "{ODBC Driver 18 for SQL Server}", "autocommit": true}

with DAG(
    dag_id="odbc_example_dag",
    start_date=pendulum.datetime(2023, 1, 1, tz="UTC"),
    schedule=None,
    catchup=False,
    tags=["odbc", "example"],
) as dag:
    create_table = SQLExecuteQueryOperator(
        task_id="create_table",
        sql="""
            CREATE TABLE IF NOT EXISTS my_odbc_table (
                id INT IDENTITY(1,1) PRIMARY KEY,
                value VARCHAR(255)
            );
        """,
        conn_id="my_odbc_conn",
    )

    insert_data = SQLExecuteQueryOperator(
        task_id="insert_data",
        sql="""
            INSERT INTO my_odbc_table (value) VALUES ('test_value_{{ ds }}');
        """,
        conn_id="my_odbc_conn",
    )

    # Note: For fetching data, you'd typically use OdbcHook in a PythonOperator
    # or a custom operator, as SQLExecuteQueryOperator is for execution.

    create_table >> insert_data

view raw JSON →