-
Notifications
You must be signed in to change notification settings - Fork 34
/
spark_consumer.py
506 lines (424 loc) · 20.8 KB
/
spark_consumer.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# Use findspark to import pyspark as a python module
import findspark
findspark.init('/root/spark-2.4.4-bin-hadoop2.7')
from pyspark.sql import SparkSession
from pyspark.sql import types
from pyspark.sql import functions as F
from pyspark.sql.functions import udf
from config import event_list_repl, event_values, mysql_user, mysql_password, kafka_config
from config import mysql_database_name, mysql_table_name, mysql_hostname, mysql_port
from config import get_cot, get_vix, get_stock_volume, bid_levels, ask_levels
from kafka import SimpleClient
from kafka.common import OffsetRequestPayload
import logging
mysql_driver = 'com.mysql.jdbc.Driver'
mysql_jdbc_url = 'jdbc:mysql://' + mysql_hostname + ':' + mysql_port + '/' + mysql_database_name
# Instantiate spark session.
# We have to add the following jars to intergrate Spark with Kafka:
# spark-sql-kafka, kafka-clients, spark-streaming-kafka-0-10-assembly
# where 0-10 is the Kafka broker version, 2.11-2.4.4 is Spark version,
# use proper jars according to your versions.
# To integrate with MySQL/MariaDB we have to add the following file:
# mysql-connector-java-5.1.48.jar.
# For testing we will run Spark locally with one worker thread (.master("local"))
# Other options to run Spark (locally, on cluster) can be found here:
# http://spark.apache.org/docs/latest/submitting-applications.html#master-urls
spark = SparkSession.builder \
.master("local") \
.appName("Stock_data_streaming") \
.config("spark.jars", "jar_files/spark-sql-kafka-0-10_2.11-2.4.4.jar,"\
"jar_files/kafka-clients-2.0.0.jar,"\
"jar_files/spark-streaming-kafka-0-10-assembly_2.11-2.1.1.jar,"\
"jar_files/mysql-connector-java-5.1.48.jar") \
.config("spark.driver.extraClassPath", "jar_files/spark-sql-kafka-0-10_2.11-2.4.4.jar,"\
"jar_files/kafka-clients-2.0.0.jar,"\
"jar_files/spark-streaming-kafka-0-10-assembly_2.11-2.1.1.jar,"\
"jar_files/mysql-connector-java-5.1.48.jar") \
.getOrCreate()
# Set number of output partitions (low values speed up processing)
spark.conf.set("spark.sql.shuffle.partitions", 5)
# Set log level
spark.sparkContext.setLogLevel("ERROR")
@udf(returnType=types.LongType())
def count_kafka_mssg(topic, server):
"""Returns the total number of messages (sum of all partitions) in given kafka topic
"""
client = SimpleClient(server)
partitions = client.topic_partitions[topic]
offset_requests = [OffsetRequestPayload(topic, p, -1, 1) for p in partitions.keys()]
offsets_responses = client.send_offset_request(offset_requests)
total_mssg = 0
for r in offsets_responses:
logging.info("partition = {}, offset = {}".format(r.partition, r.offsets[0]))
total_mssg += int(r.offsets[0])
return total_mssg
def write_stream_to_mysql(dataFrame, id):
"""Writes each batch of streaming dataFrame to MySQL/MariaDB
"""
db_properties = {"user": mysql_user,
"password": mysql_password,
"driver": mysql_driver}
if dataFrame.rdd.isEmpty():
pass
else:
dataFrame \
.write \
.jdbc(url=mysql_jdbc_url,
table=mysql_table_name,
mode='append',
properties=db_properties)
# Define VIX schema
# {"VIX": 16.04, "Timestamp": "2020-02-07 09:26:12"}
schema_vix = types.StructType([
types.StructField('VIX', types.FloatType()),
types.StructField('Timestamp', types.StringType())
])
# Construct a streaming DataFrame that reads from 'vix' topic
df_vix = spark \
.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", ", ".join(kafka_config['servers'])) \
.option("subscribe", kafka_config['topics'][0]) \
.option("startingOffsets", "latest") \
.option("failOnDataLoss", "false") \
.load() \
.selectExpr("CAST(value AS STRING)") \
.select(F.from_json(F.col("value"), schema_vix).alias("VIX")) \
.select("VIX.VIX", "VIX.Timestamp") \
.withColumn("Timestamp_vix", F.to_timestamp(F.col("Timestamp"), "yyyy-MM-dd HH:mm:ss")) \
.drop("Timestamp")
# .withColumn("ID", count_kafka_mssg(F.lit('vix'), F.lit('localhost:9092'))) # unique constantly incremental value
# Round timestamps down to nearest 5 minutes
df_vix = df_vix \
.withColumn("Timestamp_vix_floor", (F.floor(F.unix_timestamp("Timestamp_vix") / (5 * 60)) * 5 * 60).cast("timestamp"))
# Apply watermark
df_vix = df_vix.withWatermark("Timestamp_vix", "5 minutes")
# Below is the code to calculate the Moving Average for VIX using a Spark Structured Streaming Window function.
# A temporary variable id_timestamp will be used to simulate fixed time intervals. The result of window operation (moving average)
# subsequently has to be joined with the original data frame.
# In this application there is a need to perform many such window aggregations, however in Spark 2.4.4 multiple streaming aggregations
# are not yet supported on Streaming DataFrame, thus this functionality has to be moved to MariaDB.
# Create temporary column 'id_timestamp' that contains constantly incremental values with fixed time intervals
# that represent continuous stream of data (bars or candles on a chart) over which the window will be apllied.
# ID of value 1 equals 1 time interval that is represented by id_timestamp as 1 second.
# id_timestamp dosen't represent right point in time, it is used only to incorporate fixed time intervals.
# df_vix = df_vix.withColumn('id_timestamp', F.to_timestamp(F.from_unixtime(F.col('ID'))))
# Calculate 100 minute moving average
# time_interval = 5 min (corresponds to id_timestamp = 1 second)
# number_of_data_points (IDs) = 100 / 5 = 20 (how many bars we need?)
# id_timestamp_seconds = 20 sec (equals number_of_data_points)
# 20 sec = 20 data_points, time_interval = 1 second
# windowedAVG = df_vix \
# .withWatermark("id_timestamp", "0 seconds") \
# .groupBy(
# F.window("id_timestamp", "20 seconds", "1 seconds")) \
# .avg("VIX") \
# .select("window.end", "avg(VIX)")
# df_vix = df_vix.withWatermark("id_timestamp", "0 seconds")
# df_joined = windowedAVG \
# .join(df_vix, F.expr("""
# (end = id_timestamp AND
# id_timestamp >= end AND
# id_timestamp <= end + interval 1 seconds)
# """), "leftOuter")
# Define Volume schema
# {'1_open': 334.02, '2_high': 334.11, '3_low': 333.91, '4_close': 333.96,
# '5_volume': 1061578, 'Timestamp': '2020-02-06 16:00:00'}
schema_volume = types.StructType([
types.StructField('1_open', types.FloatType()),
types.StructField('2_high', types.FloatType()),
types.StructField('3_low', types.FloatType()),
types.StructField('4_close', types.FloatType()),
types.StructField('5_volume', types.IntegerType()),
types.StructField('Timestamp', types.StringType())
])
# Construct a streaming DataFrame that reads from 'volume' topic
df_volume = spark \
.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", ", ".join(kafka_config['servers'])) \
.option("subscribe", kafka_config['topics'][1]) \
.option("startingOffsets", "latest") \
.option("failOnDataLoss", "false") \
.load() \
.selectExpr("CAST(value AS STRING)") \
.select(F.from_json(F.col("value"), schema_volume).alias("Volume")) \
.select("Volume.*") \
.withColumn("Timestamp_vol", F.to_timestamp(F.col("Timestamp"), "yyyy-MM-dd HH:mm:ss")) \
.drop("Timestamp")
# Round timestamps down to nearest 5 minutes
df_volume = df_volume \
.withColumn("Timestamp_vol_floor", (F.floor(F.unix_timestamp("Timestamp_vol") / (5 * 60)) * 5 * 60).cast("timestamp"))
# Apply watermark
df_volume = df_volume.withWatermark("Timestamp_vol", "5 minutes")
# Calculate wick percentage
df_volume = df_volume \
.withColumn("candle_size", F.col("2_high") - F.col("3_low")) \
.withColumn("wick_size", F.when(F.col("4_close") >= F.col("1_open"), (F.col("2_high") - F.col("4_close"))) \
.otherwise(F.col("3_low") - F.col("4_close"))) \
.withColumn("wick_prct", F.col("wick_size") / F.col("candle_size")) \
.drop("candle_size") \
.drop("wick_size")
# Define COT reports schema
# {"Timestamp": "2020-01-15 11:29:58", "Asset": {"Asset_long_pos": 304136, "Asset_long_pos_change": 10.0,
# "Asset_long_open_int": 53.6, "Asset_short_pos": 100790, "Asset_short_pos_change": -745.0, "Asset_short_open_int": 17.8},
# "Leveraged": {"Leveraged_long_pos": 57404, "Leveraged_long_pos_change": 1922.0, "Leveraged_long_open_int": 10.1,
# "Leveraged_short_pos": 98263, "Leveraged_short_pos_change": 2377.0, "Leveraged_short_open_int": 17.3}}
schema_cot = types.StructType([types.StructField('Timestamp', types.StringType())])
# Fields in case of currencies and stocks: ['Asset', 'Leveraged']
# In the event of metals, grains, softs: [Managed']
for field in ['Asset', 'Leveraged']:
schema_cot.add(types.StructField(field, types.StructType([
types.StructField('{}_long_pos'.format(field), types.IntegerType()),
types.StructField('{}_long_pos_change'.format(field), types.FloatType()),
types.StructField('{}_long_open_int'.format(field), types.FloatType()),
types.StructField('{}_short_pos'.format(field), types.IntegerType()),
types.StructField('{}_short_pos_change'.format(field), types.FloatType()),
types.StructField('{}_short_open_int'.format(field), types.FloatType())
])))
df_cot = spark \
.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", ", ".join(kafka_config['servers'])) \
.option("subscribe", kafka_config['topics'][2]) \
.option("startingOffsets", "latest") \
.option("failOnDataLoss", "false") \
.load() \
.selectExpr("CAST(value AS STRING)") \
.withColumn("value", F.explode(F.array("value"))) \
.select(F.from_json(F.col("value"), schema_cot).alias("COT")) \
.select("COT.Timestamp", "COT.Asset.*", "COT.Leveraged.*") \
.withColumn("Timestamp_cot", F.to_timestamp(F.col("Timestamp"), "yyyy-MM-dd HH:mm:ss")) \
.drop("Timestamp")
# Round timestamps down to nearest 5 minutes
df_cot = df_cot \
.withColumn("Timestamp_cot_floor", (F.floor(F.unix_timestamp("Timestamp_cot") / (5 * 60)) * 5 * 60).cast("timestamp"))
# Apply watermark
df_cot = df_cot.withWatermark("Timestamp_cot", "5 minutes")
# Define Indicators schema
# {"Timestamp": "2020-02-07 09:54:48", "Nonfarm_Payrolls": {"Actual": 225.0, "Prev_actual_diff": -78.0, "Forc_actual_diff": -65.0},
# "Unemployment_Rate": {"Actual": 3.6, "Prev_actual_diff": -0.10000000000000009, "Forc_actual_diff": -0.10000000000000009}}
schema_ind = types.StructType([types.StructField('Timestamp', types.StringType())])
for field in event_list_repl:
schema_ind.add(types.StructField(field, types.StructType([
types.StructField(ind, types.FloatType()) for ind in event_values
])))
df_ind = spark \
.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", ", ".join(kafka_config['servers'])) \
.option("subscribe", kafka_config['topics'][3]) \
.option("startingOffsets", "latest") \
.option("failOnDataLoss", "false") \
.load() \
.selectExpr("CAST(value AS STRING)") \
.select(F.from_json(F.col("value"), schema_ind).alias("IND")) \
.select("IND.Timestamp", *[F.col("IND.{0}.{1}".format(ind, val)).alias("{0}_{1}".format(ind, val)) \
for val in event_values for ind in event_list_repl]) \
.withColumn("Timestamp_ind", F.to_timestamp(F.col("Timestamp"), "yyyy-MM-dd HH:mm:ss")) \
.drop("Timestamp")
# Round timestamps down to nearest 5 minutes
df_ind = df_ind \
.withColumn("Timestamp_ind_floor", (F.floor(F.unix_timestamp("Timestamp_ind") / (5 * 60)) * 5 * 60).cast("timestamp"))
# Apply watermark
df_ind = df_ind.withWatermark("Timestamp_ind", "5 minutes")
# Define market data schema for IEX DEEP (aggregated size of resting displayed orders at a price and side)
# {'Timestamp': '2020-01-14 10:17:55',
# 'bids_0': {'bid_0': 332.28, 'bid_0_size': 500},
# 'bids_1': {'bid_1': 332.25, 'bid_1_size': 500},
# 'bids_2': {'bid_2': 332.23, 'bid_2_size': 500},
# 'bids_3': {'bid_3': 332.2, 'bid_3_size': 500},
# 'bids_4': {'bid_4': 332.18, 'bid_4_size': 500},
# 'bids_5': {'bid_5': 332.15, 'bid_5_size': 500},
# 'bids_6': {'bid_6': 280.21, 'bid_6_size': 100},
# 'asks_0': {'ask_0': 332.33, 'ask_0_size': 500},
# 'asks_1': {'ask_1': 332.35, 'ask_1_size': 500},
# 'asks_2': {'ask_2': 332.38, 'ask_2_size': 500},
# 'asks_3': {'ask_3': 332.41, 'ask_3_size': 500}}
schema_deep = types.StructType([types.StructField('Timestamp', types.StringType())])
for i in range(bid_levels):
schema_deep.add(types.StructField('bids_{:d}'.format(i), types.StructType([
types.StructField('bid_{:d}'.format(i), types.FloatType()),
types.StructField('bid_{:d}_size'.format(i), types.IntegerType())])))
for i in range(ask_levels):
schema_deep.add(types.StructField('asks_{:d}'.format(i), types.StructType([
types.StructField('ask_{:d}'.format(i), types.FloatType()),
types.StructField('ask_{:d}_size'.format(i), types.IntegerType())])))
df_deep = spark \
.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", ", ".join(kafka_config['servers'])) \
.option("subscribe", kafka_config['topics'][4]) \
.option("startingOffsets", "latest") \
.option("failOnDataLoss", "false") \
.load() \
.selectExpr("CAST(value AS STRING)") \
.select(F.from_json(F.col("value"), schema_deep).alias("DEEP")) \
.select("DEEP.Timestamp", *['DEEP.bids_{0:d}.bid_{0:d}'.format(i) for i in range(bid_levels)] + \
['DEEP.bids_{0:d}.bid_{0:d}_size'.format(i) for i in range(bid_levels)] + \
['DEEP.asks_{0:d}.ask_{0:d}'.format(i) for i in range(ask_levels)] + \
['DEEP.asks_{0:d}.ask_{0:d}_size'.format(i) for i in range(ask_levels)]) \
.withColumn("Timestamp_deep", F.to_timestamp(F.col("Timestamp"), "yyyy-MM-dd HH:mm:ss")) \
.drop("Timestamp")
# Fill missing values
df_deep = df_deep.fillna(0)
# Round timestamps down to nearest 5 minutes
df_deep = df_deep \
.withColumn("Timestamp_deep_floor", (F.floor(F.unix_timestamp("Timestamp_deep") / (5 * 60)) * 5 * 60).cast("timestamp"))
# Apply watermark
df_deep = df_deep.withWatermark("Timestamp_deep", "5 minutes")
# Calculate weighted average for bid's side orders
# LaTex formula:
# \frac{\sum_0^n (price_{0} - price_{n}) \cdot size_{n}}{\sum_0^n size_{n}}
bids_prices = [F.col('bid_{0:d}'.format(i)) for i in range(bid_levels)]
bids_sizes = [F.col('bid_{0:d}_size'.format(i)) for i in range(bid_levels)]
bidsWeightedAverage = sum(F.when(price.isNotNull() & size.isNotNull(), ((F.col("bid_0") - price) * size)).otherwise(0) \
for price, size in zip(bids_prices, bids_sizes)) / sum(F.when(size.isNotNull(), size).otherwise(0) for size in bids_sizes)
df_deep = df_deep \
.withColumn("bids_ord_WA", bidsWeightedAverage)
# Calculate weighted average for ask's side orders
asks_prices = [F.col('ask_{0:d}'.format(i)) for i in range(ask_levels)]
asks_sizes = [F.col('ask_{0:d}_size'.format(i)) for i in range(ask_levels)]
asksWeightedAverage = sum(F.when(price.isNotNull() & size.isNotNull(), ((F.col("ask_0") - price) * size)).otherwise(0) \
for price, size in zip(asks_prices, asks_sizes)) / sum(F.when(size.isNotNull(), size).otherwise(0) for size in asks_sizes)
df_deep = df_deep \
.withColumn("asks_ord_WA", asksWeightedAverage)
# Calculate Order Volume Imbalance
# LaTex formula:
# \frac{V_t^b - V_t^a}{V_t^b + V_t^a}
# where Vtb and Vta denotes the volume at the best bid and at the best ask, respectively
df_deep = df_deep \
.withColumn("vol_imbalance", ((F.col("bid_0_size") - F.col("ask_0_size")) / ((F.col("bid_0_size") + F.col("ask_0_size")))))
# Calculate Delta indicator
# Delta is the difference between the ask and bid traded volume
df_deep = df_deep \
.withColumn("delta", sum(F.when(size.isNotNull(), size).otherwise(0) for size in asks_sizes) - \
sum(F.when(size.isNotNull(), size).otherwise(0) for size in bids_sizes))
# Calculate Micro-Price (according to Gatheral and Oomen)
# LaTex formula:
# I_{t} \cdot P_t^a + (1 - I_{t}) \cdot P_t^b
# where:
# P_t^a, P_t^b - best ask and bid price respectively
# I_{t} = \frac{V_t^b}{V_t^b + V_t^a}
I_t = ((F.col("bid_0_size")) / ((F.col("bid_0_size") + F.col("ask_0_size"))))
df_deep = df_deep \
.withColumn("micro_price", (I_t * F.col("ask_0") + (1 - I_t) * F.col("bid_0")))
# Bid-Ask Spread
df_deep = df_deep \
.withColumn("spread", F.when((F.col("ask_0") != 0) & (F.col("bid_0") != 0), F.col("bid_0") - F.col('ask_0')).otherwise(0))
# Calculate the bid and ask price relative to best values
for i, price in enumerate(asks_prices):
df_deep = df_deep \
.withColumn("ask_{:d}_temp".format(i), F.when(price != 0, F.col("ask_0") - price).otherwise(0))
for i, price in enumerate(bids_prices):
df_deep = df_deep \
.withColumn("bid_{:d}_temp".format(i), F.when(price != 0, F.col("bid_0") - price).otherwise(0))
# Drop old price levels
for i in range(ask_levels):
df_deep = df_deep \
.drop("ask_{:d}".format(i))
for i in range(bid_levels):
df_deep = df_deep \
.drop("bid_{:d}".format(i))
# Rename columns
for i in range(ask_levels):
df_deep = df_deep \
.withColumnRenamed("ask_{:d}_temp".format(i), "ask_{:d}".format(i))
for i in range(ask_levels):
df_deep = df_deep \
.withColumnRenamed("bid_{:d}_temp".format(i), "bid_{:d}".format(i))
# Remove level 0 prices (as F.col("ask_0") - ask_0) always returns 0)
df_deep = df_deep \
.drop("ask_0") \
.drop("bid_0")
# Extract the day of the week as a number ("u")
df_deep = df_deep \
.withColumn("week_day", F.date_format(F.col("Timestamp_deep"), "u").cast("integer"))
# Extract the week of the month as a number ("W")
df_deep = df_deep \
.withColumn("week_of_month", F.date_format(F.col("Timestamp_deep"), "W"))
# Session start (first 2 hours following market opening)
df_deep = df_deep \
.withColumn("Time", F.date_format('Timestamp_deep', 'H:m:s')) \
.withColumn('session_start', F.when((F.split("Time", ":")[0].cast("integer") >= 11) & \
(F.split("Time", ":")[1].cast("integer") >= 30), 0).otherwise(1)) \
.drop("Time")
# Machine Learning Pipelines in current version of Pyspark cannot be fitted into
# Streaming DataFrames (only static DataFrames are supported)
# Therefore, manual oneHotEncoding will be performed
df_deep = df_deep \
.withColumn("day_1", F.when(F.col("week_day") == 1, 1).otherwise(0)) \
.withColumn("day_2", F.when(F.col("week_day") == 2, 1).otherwise(0)) \
.withColumn("day_3", F.when(F.col("week_day") == 3, 1).otherwise(0)) \
.withColumn("day_4", F.when(F.col("week_day") == 4, 1).otherwise(0)) \
.drop("week_day")
df_deep = df_deep \
.withColumn("week_1", F.when(F.col("week_of_month") == 1, 1).otherwise(0)) \
.withColumn("week_2", F.when(F.col("week_of_month") == 2, 1).otherwise(0)) \
.withColumn("week_3", F.when(F.col("week_of_month") == 3, 1).otherwise(0)) \
.withColumn("week_4", F.when(F.col("week_of_month") == 4, 1).otherwise(0)) \
.drop("week_of_month")
# Join streaming DataFrames
df_joined = df_deep.select("*")
if get_vix:
df_joined = df_joined \
.join(df_vix, F.expr("""
(Timestamp_deep_floor = Timestamp_vix_floor AND
Timestamp_vix >= Timestamp_deep AND
Timestamp_vix <= Timestamp_deep + interval 3 minutes)
""")) \
.drop("Timestamp_vix") \
.drop("Timestamp_vix_floor")
if get_stock_volume:
df_joined = df_joined \
.join(df_volume, F.expr("""
(Timestamp_deep_floor = Timestamp_vol_floor AND
Timestamp_vol >= Timestamp_deep AND
Timestamp_vol <= Timestamp_deep + interval 3 minutes)
""")) \
.drop("Timestamp_vol") \
.drop("Timestamp_vol_floor")
if get_cot:
df_joined = df_joined \
.join(df_cot, F.expr("""
(Timestamp_deep_floor = Timestamp_cot_floor AND
Timestamp_cot >= Timestamp_deep AND
Timestamp_cot <= Timestamp_deep + interval 3 minutes)
""")) \
.drop("Timestamp_cot") \
.drop("Timestamp_cot_floor")
df_joined = df_joined \
.join(df_ind, F.expr("""
(Timestamp_deep_floor = Timestamp_ind_floor AND
Timestamp_ind >= Timestamp_deep AND
Timestamp_ind <= Timestamp_deep + interval 3 minutes)
""")) \
.drop("Timestamp_ind") \
.drop("Timestamp_ind_floor") \
.drop("Timestamp_deep_floor") \
.withColumnRenamed("Timestamp_deep", "Timestamp") \
.dropDuplicates()
# Fill missing values
df_joined = df_joined.fillna(0)
# Write stream to MySQL/MariaDB
df_joined \
.writeStream \
.outputMode('append') \
.foreachBatch(write_stream_to_mysql) \
.start() \
# .awaitTermination()
# Extract current Timestamp and send it to kafka 'predict_timestamp' topic to signal
# to Pytorch model readiness to make a prediction for current datapoint.
df_kafka_signal = df_joined \
.select(F.to_json(F.struct("Timestamp")).alias("value")) \
df_kafka_signal \
.writeStream \
.format("kafka") \
.option("kafka.bootstrap.servers", ", ".join(kafka_config['servers'])) \
.option("topic", kafka_config['topics'][5]) \
.option("checkpointLocation", "checkpoint") \
.start() \
.awaitTermination()
# Write stream to console (debug purposes)
# df_deep.printSchema()
# df_deep.writeStream.outputMode("append").option("truncate", False).format("console").start()#.awaitTermination()