-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemble_cargo.py
149 lines (129 loc) · 3.81 KB
/
assemble_cargo.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
import argparse
from pyspark.sql import SparkSession
dataset_names = ['cargodesc', 'hazmat', 'hazmatclass']
def create_spark_session():
spark = SparkSession \
.builder \
.getOrCreate()
return spark
def create_temp_view(spark, name, input_dir):
"""
Create a Spark temporary view for a dataset
Parameters
----------
spark : Spark session
Current Spark session
name : str
Name of the dataset
input_dir : str
Input directory
"""
df = spark.read \
.option("header", True) \
.option("escape", '"') \
.option("inferSchema", True) \
.csv(f"{input_dir}/ams/*/*/ams__{name}_*__*.csv")
df.createOrReplaceTempView(name)
def create_hts_temp_view(spark, input_dir):
"""
Create a Spark temporary view for the hts data
Parameters
----------
spark : Spark session
Current Spark session
input_dir : str
Input directory
"""
df = spark.read \
.option("header", True) \
.option("escape", '"') \
.option("inferSchema", True) \
.csv(f"{input_dir}/hts.csv")
df.createOrReplaceTempView("tariff_harmonized_schedule")
def process_cargo_data(spark, input_dir, output):
"""
Process cargo data by running a query to select fields
Parameters
----------
spark : Spark session
Current Spark session
input_dir : str
Input directory
output : str
Output directory
"""
for name in dataset_names:
create_temp_view(spark, name, input_dir)
create_hts_temp_view(spark, input_dir)
cargo_table = spark.sql("""
SELECT
c.identifier,
c.container_number,
c.description_sequence_number AS sequence_number,
c.piece_count,
c.description_text AS description,
h.hazmat_code,
(CASE
WHEN (hc.hazmat_classification IS NOT NULL) THEN hc.hazmat_classification
ELSE h.hazmat_class
END) AS hazmat_class,
h.hazmat_code_qualifier,
h.hazmat_contact,
h.hazmat_page_number,
h.hazmat_flash_point_temperature,
h.hazmat_flash_point_temperature_negative_ind,
h.hazmat_flash_point_temperature_unit,
h.hazmat_description,
t.harmonized_number,
t.harmonized_value,
t.harmonized_weight,
t.harmonized_weight_unit,
ts.description AS harmonized_tariff_schedule_desc,
ts.general_rate_of_duty,
ts.special_rate_of_duty,
ts.column_2_rate_of_duty,
ts.quota_quantity,
ts.additional_duties
FROM cargo_desc AS c
LEFT JOIN hazmat AS h
ON
c.identifier = h.identifier AND
c.container_number = h.container_number AND
c.description_sequence_number = h.hazmat_sequence_number
LEFT JOIN hazmat_class AS hc
ON
c.identifier = hc.identifier AND
c.container_number = hc.container_number AND
c.description_sequence_number = hc.hazmat_sequence_number
LEFT JOIN tariff as t
ON
c.identifier = t.identifier AND
c.container_number = t.container_number AND
c.description_sequence_number = t.description_sequence_number
LEFT JOIN tariff_harmonized_schedule as ts
ON
CAST(t.harmonized_number as string) = ts.hts_number
WHERE c.identifier IS NOT NULL AND c.container_number IS NOT NULL
""")
cargo_table.repartition(1).write.mode('overwrite').format("csv") \
.option("header", True) \
.option("escape", '"') \
.save(f"{output}/cargo/")
def main(input_dir, output):
"""
Process cargo data
Parameters
----------
input_dir : str
Input directory
output : str
Output directory
"""
spark = create_spark_session()
process_cargo_data(spark, input_dir, output)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', default='/input')
parser.add_argument('-o', '--output', default='/output')
args = parser.parse_args()
main(args.input, args.output)