-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
280 lines (223 loc) · 8.31 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from dataclasses import dataclass
from typing import Any, List, Optional
from urllib.parse import ParseResult, parse_qs, urljoin, urlparse
import azure.mgmt.datafactory.models as DfModels
from metaphor.common.entity_id import dataset_normalized_name
from metaphor.common.logger import get_logger
from metaphor.common.snowflake import normalize_snowflake_account
from metaphor.common.utils import removesuffix
from metaphor.models.metadata_change_event import (
DataPlatform,
Dataset,
DatasetLogicalID,
DatasetSchema,
EntityUpstream,
SchemaType,
)
logger = get_logger()
@dataclass
class LinkedService:
database: Optional[str] = None
account: Optional[str] = None
project: Optional[str] = None
url: Optional[str] = None
def get_abs_full_path(
storage_account: str, abs_location: DfModels.AzureBlobStorageLocation
) -> str:
parts: List[str] = [
part # type: ignore
for part in [
abs_location.container,
abs_location.folder_path,
abs_location.file_name,
]
if part is not None and isinstance(part, str)
]
full_path = urljoin(storage_account, "/".join(parts))
return full_path
def get_adls_path(url: str, blob_fs_location: DfModels.AzureBlobFSLocation) -> str:
parts: List[str] = [
part # type: ignore
for part in [
blob_fs_location.file_name,
blob_fs_location.folder_path,
blob_fs_location.file_system,
]
if part is not None and isinstance(part, str)
]
full_path = urljoin(url, "/".join(parts))
return full_path
def init_dataset(
name: Optional[str],
platform: DataPlatform,
schema_type: Optional[SchemaType] = None,
account: Optional[str] = None,
) -> Dataset:
return Dataset(
logical_id=DatasetLogicalID(
name=name,
platform=platform,
account=account,
),
entity_upstream=EntityUpstream(source_entities=[]),
schema=DatasetSchema(schema_type=schema_type) if schema_type else None,
)
def init_snowflake_dataset(
snowflake_dataset: DfModels.SnowflakeDataset, linked_service: LinkedService
) -> Dataset:
schema = safe_get_from_json(snowflake_dataset.schema_type_properties_schema)
table = safe_get_from_json(snowflake_dataset.table)
database = linked_service.database
return init_dataset(
dataset_normalized_name(database, schema, table),
DataPlatform.SNOWFLAKE,
account=linked_service.account,
)
def init_azure_sql_table_dataset(
sql_table_dataset: DfModels.AzureSqlTableDataset, linked_service: LinkedService
) -> Dataset:
schema = safe_get_from_json(sql_table_dataset.schema_type_properties_schema)
table = safe_get_from_json(sql_table_dataset.table)
return init_dataset(
name=dataset_normalized_name(linked_service.database, schema, table),
platform=DataPlatform.MSSQL,
account=linked_service.account,
)
def init_json_dataset(
json_dataset: DfModels.JsonDataset, linked_service: LinkedService
) -> Optional[Dataset]:
if (
isinstance(json_dataset.location, DfModels.HttpServerLocation)
and linked_service.url
):
return init_dataset(
name=linked_service.url,
platform=DataPlatform.HTTP,
schema_type=SchemaType.JSON,
)
if (
isinstance(json_dataset.location, DfModels.AzureBlobFSLocation)
and linked_service.url
):
blob_fs_location = json_dataset.location
adls_path = get_adls_path(linked_service.url, blob_fs_location)
return init_dataset(
name=adls_path,
platform=DataPlatform.AZURE_DATA_LAKE_STORAGE,
schema_type=SchemaType.JSON,
)
if (
isinstance(json_dataset.location, DfModels.AzureBlobStorageLocation)
and linked_service.account
):
abs_location = json_dataset.location
abs_full_path = get_abs_full_path(linked_service.account, abs_location)
return init_dataset(
name=abs_full_path,
platform=DataPlatform.AZURE_BLOB_STORAGE,
schema_type=SchemaType.JSON,
)
return None
def init_parquet_dataset(
parquet_dataset: DfModels.ParquetDataset, linked_service: LinkedService
) -> Optional[Dataset]:
if (
isinstance(parquet_dataset.location, DfModels.AzureBlobFSLocation)
and linked_service.url
):
blob_fs_location = parquet_dataset.location
adls_path = get_adls_path(linked_service.url, blob_fs_location)
return init_dataset(
name=adls_path,
platform=DataPlatform.AZURE_DATA_LAKE_STORAGE,
schema_type=SchemaType.PARQUET,
)
if (
isinstance(parquet_dataset.location, DfModels.AzureBlobStorageLocation)
and linked_service.account
):
abs_full_path = get_abs_full_path(
linked_service.account, parquet_dataset.location
)
return init_dataset(
name=abs_full_path,
platform=DataPlatform.AZURE_BLOB_STORAGE,
schema_type=SchemaType.PARQUET,
)
return None
def init_rest_dataset(
rest_dataset: DfModels.RestResourceDataset, linked_service: LinkedService
) -> Dataset:
http_endpoint = linked_service.url
return init_dataset(
name=http_endpoint,
platform=DataPlatform.HTTP,
)
def init_delimited_text_dataset(
delimited_text_dataset: DfModels.DelimitedTextDataset, linked_service: LinkedService
) -> Optional[Dataset]:
if (
isinstance(delimited_text_dataset.location, DfModels.AzureBlobFSLocation)
and linked_service.url
):
blob_fs_location = delimited_text_dataset.location
adls_path = get_adls_path(linked_service.url, blob_fs_location)
return init_dataset(
name=adls_path,
platform=DataPlatform.AZURE_DATA_LAKE_STORAGE,
schema_type=SchemaType.SCHEMALESS,
)
if (
isinstance(delimited_text_dataset.location, DfModels.AzureBlobStorageLocation)
and linked_service.account
):
abs_full_path = get_abs_full_path(
linked_service.account, delimited_text_dataset.location
)
return init_dataset(
name=abs_full_path,
platform=DataPlatform.AZURE_BLOB_STORAGE,
schema_type=SchemaType.SCHEMALESS,
)
return None
def process_snowflake_linked_service(
snowflake_connection: DfModels.SnowflakeLinkedService, linked_service_name: str
) -> Optional[LinkedService]:
connection_string = safe_get_from_json(snowflake_connection.connection_string)
if connection_string is None:
logger.warning(
f"unknown connection string for {linked_service_name}, connection string: {connection_string}"
)
return None
url: ParseResult = urlparse(connection_string)
query_db = parse_qs(url.query or "").get("db")
database = query_db[0] if query_db else None
# extract snowflake account name from jdbc format, 'snowflake://<snowflake_host>/'
hostname = urlparse(url.path).hostname
snowflake_account = normalize_snowflake_account(hostname) if hostname else None
return LinkedService(database=database, account=snowflake_account)
def process_azure_sql_linked_service(
sql_database: DfModels.AzureSqlDatabaseLinkedService, linked_service_name: str
) -> Optional[LinkedService]: # format: <key>=<value>;<key>=<value>
connection_string = safe_get_from_json(sql_database.connection_string)
if connection_string is None:
logger.warning(
f"unknown connection string for {linked_service_name}, connection string: {connection_string}"
)
return None
server_host, database = None, None
for kv_pair in connection_string.split(";"):
[key, value] = kv_pair.split("=") if "=" in kv_pair else ["", ""]
if key == "Data Source":
server_host = (
removesuffix(str(value), ".database.windows.net") if value else None
)
if key == "Initial Catalog":
database = value
return LinkedService(database=database, account=server_host)
def safe_get_from_json(prop: Any) -> Optional[str]:
if isinstance(prop, str):
return prop
else:
logger.warning(f"Unable to get value from {prop}")
return None