使用 SQLExecuteQueryOperator 連線到 Sqlite

使用 SQLExecuteQueryOperatorSqlite 資料庫中執行 Sqlite 命令。

注意

之前,SqliteOperator 用於執行此類操作。棄用後已移除。請改用 SQLExecuteQueryOperator

使用運算元

使用 conn_id 引數連線到您的 Sqlite 例項,連線元資料結構如下

Sqlite Airflow 連線元資料

引數

輸入

主機:字串

Sqlite 資料庫檔案

使用 SQLExecuteQueryOperator 連線到 Sqlite 的一個示例如下

tests/system/sqlite/example_sqlite.py


    # Example of creating a task that calls a common CREATE TABLE sql command.
    create_table_sqlite_task = SQLExecuteQueryOperator(
        task_id="create_table_sqlite",
        sql=r"""
        CREATE TABLE Customers (
            customer_id INT PRIMARY KEY,
            first_name TEXT,
            last_name TEXT
        );
        """,
    )

此外,您可以使用外部檔案執行 SQL 命令。指令碼資料夾必須與 DAG.py 檔案處於同一級別。

tests/system/sqlite/example_sqlite.py


    # Example of creating a task that calls an sql command from an external file.
    external_create_table_sqlite_task = SQLExecuteQueryOperator(
        task_id="create_table_sqlite_external_file",
        sql="create_table.sql",
    )

參考

欲瞭解更多資訊,請參閱

注意

透過 SQLExecuteQueryOperator() 提供的引數相對於透過 Airflow 連線元資料設定的引數(例如 schemaloginpassword 等)具有優先權。

此條目是否有幫助?