sm_data_ml_utils 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sm_data_ml_utils-0.1.0/LICENSE.txt +21 -0
- sm_data_ml_utils-0.1.0/PKG-INFO +116 -0
- sm_data_ml_utils-0.1.0/README.md +54 -0
- sm_data_ml_utils-0.1.0/pyproject.toml +60 -0
- sm_data_ml_utils-0.1.0/sm_data_ml_utils/core/__init__.py +0 -0
- sm_data_ml_utils-0.1.0/sm_data_ml_utils/core/config.py +25 -0
- sm_data_ml_utils-0.1.0/sm_data_ml_utils/core/databricks_utils.py +134 -0
- sm_data_ml_utils-0.1.0/sm_data_ml_utils/databricks_client/__init__.py +0 -0
- sm_data_ml_utils-0.1.0/sm_data_ml_utils/databricks_client/client.py +55 -0
- sm_data_ml_utils-0.1.0/sm_data_ml_utils/databricks_client/databricks-sql.ipynb +1043 -0
- sm_data_ml_utils-0.1.0/sm_data_ml_utils/mlflow_databricks/__init__.py +0 -0
- sm_data_ml_utils-0.1.0/sm_data_ml_utils/mlflow_databricks/mlflow_model_utils.py +398 -0
- sm_data_ml_utils-0.1.0/sm_data_ml_utils/mlflow_databricks/mlflow_prediction_requests.py +103 -0
- sm_data_ml_utils-0.1.0/sm_data_ml_utils/mlflow_databricks/mlflow_serve.py +253 -0
- sm_data_ml_utils-0.1.0/sm_data_ml_utils/mlflow_databricks/mlflow_tracker.py +173 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-2023
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: sm_data_ml_utils
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Common Python tools and utilities for ML work
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: Shuming Peh
|
|
7
|
+
Author-email: shuming.peh@gmail.com
|
|
8
|
+
Requires-Python: >=3.10,<3.13
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Requires-Dist: PyYAML (==6.0)
|
|
15
|
+
Requires-Dist: Werkzeug (>=3.0.3,<4.0.0)
|
|
16
|
+
Requires-Dist: aiobotocore (>=2.8.0,<3.0.0)
|
|
17
|
+
Requires-Dist: appdirs (==1.4.4)
|
|
18
|
+
Requires-Dist: attrs (>=22.2.0,<23.0.0)
|
|
19
|
+
Requires-Dist: black (>=22.6.0,<23.0.0)
|
|
20
|
+
Requires-Dist: boto3 (>=1.33.5,<2.0.0)
|
|
21
|
+
Requires-Dist: botocore (>=1.34.15,<2.0.0)
|
|
22
|
+
Requires-Dist: certifi (>=2023.7.22,<2024.0.0)
|
|
23
|
+
Requires-Dist: cfgv (==3.2.0)
|
|
24
|
+
Requires-Dist: coverage (==5.4)
|
|
25
|
+
Requires-Dist: databricks-sql-connector (>=3.3.0,<4.0.0)
|
|
26
|
+
Requires-Dist: distlib (>=0.3.8,<0.4.0)
|
|
27
|
+
Requires-Dist: filelock (>=3.14.0,<4.0.0)
|
|
28
|
+
Requires-Dist: flake8 (>=4.0.1,<5.0.0)
|
|
29
|
+
Requires-Dist: identify (==1.5.13)
|
|
30
|
+
Requires-Dist: iniconfig (==1.1.1)
|
|
31
|
+
Requires-Dist: isort (>=5.10.1,<6.0.0)
|
|
32
|
+
Requires-Dist: joblib (==1.3.2)
|
|
33
|
+
Requires-Dist: mccabe (==0.6.1)
|
|
34
|
+
Requires-Dist: mlflow (==2.22.2)
|
|
35
|
+
Requires-Dist: mock (>=4.0.3,<5.0.0)
|
|
36
|
+
Requires-Dist: moto (>=4.2.7,<5.0.0)
|
|
37
|
+
Requires-Dist: mypy-extensions (==0.4.3)
|
|
38
|
+
Requires-Dist: nodeenv (>=1.5.0,<2.0.0)
|
|
39
|
+
Requires-Dist: numpy (==1.23.5)
|
|
40
|
+
Requires-Dist: packaging (>=23.1,<24.0)
|
|
41
|
+
Requires-Dist: pandas (==1.4.4)
|
|
42
|
+
Requires-Dist: pluggy (==1.5.0)
|
|
43
|
+
Requires-Dist: polling (==0.3.2)
|
|
44
|
+
Requires-Dist: py (>=1.11.0,<2.0.0)
|
|
45
|
+
Requires-Dist: pyarrow (>=14.0.0,<15.0.0)
|
|
46
|
+
Requires-Dist: pydantic (==1.10.24)
|
|
47
|
+
Requires-Dist: pyparsing (==2.4.7)
|
|
48
|
+
Requires-Dist: pytest (>=8.3.3,<9.0.0)
|
|
49
|
+
Requires-Dist: pytest-cov (>=3.0.0,<4.0.0)
|
|
50
|
+
Requires-Dist: pytest-custom-exit-code (==0.3.0)
|
|
51
|
+
Requires-Dist: regex (>=2024.9.11,<2025.0.0)
|
|
52
|
+
Requires-Dist: requests (>=2.32.0,<3.0.0)
|
|
53
|
+
Requires-Dist: responses (==0.23.1)
|
|
54
|
+
Requires-Dist: s3fs (>=2023.10.0,<2024.0.0)
|
|
55
|
+
Requires-Dist: six (==1.15.0)
|
|
56
|
+
Requires-Dist: toml (==0.10.2)
|
|
57
|
+
Requires-Dist: torch (==2.3.1)
|
|
58
|
+
Requires-Dist: typed-ast (>=1.5.3,<2.0.0)
|
|
59
|
+
Requires-Dist: typing-extensions (>=4.4.0,<5.0.0)
|
|
60
|
+
Description-Content-Type: text/markdown
|
|
61
|
+
|
|
62
|
+
# data-ml-utils
|
|
63
|
+
A utility python package that covers the common libraries we use
|
|
64
|
+
|
|
65
|
+
## Installation
|
|
66
|
+
This is an open source library hosted on pypi. Run the following command to install the library.
|
|
67
|
+
```
|
|
68
|
+
pip install data-ml-utils --upgrade
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Documentation
|
|
72
|
+
Head over to https://data-ml-utils.readthedocs.io/en/latest/index.html# to read our library documentation
|
|
73
|
+
|
|
74
|
+
## Feature
|
|
75
|
+
### Pyathena client initialisation
|
|
76
|
+
Almost one liner
|
|
77
|
+
```python
|
|
78
|
+
import os
|
|
79
|
+
from data_ml_utils.pyathena_client.client import PyAthenaClient
|
|
80
|
+
|
|
81
|
+
os.environ["AWS_ACCESS_KEY_ID"] = "xxx"
|
|
82
|
+
os.environ["AWS_SECRET_ACCESS_KEY"] = "xxx" # pragma: allowlist secret
|
|
83
|
+
os.environ["S3_BUCKET"] = "xxx"
|
|
84
|
+
|
|
85
|
+
pyathena_client = PyAthenaClient()
|
|
86
|
+
```
|
|
87
|
+

|
|
88
|
+
|
|
89
|
+
### Pyathena query
|
|
90
|
+
Almost one liner
|
|
91
|
+
```python
|
|
92
|
+
query = """
|
|
93
|
+
SELECT
|
|
94
|
+
*
|
|
95
|
+
FROM
|
|
96
|
+
dev.example_pyathena_client_table
|
|
97
|
+
LIMIT 10
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
df_raw = pyathena_client.query_as_pandas(final_query=query)
|
|
101
|
+
```
|
|
102
|
+

|
|
103
|
+
|
|
104
|
+
### MLflow utils
|
|
105
|
+
Visit [link](https://data-ml-utils.readthedocs.io/en/latest/index.html#mlflow-utils)
|
|
106
|
+
|
|
107
|
+
### More to Come
|
|
108
|
+
* You suggest, raise a feature request issue and we will review!
|
|
109
|
+
|
|
110
|
+
## Tutorials
|
|
111
|
+
### Pyathena
|
|
112
|
+
There is a jupyter notebook to show how to use the package utility package for `pyathena`: [notebook](tutorials/[TUTO]%20pyathena.ipynb)
|
|
113
|
+
|
|
114
|
+
### MLflow utils
|
|
115
|
+
There is a jupyter notebook to show how to use the package utility package for `mlflow_databricks`: [notebook](tutorials/[TUTO]%20mlflow_databricks.ipynb)
|
|
116
|
+
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# data-ml-utils
|
|
2
|
+
A utility python package that covers the common libraries we use
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
This is an open source library hosted on pypi. Run the following command to install the library.
|
|
6
|
+
```
|
|
7
|
+
pip install data-ml-utils --upgrade
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Documentation
|
|
11
|
+
Head over to https://data-ml-utils.readthedocs.io/en/latest/index.html# to read our library documentation
|
|
12
|
+
|
|
13
|
+
## Feature
|
|
14
|
+
### Pyathena client initialisation
|
|
15
|
+
Almost one liner
|
|
16
|
+
```python
|
|
17
|
+
import os
|
|
18
|
+
from data_ml_utils.pyathena_client.client import PyAthenaClient
|
|
19
|
+
|
|
20
|
+
os.environ["AWS_ACCESS_KEY_ID"] = "xxx"
|
|
21
|
+
os.environ["AWS_SECRET_ACCESS_KEY"] = "xxx" # pragma: allowlist secret
|
|
22
|
+
os.environ["S3_BUCKET"] = "xxx"
|
|
23
|
+
|
|
24
|
+
pyathena_client = PyAthenaClient()
|
|
25
|
+
```
|
|
26
|
+

|
|
27
|
+
|
|
28
|
+
### Pyathena query
|
|
29
|
+
Almost one liner
|
|
30
|
+
```python
|
|
31
|
+
query = """
|
|
32
|
+
SELECT
|
|
33
|
+
*
|
|
34
|
+
FROM
|
|
35
|
+
dev.example_pyathena_client_table
|
|
36
|
+
LIMIT 10
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
df_raw = pyathena_client.query_as_pandas(final_query=query)
|
|
40
|
+
```
|
|
41
|
+

|
|
42
|
+
|
|
43
|
+
### MLflow utils
|
|
44
|
+
Visit [link](https://data-ml-utils.readthedocs.io/en/latest/index.html#mlflow-utils)
|
|
45
|
+
|
|
46
|
+
### More to Come
|
|
47
|
+
* You suggest, raise a feature request issue and we will review!
|
|
48
|
+
|
|
49
|
+
## Tutorials
|
|
50
|
+
### Pyathena
|
|
51
|
+
There is a jupyter notebook to show how to use the package utility package for `pyathena`: [notebook](tutorials/[TUTO]%20pyathena.ipynb)
|
|
52
|
+
|
|
53
|
+
### MLflow utils
|
|
54
|
+
There is a jupyter notebook to show how to use the package utility package for `mlflow_databricks`: [notebook](tutorials/[TUTO]%20mlflow_databricks.ipynb)
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "sm_data_ml_utils"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Common Python tools and utilities for ML work"
|
|
5
|
+
authors = ["Shuming Peh <shuming.peh@gmail.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[tool.poetry.dependencies]
|
|
10
|
+
python = ">=3.10,<3.13"
|
|
11
|
+
aiobotocore = "^2.8.0"
|
|
12
|
+
appdirs = "1.4.4"
|
|
13
|
+
attrs = "^22.2.0"
|
|
14
|
+
black = "^22.6.0"
|
|
15
|
+
boto3 = "^1.33.5"
|
|
16
|
+
botocore = "^1.34.15"
|
|
17
|
+
certifi = "^2023.7.22"
|
|
18
|
+
cfgv = "3.2.0"
|
|
19
|
+
coverage = "5.4"
|
|
20
|
+
databricks-sql-connector = "^3.3.0"
|
|
21
|
+
distlib = "^0.3.8"
|
|
22
|
+
filelock = "^3.14.0"
|
|
23
|
+
flake8 = "^4.0.1"
|
|
24
|
+
identify = "1.5.13"
|
|
25
|
+
iniconfig = "1.1.1"
|
|
26
|
+
isort = "^5.10.1"
|
|
27
|
+
joblib= "1.3.2"
|
|
28
|
+
mccabe = "0.6.1"
|
|
29
|
+
mlflow = "2.22.2"
|
|
30
|
+
mock = "^4.0.3"
|
|
31
|
+
moto = "^4.2.7"
|
|
32
|
+
mypy-extensions = "0.4.3"
|
|
33
|
+
nodeenv = "^1.5.0"
|
|
34
|
+
numpy = "1.23.5"
|
|
35
|
+
packaging = "^23.1"
|
|
36
|
+
pandas = "1.4.4"
|
|
37
|
+
pluggy = "1.5.0"
|
|
38
|
+
polling = "0.3.2"
|
|
39
|
+
py = "^1.11.0"
|
|
40
|
+
pyarrow = "^14.0.0"
|
|
41
|
+
pydantic = "1.10.24"
|
|
42
|
+
pyparsing = "2.4.7"
|
|
43
|
+
pytest = "^8.3.3"
|
|
44
|
+
pytest-cov = "^3.0.0"
|
|
45
|
+
pytest-custom-exit-code = "0.3.0"
|
|
46
|
+
PyYAML = "6.0"
|
|
47
|
+
regex = "^2024.9.11"
|
|
48
|
+
requests = "^2.32.0"
|
|
49
|
+
responses = "0.23.1"
|
|
50
|
+
s3fs = "^2023.10.0"
|
|
51
|
+
six = "1.15.0"
|
|
52
|
+
toml = "0.10.2"
|
|
53
|
+
torch = "2.3.1"
|
|
54
|
+
typed-ast = "^1.5.3"
|
|
55
|
+
typing-extensions = "^4.4.0"
|
|
56
|
+
Werkzeug = "^3.0.3"
|
|
57
|
+
|
|
58
|
+
[build-system]
|
|
59
|
+
requires = ["poetry-core>=1.0.0"]
|
|
60
|
+
build-backend = "poetry.core.masonry.api"
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from typing import Dict
|
|
2
|
+
|
|
3
|
+
from pydantic_settings import BaseSettings
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Settings(BaseSettings):
|
|
7
|
+
|
|
8
|
+
AWS_DEFAULT_REGION: str
|
|
9
|
+
AWS_DEFAULT_REGION = "ap-southeast-2"
|
|
10
|
+
MODEL_SERVING_WORKLOAD_SIZE: str
|
|
11
|
+
MODEL_SERVING_WORKLOAD_SIZE = "Medium"
|
|
12
|
+
MODEL_SERVING_SCALE_TO_ZERO: str
|
|
13
|
+
MODEL_SERVING_SCALE_TO_ZERO = "True"
|
|
14
|
+
model_func_dict: Dict
|
|
15
|
+
model_func_dict = {
|
|
16
|
+
"sk_model": ["sklearn", "code_paths"],
|
|
17
|
+
"xgb_model": ["xgboost", "code_paths"],
|
|
18
|
+
"lgb_model": ["lightgbm", "code_paths"],
|
|
19
|
+
"keras_model": ["keras", "code_paths"],
|
|
20
|
+
"pytorch_model": ["pytorch", "code_paths"],
|
|
21
|
+
"python_model": ["pyfunc", "code_path"],
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
settings = Settings()
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
import importlib
|
|
3
|
+
import pathlib
|
|
4
|
+
from typing import Callable
|
|
5
|
+
from typing import Dict
|
|
6
|
+
from typing import List
|
|
7
|
+
|
|
8
|
+
import yaml
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def read_sql(file_path: str) -> str:
|
|
12
|
+
"""
|
|
13
|
+
reads sql file and return string
|
|
14
|
+
|
|
15
|
+
Parameters
|
|
16
|
+
----------
|
|
17
|
+
file_path : str
|
|
18
|
+
file path of sql file
|
|
19
|
+
|
|
20
|
+
Returns
|
|
21
|
+
-------
|
|
22
|
+
str
|
|
23
|
+
query string of sql file
|
|
24
|
+
"""
|
|
25
|
+
return pathlib.Path(file_path).read_text()
|
|
26
|
+
|
|
27
|
+
def load_yaml(path: str) -> Dict:
|
|
28
|
+
"""
|
|
29
|
+
function that loads the settings from the yaml file
|
|
30
|
+
|
|
31
|
+
Parameters
|
|
32
|
+
----------
|
|
33
|
+
path: str
|
|
34
|
+
path to the yaml file
|
|
35
|
+
|
|
36
|
+
Returns
|
|
37
|
+
-------
|
|
38
|
+
Dict
|
|
39
|
+
returns a dictionary with the settings
|
|
40
|
+
"""
|
|
41
|
+
with open(path) as f:
|
|
42
|
+
settings = yaml.safe_load(f)
|
|
43
|
+
return settings
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def get_test_date(datetime_provided: datetime.datetime, days_difference: int) -> int:
|
|
47
|
+
"""
|
|
48
|
+
function that returns a lookback date from specified days ago
|
|
49
|
+
|
|
50
|
+
Parameters
|
|
51
|
+
----------
|
|
52
|
+
datetime_provided: datetime.datetime
|
|
53
|
+
datetime input, most likely datetime.datetime.now()
|
|
54
|
+
days_difference: int
|
|
55
|
+
number days ago we should look back
|
|
56
|
+
|
|
57
|
+
Returns
|
|
58
|
+
-------
|
|
59
|
+
int
|
|
60
|
+
returns a date dim key, yyyyMMdd
|
|
61
|
+
"""
|
|
62
|
+
return int(
|
|
63
|
+
(datetime_provided - datetime.timedelta(days=days_difference)).strftime(
|
|
64
|
+
"%Y%m%d"
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def get_function_to_load(function_dict: Dict, file_format: str) -> Callable:
|
|
70
|
+
"""
|
|
71
|
+
function that retrieves the library function callable
|
|
72
|
+
|
|
73
|
+
Parameters
|
|
74
|
+
----------
|
|
75
|
+
function_dict: Dict
|
|
76
|
+
dictionary that contains the function mapping
|
|
77
|
+
file_format: str
|
|
78
|
+
name of file format, pkl, parquet or delta table
|
|
79
|
+
|
|
80
|
+
Returns
|
|
81
|
+
-------
|
|
82
|
+
Callable
|
|
83
|
+
function that we intend to use
|
|
84
|
+
"""
|
|
85
|
+
return getattr(
|
|
86
|
+
importlib.import_module(function_dict[file_format][0]),
|
|
87
|
+
function_dict[file_format][1],
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def get_target_stage_for_env(env: str) -> str:
|
|
92
|
+
"""
|
|
93
|
+
function to get corresponding target stage based on running environment
|
|
94
|
+
|
|
95
|
+
Parameters
|
|
96
|
+
----------
|
|
97
|
+
env: str
|
|
98
|
+
running environment "dev", "staging" or "prod"
|
|
99
|
+
|
|
100
|
+
Returns
|
|
101
|
+
-------
|
|
102
|
+
str
|
|
103
|
+
target stage
|
|
104
|
+
"Staging" for "dev" and "staging" env
|
|
105
|
+
"Production" for "prod" env
|
|
106
|
+
"""
|
|
107
|
+
|
|
108
|
+
if not (isinstance(env, str)) or (env.lower() not in ["dev", "staging", "prod"]):
|
|
109
|
+
raise ValueError("Invalid environment")
|
|
110
|
+
|
|
111
|
+
return "Staging" if env.lower() in {"dev", "staging"} else "Production"
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def get_date_intervals_model_drift(
|
|
115
|
+
date_int: int,
|
|
116
|
+
) -> List:
|
|
117
|
+
"""
|
|
118
|
+
function to list of date intervals for model drift
|
|
119
|
+
|
|
120
|
+
Parameters
|
|
121
|
+
----------
|
|
122
|
+
date_int: int
|
|
123
|
+
start date of monitoring in YYYYmmdd
|
|
124
|
+
|
|
125
|
+
Returns
|
|
126
|
+
-------
|
|
127
|
+
List
|
|
128
|
+
list of date intervals for model drift monitoring
|
|
129
|
+
"""
|
|
130
|
+
return_list = []
|
|
131
|
+
for i in range(1, 5):
|
|
132
|
+
return_list.append(f"{date_int}_hour_pair_{i}")
|
|
133
|
+
|
|
134
|
+
return return_list
|
|
File without changes
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
import databricks
|
|
6
|
+
import pandas as pd
|
|
7
|
+
from databricks.sql import connect
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class DatabricksSQLClient:
|
|
11
|
+
"""
|
|
12
|
+
Class that handles queries from databricks sql connector
|
|
13
|
+
Main purpose is to create a connection we can query from databricks sql warehouse
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(self):
|
|
17
|
+
self.engine = self._connect()
|
|
18
|
+
|
|
19
|
+
def _connect(self) -> databricks.sql.client.Connection:
|
|
20
|
+
"""
|
|
21
|
+
create a databricks sql connection
|
|
22
|
+
|
|
23
|
+
Returns
|
|
24
|
+
-------
|
|
25
|
+
databricks.sql.client.Connection
|
|
26
|
+
databricks sql warehouse connection engine
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
return connect(
|
|
30
|
+
server_hostname=os.environ["DATABRICKS_HOST"],
|
|
31
|
+
http_path=os.environ["DATABRICKS_SQL_PATH"],
|
|
32
|
+
access_token=os.environ["DATABRICKS_TOKEN"],
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
def query_as_pandas(self, final_query: str) -> pd.DataFrame:
|
|
36
|
+
"""
|
|
37
|
+
query databricks sqls with databricks connection and store them into pandas
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
final_query : str
|
|
42
|
+
query to run
|
|
43
|
+
|
|
44
|
+
Returns
|
|
45
|
+
-------
|
|
46
|
+
pd.DataFrame
|
|
47
|
+
return of pandas dataframe
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
_cursor = self.engine.cursor()
|
|
51
|
+
return_values = _cursor.execute(final_query).fetchall()
|
|
52
|
+
|
|
53
|
+
return pd.DataFrame(
|
|
54
|
+
return_values, columns=[col[0] for col in _cursor.description]
|
|
55
|
+
)
|