This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
json.py
268 lines (217 loc) · 6.51 KB
/
json.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
import glob
import json
import logging
import os
import subprocess
from os.path import expanduser
from pathlib import Path
from tempfile import mkstemp
from typing import Any, Dict, List, Optional, Union
import boto3
import IPython.display
from IPython.display import JSON
from aws_orbit_sdk.common import get_properties, get_workspace, split_s3_path
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger()
def display_json(doc: Dict[str, Any], root: Optional[str] = "root") -> IPython.core.display.JSON:
"""
Create a JSON display object given raw JSON data.
Parameters
----------
doc : JSON dict or list
Raw JSON data to display.
root : str, optional
The name of the root element of the JSON tree (default 'root').
Returns
-------
IPython display of JSON data : IPython.core.display.JSON
An IPython JSON display representing the data.
Example
-------
>>> from aws.utils.notebooks import json as json_utils
>>> json_utils.display_json(doc=my_schemas ,root="database")
"""
return JSON(doc)
def write_json(doc: Any, path: str) -> None:
"""
Write data to a s3 bucket path.
Parameters
----------
doc : Any (Object Data)
Data to write to a s3 bucket.
path : str
s3 Bucket path for where to write data.
Returns
-------
None
None.
Example
-------
>>> from aws.utils.notebooks import json as json_utils
>>> data = {
... 'id' : [1]
... 'name': ['Noah']
... }
>>> write_json(doc=data, path='testbucket123')
"""
s3 = boto3.client("s3")
if path.startswith("s3://"):
(bucket, key) = split_s3_path(path)
s3.put_object(Body=doc, Bucket=bucket, Key=key)
dir = os.path.dirname(path)
Path(dir).mkdir(parents=True, exist_ok=True)
with open(path, "w") as outfile:
json.dump(doc, outfile)
def run_schema_induction(
data_path: str,
table_name: str,
s3_location: str,
root_definition_name: str,
is_array: Optional[bool] = True,
) -> Dict[str, Dict[str, str]]:
"""
Calls on helper functions to run Schema Induction with given user arguments and returns ddl and schema metadata.
Parameters
----------
data_path : str
An input json file path.
table_name : str
Table name to use when creating DDL.
s3_location : str
Table location to use when creating DDL.
root_definition_name : str
The root directory name of the data.
is_array : bool, optional
Is the document a json array.
Returns
-------
ddl : str
SQL ddl statement to create new external table with given metadata
schema : dict
Schema metadata stored as a json for the specified table.
Example
-------
>>> from aws.utils.notebooks import json as json_utils
>>>table = json_utils.run_schema_induction(data_path=f's3://{target_bucket}/Claim-1/data.json',
... table_name='users.table',
... s3_location=f's3://{target_bucket}/Claim-1/',
... root_definition_name='Claim',
... is_array=True)
>>> json_utils.display_json(table['schema'])
>>> ddl = table['ddl']
"""
logger.info("Start induction process for " + table_name)
args = [
"-i",
data_path,
"-c",
"ec2",
"-t",
table_name,
"--location",
s3_location,
"--root",
root_definition_name,
]
if is_array:
args.append("-a")
ret = run_schema_induction_args(args)
logger.info("Finish induction process for " + table_name)
return ret
def run_process(args: Union[str, List[str]]) -> None:
"""
Helper function called by run_schema_induction_args() to log outputs and errors.
Parameters
----------
args : Union[str, List[str]]
User arguments used for the schema induction process to run.
Returns
-------
None
None.
"""
try:
completed = subprocess.run(
args,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except subprocess.CalledProcessError as err:
logger.error(f"ERROR: {err}")
else:
out = completed.stdout.decode("utf-8")
err = completed.stderr.decode("utf-8")
if len(out) > 0:
logger.info(out)
if len(err):
logger.error(err)
def run_schema_induction_args(user_args: Union[str, List[str]]) -> Dict[str, Dict[str, str]]:
"""
Calls on run_process to run Schema Induction with given user arguments gets ddl and schema metadata for a specified
table.
Parameters
----------
user_args : Union[str, List[str]]
User parameters including ( data_path, instance type, table_name, s3_location, and root definition name)
Returns
-------
ddl : str
SQL ddl statement to create new external table with given metadata.
schema :
Schema metadata stored as a json for the specified table.
Examples
--------
>>>from aws.utils.notebooks import json as json_utils
>>>args = [ "-i data_path -c ec2 -t table_name --location s3_location --root ClaimData"]
>>>args.append("-a")
>>>table = json_utils.run_schema_induction_args(args)
"""
ddlFd, ddl_path = mkstemp()
schemaFd, schema_path = mkstemp()
home = expanduser("~")
jar = glob.glob(f"{home}/orbit/java/schema-induction*.jar")[0]
args = [
"/opt/jdk-13.0.1/bin/java",
"-jar",
f"{jar}",
"-s",
schema_path,
"-d",
ddl_path,
]
args.extend(user_args)
run_process(args)
ddl = readFile(ddl_path, ddlFd)
schema = readFile(schema_path, schemaFd)
return {"ddl": ddl, "schema": schema}
def readFile(path: str, fd: int) -> Any:
"""
Read File Contents located at a specified path.
Parameters
----------
path : str
File path to locate file to read.
fd : int
Associated file descriptor
Returns
-------
content : Any
File contents
Example
-------
>>>from aws.utils.notebooks import json as json_utils
>>>path = "/home/ihritik/Desktop/file2.txt"
>>>fd = os.open( "foo.txt", os.O_RDONLY)
>>>data = json_utils.readFile(path=path,fd=fd)
"""
file = open(path, "r")
content = file.read()
file.close()
os.close(fd)
os.remove(path)
return content