-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspark_stream.py
126 lines (103 loc) · 3.82 KB
/
spark_stream.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
import pyspark,sys
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType,StructField,IntegerType,StringType,DoubleType
from pyspark.sql.functions import col,explode,split,expr,element_at
from json import loads
schema = StructType([
StructField("Year", IntegerType(), True),
StructField("Month", IntegerType(), True),
StructField("DayofMonth", IntegerType(), True),
StructField("DayOfWeek", IntegerType(), True),
StructField("DepTime", IntegerType(), True),
StructField("CRSDepTime", IntegerType(), True),
StructField("ArrTime", IntegerType(), True),
StructField("CRSArrTime", IntegerType(), True),
StructField("UniqueCarrier", StringType(), True),
StructField("FlightNum", IntegerType(), True),
StructField("TailNum", StringType(), True),
StructField("ActualElapsedTime", IntegerType(), True),
StructField("CRSElapsedTime", IntegerType(), True),
StructField("AirTime", IntegerType(), True),
StructField("ArrDelay", IntegerType(), True),
StructField("DepDelay", IntegerType(), True),
StructField("Origin", StringType(), True),
StructField("Dest", StringType(), True),
StructField("Distance", IntegerType(), True),
StructField("TaxiIn", IntegerType(), True),
StructField("TaxiOut", IntegerType(), True),
StructField("Cancelled", IntegerType(), True),
StructField("CancellationCode", StringType(), True,metadata={"nullValue": "NA"}),
StructField("Diverted", IntegerType(), True,metadata={"nullValue": "NA"}),
StructField("CarrierDelay", IntegerType(), True,metadata={"nullValue": "NA"}),
StructField("WeatherDelay", IntegerType(), True,metadata={"nullValue": "NA"}),
StructField("NASDelay", IntegerType(), True,metadata={"nullValue": "NA"}),
StructField("SecurityDelay", IntegerType(), True,metadata={"nullValue": "NA"}),
StructField("LateAircraftDelay", IntegerType(), True, metadata={"nullValue": "NA"})
])
spark = SparkSession\
.builder\
.appName("OTPMS")\
.config("spark.jars.packages","org.apache.spark:spark-sql-kafka-0-10_2.12:3.2.0")\
.getOrCreate()
Topic = "airport"
df = spark \
.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", "localhost:9092") \
.option("subscribe", Topic) \
.load()
'''
#.selectExpr("CAST(value AS STRING)")
k = 0
query = df.selectExpr("CAST(value as STRING)")\
.toDF("Potato")\
.select(split(col("Potato"), ",").getItem(14).cast("integer").alias("Delay")) \
.writeStream\
.outputMode("append")\
.format("console")\
.start()\
'''
query = df.selectExpr("CAST(value as STRING)")\
.toDF("Potato")\
.select(split(col("Potato"), ",").getItem(14).cast("integer").alias("Delay"))
sum = 0
'''
query = query.select(split(col("value"), ",").getItem(9).cast("double").alias("col_10")) \
.agg(sum(col("col_10")).alias("sum_col_10")) \
.writeStream \
.outputMode("complete") \
.format("console") \
.start()
'''
#fun = df.select(split(col("value"), ",").getItem(1).cast("integer").alias("year"))
'''
fun = df.selectExpr("CAST(value as STRING)")\
.select(element_at("value", 1)).show()
query = fun.writeStream\
.outputMode("append")\
.format("console")\
.start()
'''
#fun.awaitTermination()
#query.awaitTermination()
#record = df.selectExpr("CAST(value AS STRING)")
#record_obj = record.select(split(col("value"), ",").getItem(9).cast("double").alias("col_10"))
#record_obj.show()
'''
q = df.select("value") \
.writeStream \
.outputMode("append")\
.format("console") \
.start()
q.awaitTermination()
'''
#Does not work
'''
query = df.selectExpr("CAST(value AS STRING)") \
.select(split(col("value"), ",").getItem(9).cast("double").alias("col_10")) \
.agg(sum(col("col_10")).alias("sum_col_10")) \
.writeStream \
.outputMode("complete") \
.format("console") \
.start()
'''