-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
45 lines (32 loc) · 922 Bytes
/
utils.py
File metadata and controls
45 lines (32 loc) · 922 Bytes
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
import json
import os
import pyspark.sql.types as spark_types
import shutil
def spark_schema_from_json(j):
return spark_types.StructType([
spark_types.StructField(
f["name"],
spark_types._parse_datatype_string(f.get("type", "string")),
f.get("nullable", True),
f.get("metadata", None)
)
for f in j
])
def load_schema():
with open("ghtorrent-schema.json") as f:
db_schema = json.load(f)
return db_schema
def remove(path):
if os.path.exists(path):
shutil.rmtree(path)
def read_csv(spark_ctx, path, schema_key=None):
# Extract filename from path
file = os.path.basename(path)
if schema_key is None:
schema_key = file
schema = load_schema()[schema_key]
return spark_ctx.read.csv(
path=path,
schema=spark_schema_from_json(schema),
nullValue="\\N",
)