SQLExecuteQueryOperator for ClickHouse

Use the SQLExecuteQueryOperator to execute SQL commands in a ClickHouse database.

Because ClickHouseHook extends DbApiHook, no dedicated ClickHouse operator is needed — the generic SQLExecuteQueryOperator handles DDL, DML, and analytical queries.

Using the Operator

Set the conn_id argument to the ID of a ClickHouse connection.

ClickHouse Airflow Connection Metadata

Parameter

Input

Host: string

ClickHouse server hostname

Port: integer

HTTP port (default: 8123, TLS: 8443)

Login: string

ClickHouse username (default: default)

Password: string

ClickHouse user password

Database (Schema): string

Default database (default: default)

Extra: JSON dict

secure, verify, connect_timeout, send_receive_timeout, compress, client_name

An example usage of the SQLExecuteQueryOperator to connect to ClickHouse:

tests/system/clickhouse/example_clickhouse.py[source]

create_table = SQLExecuteQueryOperator(
    task_id="create_table",
    sql=CREATE_TABLE_SQL,
)

insert_rows = SQLExecuteQueryOperator(
    task_id="insert_rows",
    sql=INSERT_ROWS_SQL,
)
read_rows = SQLExecuteQueryOperator(
    task_id="read_rows",
    sql=f"SELECT id, name FROM {CLICKHOUSE_TABLE} ORDER BY id",
)

drop_table = SQLExecuteQueryOperator(
    task_id="drop_table",
    sql=DROP_TABLE_SQL,
)

Querying Data

Use a handler to return query results:

tests/system/clickhouse/example_clickhouse.py[source]

read_rows = SQLExecuteQueryOperator(
    task_id="read_rows",
    sql=f"SELECT id, name FROM {CLICKHOUSE_TABLE} ORDER BY id",
)

Note

session_settings passed to ClickHouseHook directly (via hook_params) take precedence over any session_settings defined in the connection’s extra JSON field. Conflicting keys are resolved in favor of the constructor argument.

Was this entry helpful?