-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathETL_337977045_322228974_322995358.py
177 lines (150 loc) · 6.67 KB
/
ETL_337977045_322228974_322995358.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
# this code is designed to run on the spark cluster
# based on my understanding, the code can be designed for either
from pyspark.sql import SparkSession
import pyspark.sql.functions as F
from pyspark.sql.types import *
from sklearn.cluster import KMeans
appendage = "AfikChecking"
termination_time = 60*60*3.5
# countries to load
country_list = ["UK", "FR", "GM", "SP", "NO"]
topics = "UK, FR, GM, SP, NO"
format_for_server = "com.microsoft.sqlserver.jdbc.spark"
def init_spark(app_name: str):
spark = SparkSession.builder.appName(app_name).getOrCreate()
sc = spark.sparkContext
return spark, sc
print("Program started: " + appendage)
print("time alloted to streaming: " + str(termination_time))
spark, sc = init_spark('ETL')
# schema for importing data from kafka
basic_schema = StructType([StructField('StationId', StringType(), False),
StructField('Date', IntegerType(), False),
StructField('Variable', StringType(), False),
StructField('Value', IntegerType(), False),
StructField('M_Flag', StringType(), True),
StructField('Q_Flag', StringType(), True),
StructField('S_Flag', StringType(), True),
StructField('ObsTime', StringType(), True)])
# schema for state of aggregation
parquet_schema = StructType([StructField('StationId', StringType(), True),
StructField('maxTemp', IntegerType(), True),
StructField('minTemp', IntegerType(), True),
StructField('avgPrcp', DoubleType(), True)])
# database information
username = "arieln"
password = "Qwerty12!"
server_name = "jdbc:sqlserver://technionddscourse.database.windows.net:1433"
database_name = "arieln"
url = server_name + ";" + "databaseName=" + database_name + ";"
# Our kafka server
kafka_server = 'dds2020s-kafka.eastus.cloudapp.azure.com:9092'
# date range
min_date = 20160000
max_date = 20210000
station_df = spark.read.text('ghcnd-stations.txt')
station_df = station_df.select(station_df.value.substr(0, 11).alias('StationId'),
station_df.value.substr(12, 9).cast("float").alias('latitude'),
station_df.value.substr(22, 9).cast("float").alias('longitude'))\
.withColumn("country", F.col("StationId").substr(1, 2))\
.filter(F.col("country").isin(country_list)) \
.drop(F.col("country"))
print("read stations")
# overwrite folder with empty dataframe for aggregation state
emptyRDD = spark.sparkContext.emptyRDD()
spark.createDataFrame(emptyRDD, parquet_schema)\
.write.mode("overwrite").parquet("tmpFile.parquet")
output_mode = "append"
filtered_df = spark.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", kafka_server) \
.option("subscribe", topics) \
.option("maxOffsetsPerTrigger", "500000") \
.option("startingOffsets", "earliest") \
.load() \
.selectExpr("CAST(value AS STRING)") \
.select(F.from_json(F.col("value"), schema=basic_schema).alias('json')) \
.select("json.*") \
.filter((F.col("Date") >= min_date) & (F.col("Date") <= max_date)) \
.withColumn("Date", F.to_date(F.col("Date").cast("string"), 'yyyyMMdd')) \
.drop("M_Flag", "S_Flag", "ObsTime") \
.filter(F.col("Variable").isin(["TMAX", "TMIN", "PRCP"])) \
.filter(F.col("Q_Flag").isNull()) \
.drop("Q_Flag")
def writeToServer(df, epoch):
"""
For each batch,
1. Write to time_df the percipitation that fell each day
2. Update the parquet that saves the aggregation state for each Station per year and month
:param df:
:param epoch:
:return:
"""
df.persist()
df.filter(F.col("Variable") == "PRCP")\
.drop("Variable")\
.withColumnRenamed("Value", "label")\
.write \
.format(format_for_server) \
.mode("append") \
.option("url", url) \
.option("dbtable", "timeDF") \
.option("user", username) \
.option("password", password) \
.save()
# if not empty, then update state table
if len(df.take(1)) != 0:
tmax_temp = df.filter(F.col("Variable") == "TMAX").groupby("StationId").max("Value") \
.withColumnRenamed("max(Value)", "maxTemp")
tmin_temp = df.filter(F.col("Variable") == "TMIN").groupby("StationId").min("Value") \
.withColumnRenamed("min(Value)", "minTemp")
prcp_temp = df.filter(F.col("Variable") == "PRCP").groupBy("StationId").agg(F.avg("Value").alias("avgPrcp"))
current = tmax_temp.join(tmin_temp, on="StationId").join(prcp_temp, on="StationId")
current.write.mode("append").parquet("tmpFile.parquet")
df.unpersist()
# run query
query = filtered_df\
.writeStream\
.outputMode(output_mode)\
.foreachBatch(lambda df, epoch_id: writeToServer(df, epoch_id))\
.start()
query.awaitTermination(termination_time)
#after finished perform clustering
print("starting clustering")
pdclustering_df = spark.read.parquet("tmpFile.parquet").groupBy("StationId")\
.agg(F.avg("maxTemp").alias("maxTemp"), F.avg("minTemp").alias("minTemp"), \
F.avg("avgPrcp").alias("avgPrcp")).toPandas()
print(pdclustering_df.head())
pdclustering_df["cluster_label"] = KMeans(n_clusters = 3).fit_predict(pdclustering_df[["avgPrcp", "minTemp", "maxTemp"]])
clustering_df = spark.createDataFrame(pdclustering_df)
print("finished clustering combining with station_df")
spatial_df = station_df.join(clustering_df, on="StationId")
print("writing spatial df")
spatial_df.write \
.format(format_for_server) \
.mode("append") \
.option("url", url) \
.option("dbtable", "spatialDF") \
.option("user", username) \
.option("password", password) \
.save()
print("reading time_df")
prcp_df = spark.read \
.format("com.microsoft.sqlserver.jdbc.spark") \
.option("url", url) \
.option("dbtable", "timeDF") \
.option("user", username) \
.option("password", password).load()
print("joining spatial and time for model")
model_df = prcp_df.join(spatial_df.drop("latitude", "longitude", "maxTemp", "minTemp"), on="StationId", how="inner")\
.withColumn("month", F.month("Date")).withColumn("year", F.year("Date")).drop("Date")
print("writing end model")
model_df.write \
.format(format_for_server) \
.mode("append") \
.option("url", url) \
.option("dbtable", "modelDF") \
.option("user", username) \
.option("password", password) \
.save()
print("finished")