-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add airflow operator and hook for ClickHouse
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
|
||
import clickhouse_connect | ||
from airflow.providers.common.sql.hooks.sql import DbApiHook | ||
import clickhouse_connect.dbapi as clickhouse_dbapi | ||
from clickhouse_connect.dbapi import connect # type: ignore | ||
from clickhouse_connect.dbapi.connection import Connection | ||
from clickhouse_connect.driver import httputil # type: ignore | ||
import clickhouse_connect.driver | ||
from clickhouse_connect.driver import client | ||
|
||
|
||
class SQLMeshClickHouseHook(DbApiHook): | ||
""" | ||
Uses the ClickHouse Python DB API connector. | ||
""" | ||
|
||
conn_name_attr = "sqlmesh_clickhouse_conn_id" | ||
default_conn_name = "sqlmesh_clickhouse_default" | ||
conn_type = "sqlmesh_clickhouse" | ||
hook_name = "SQLMesh ClickHouse" | ||
connector = clickhouse_connect | ||
|
||
def get_conn(self) -> Connection: | ||
"""Returns a Redshift connection object""" | ||
db = self.get_connection(getattr(self, t.cast(str, self.conn_name_attr))) | ||
|
||
return connect( | ||
host=db.host, | ||
port=db.port, | ||
user=db.login, | ||
password=db.password, | ||
database=db.schema, | ||
**db.extra_dejson, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
|
||
|
||
from sqlmesh.schedulers.airflow.hooks.clickhouse import SQLMeshClickHouseHook | ||
from sqlmesh.schedulers.airflow.operators.base import BaseDbApiOperator | ||
from sqlmesh.schedulers.airflow.operators.targets import BaseTarget | ||
|
||
|
||
class SQLMeshClickHouseOperator(BaseDbApiOperator): | ||
"""The operator that evaluates a SQLMesh model snapshot on a ClickHouse target | ||
Args: | ||
target: The target that will be executed by this operator instance. | ||
postgres_conn_id: The Airflow connection id for the postgres target. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
target: BaseTarget, | ||
clickhouse_conn_id: str = SQLMeshClickHouseHook.default_conn_name, | ||
**kwargs: t.Any, | ||
) -> None: | ||
super().__init__( | ||
target=target, | ||
conn_id=clickhouse_conn_id, | ||
dialect="clickhouse", | ||
hook_type=SQLMeshClickHouseHook, | ||
**kwargs, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters