-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a ClickhouseResource in Dagster (#1796)
* Loads config from environment variables
- Loading branch information
Showing
4 changed files
with
85 additions
and
8 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
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# ruff: noqa: F403 | ||
|
||
from .bq_dts import * | ||
from .bq_dts import * | ||
from .clickhouse import * |
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,72 @@ | ||
import clickhouse_connect | ||
from contextlib import contextmanager | ||
from typing import Optional | ||
from dagster import ( | ||
ConfigurableResource, | ||
resource, | ||
) | ||
from pydantic import Field | ||
from ..constants import CLICKHOUSE_HOST, CLICKHOUSE_USER, CLICKHOUSE_PASSWORD | ||
from ..utils.common import ensure | ||
|
||
""" | ||
Note: This code is predominantly copied from the BigQueryResource | ||
It simply returns a Clickhouse Connect Client | ||
""" | ||
|
||
class ClickhouseResource(ConfigurableResource): | ||
"""Resource for interacting with Clickhouse. | ||
Examples: | ||
.. code-block:: python | ||
@asset | ||
def tables(clickhouse: ClickhouseResource): | ||
with clickhouse.get_client() as client: | ||
client.query(...) | ||
defs = Definitions( | ||
assets=[tables], | ||
resources={ | ||
"clickhouse": ClickhouseResource() | ||
} | ||
) | ||
""" | ||
|
||
host: Optional[str] = Field( | ||
default=None, | ||
description="Clickhouse host.", | ||
) | ||
|
||
user: Optional[str] = Field( | ||
default=None, | ||
description="Clickhouse username.", | ||
) | ||
|
||
password: Optional[str] = Field( | ||
default=None, | ||
description="Clickhouse password.", | ||
) | ||
|
||
@contextmanager | ||
def get_client(self): | ||
#Context manager to create a Clickhouse Client. | ||
host = ensure(self.host or CLICKHOUSE_HOST, "Missing CLICKHOUSE_HOST") | ||
username = ensure(self.user or CLICKHOUSE_USER, "Missing CLICKHOUSE_USER") | ||
password = ensure(self.password or CLICKHOUSE_PASSWORD, "Missing CLICKHOUSE_PASSWORD") | ||
client = clickhouse_connect.get_client( | ||
host=host, | ||
username=username, | ||
password=password, | ||
secure=True | ||
) | ||
yield client | ||
|
||
@resource( | ||
config_schema=ClickhouseResource.to_config_schema(), | ||
description="Dagster resource for connecting to Clickhouse.", | ||
) | ||
def clickhouse_resource(context): | ||
clickhouse_resource = ClickhouseResource.from_resource_context(context) | ||
with clickhouse_resource.get_client() as client: | ||
yield client |