-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscover.py
151 lines (115 loc) · 5.43 KB
/
discover.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
from api_client_factory import ApiClientFactory
import delta_sharing
import publisher_pb2 as pb2
from concurrent.futures import wait
from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import ThreadPoolExecutor
import json
import numpy as np
import pandas as pd
def get_type(data_type: str) -> pb2.PropertyType:
try:
data_type = data_type.lower()
if 'object' in data_type or 'string' in data_type:
return pb2.PropertyType.STRING
elif 'float' in data_type:
return pb2.PropertyType.FLOAT
elif 'int' in data_type:
return pb2.PropertyType.INTEGER
elif 'datetime' in data_type:
return pb2.PropertyType.DATETIME
elif 'bool' in data_type:
return pb2.PropertyType.BOOL
else:
return pb2.PropertyType.STRING
except Exception as e:
print(f'in get type:{e}')
def read_df(table_url, limit):
if limit < 0:
df = delta_sharing.load_as_pandas(table_url)
else:
df = delta_sharing.load_as_pandas(table_url, limit=limit)
return df
def get_all_schemas_concurrent(api_client_factory: ApiClientFactory, sample_size: int = 5):
api_client = api_client_factory.get_api_client()
schemas = []
tables = api_client.sharing_client.list_all_tables()
# executor = ProcessPoolExecutor(10)
schema = None
with ProcessPoolExecutor(max_workers=10) as executor:
futures = []
for table in tables:
schema_id = f'{table.share}.{table.schema}.{table.name}'
schema = pb2.Schema(id=schema_id, name=schema_id, data_flow_direction=pb2.Schema.DataFlowDirection.READ)
table_url = api_client.profile_file + f'#{schema_id}'
future = executor.submit(read_df, table_url, sample_size)
futures.append(future)
schemas.append(schema)
wait(futures)
for idx, future in enumerate(futures):
df = future.result()
schema = schemas[idx]
try:
for col in df.columns:
type_at_source = str(df[col].dtype)
type_at_dest = get_type(type_at_source)
column_property = pb2.Property(id=str(col), name=str(col), type=type_at_dest, type_at_source=type_at_source)
schema.properties.append(column_property)
df = df.replace({pd.NaT: None}).replace(np.nan, None)
for index, record in df.iterrows():
data = json.dumps(record.to_dict(), default=str)
record = pb2.Record(data_json=data)
schema.sample.append(record)
except Exception as e:
print(f'error in discover:{e}')
executor.shutdown(wait=False)
return schemas
def get_all_schemas(api_client_factory: ApiClientFactory, sample_size: int = 5):
api_client = api_client_factory.get_api_client()
schemas = []
tables = api_client.sharing_client.list_all_tables()
schema = None
for table in tables:
schema_id = f'{table.share}.{table.schema}.{table.name}'
schema = pb2.Schema(id=schema_id, name=schema_id, data_flow_direction=pb2.Schema.DataFlowDirection.READ)
schemas.append(schema)
table_url = api_client.profile_file + f'#{schema_id}'
df = read_df(table_url, sample_size)
try:
for col in df.columns:
type_at_source = str(df[col].dtype)
type_at_dest = get_type(type_at_source)
column_property = pb2.Property(id=str(col), name=str(col), type=type_at_dest, type_at_source=type_at_source)
schema.properties.append(column_property)
df = df.replace({pd.NaT: None}).replace(np.nan, None)
for index, record in df.iterrows():
data = json.dumps(record.to_dict(), default=str)
record = pb2.Record(data_json=data)
schema.sample.append(record)
except Exception as e:
print(f'error in discover:{e}')
return schemas
def get_count_of_records(api_client_factory: ApiClientFactory, schema: pb2.Schema):
return pb2.Count.Kind.UNAVAILABLE
def get_refresh_schema_for_table(api_client_factory: ApiClientFactory, schema, sample_size: int = 5):
api_client = api_client_factory.get_api_client()
table_url = api_client.profile_file + f'#{schema.id}'
df = read_df(table_url, sample_size)
del schema.properties[:]
try:
for col in df.columns:
type_at_source = str(df[col].dtype)
type_at_dest = get_type(type_at_source)
column_property = pb2.Property(id=str(col), name=str(col), type=type_at_dest, type_at_source=type_at_source)
schema.properties.append(column_property)
df = df.replace({pd.NaT: None}).replace(np.nan, None)
for index, record in df.iterrows():
data = json.dumps(record.to_dict(), default=str)
record = pb2.Record(data_json=data)
schema.sample.append(record)
except Exception as e:
print(f'error in discover:{e}')
return schema
def get_refresh_schemas(api_client_factory: ApiClientFactory, refresh_schemas, sample_size: int = 5):
for schema in refresh_schemas:
yield get_refresh_schema_for_table(api_client_factory, schema, sample_size)