-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextra_spark_stream.py
More file actions
165 lines (141 loc) · 6.2 KB
/
Copy pathextra_spark_stream.py
File metadata and controls
165 lines (141 loc) · 6.2 KB
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
# fixed_spark_stream.py
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, from_json, explode, split, current_timestamp
from pyspark.sql.types import StructType, StructField, StringType, IntegerType, FloatType, ArrayType
# Create Spark Session with better configurations
spark = SparkSession.builder \
.appName("AirlineSentimentStream") \
.config("spark.jars.packages", "org.apache.spark:spark-sql-kafka-0-10_2.12:3.3.0") \
.config("spark.jars", "/home/siri/Downloads/postgresql-42.2.5.jar") \
.config("spark.executor.memory", "2g") \
.config("spark.driver.memory", "2g") \
.config("spark.sql.shuffle.partitions", "10") \
.config("spark.default.parallelism", "10") \
.config("spark.streaming.stopGracefullyOnShutdown", "true") \
.getOrCreate()
# Enable more detailed logging
spark.sparkContext.setLogLevel("WARN")
# Define schema based on your Kafka data format
schema = StructType([
StructField("tweet_id", StringType()),
StructField("airline_sentiment", StringType()),
StructField("airline_sentiment_confidence", FloatType()),
StructField("negativereason", StringType()),
StructField("airline", StringType()),
StructField("username", StringType()),
StructField("tweet_text", StringType()),
StructField("created_at", StringType()),
StructField("retweet_count", IntegerType()),
StructField("tweet_location", StringType()),
StructField("user_timezone", StringType()),
StructField("hashtags", ArrayType(StringType()))
])
# Read from Kafka topic with better error handling
try:
df = spark.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", "localhost:9092") \
.option("subscribe", "dbtech_airline_sentiment") \
.option("startingOffsets", "earliest") \
.option("failOnDataLoss", "false") \
.option("maxOffsetsPerTrigger", 1000) \
.load()
print("Successfully connected to Kafka stream")
# Parse JSON to DataFrame
parsed_df = df.selectExpr("CAST(value AS STRING) as json") \
.select(from_json(col("json"), schema).alias("data")) \
.select("data.*") \
.withColumn("processing_time", current_timestamp())
# ---- HASHTAG PROCESSING ----
# Extract and count hashtags
hashtag_df = parsed_df.select(
explode(col("hashtags")).alias("hashtag")
).filter(col("hashtag").isNotNull())
hashtag_counts = hashtag_df.groupBy("hashtag").count()
# Write hashtag results to console for monitoring
query1 = hashtag_counts.writeStream \
.outputMode("complete") \
.format("console") \
.option("truncate", False) \
.option("numRows", 10) \
.trigger(processingTime="10 seconds") \
.start()
# ---- POSTGRESQL BATCH SAVE FUNCTION ----
def save_to_postgres(batch_df, batch_id, table_name):
if batch_df.count() > 0:
try:
# Overwrite mode for hashtags as we're updating counts
mode = "overwrite" if table_name == "hashtags" else "append"
batch_df.write \
.format("jdbc") \
.option("url", "jdbc:postgresql://localhost:5432/twitter_data") \
.option("dbtable", table_name) \
.option("user", "postgres") \
.option("password", "<enter your password>") \
.option("driver", "org.postgresql.Driver") \
.mode(mode) \
.save()
print(f"Successfully wrote batch {batch_id} to {table_name}")
except Exception as e:
print(f"Error writing to PostgreSQL: {e}")
# Write hashtags to Postgres
query2 = hashtag_counts.writeStream \
.outputMode("complete") \
.foreachBatch(lambda batch_df, batch_id: save_to_postgres(batch_df, batch_id, "hashtags")) \
.trigger(processingTime="30 seconds") \
.start()
# ---- SENTIMENT ANALYSIS ----
# Count by sentiment
sentiment_counts = parsed_df.groupBy("airline_sentiment").count()
# Write sentiment counts to console
query3 = sentiment_counts.writeStream \
.outputMode("complete") \
.format("console") \
.option("truncate", False) \
.trigger(processingTime="10 seconds") \
.start()
# Write sentiment counts to Postgres
query4 = sentiment_counts.writeStream \
.outputMode("complete") \
.foreachBatch(lambda batch_df, batch_id: save_to_postgres(batch_df, batch_id, "sentiment_counts")) \
.trigger(processingTime="30 seconds") \
.start()
# ---- AIRLINE PERFORMANCE ----
# Analyze sentiment by airline
airline_sentiment = parsed_df.groupBy("airline", "airline_sentiment").count()
# Write airline sentiment to console
query5 = airline_sentiment.writeStream \
.outputMode("complete") \
.format("console") \
.option("truncate", False) \
.trigger(processingTime="10 seconds") \
.start()
# Write airline sentiment to Postgres
query6 = airline_sentiment.writeStream \
.outputMode("complete") \
.foreachBatch(lambda batch_df, batch_id: save_to_postgres(batch_df, batch_id, "airline_sentiment")) \
.trigger(processingTime="30 seconds") \
.start()
# ---- SAVE RAW TWEETS ----
# Write raw tweets to Postgres (limited to key fields to reduce data size)
tweet_df = parsed_df.select(
"tweet_id", "airline", "airline_sentiment", "tweet_text",
"created_at", "retweet_count", "processing_time"
)
query7 = tweet_df.writeStream \
.outputMode("append") \
.foreachBatch(lambda batch_df, batch_id: save_to_postgres(batch_df, batch_id, "tweets")) \
.trigger(processingTime="30 seconds") \
.start()
print("All streams started. Waiting for termination...")
# Wait for termination
spark.streams.awaitAnyTermination()
except Exception as e:
print(f"Error in Spark streaming: {e}")
import traceback
traceback.print_exc()
finally:
# Stop Spark session gracefully
if 'spark' in locals():
spark.stop()
print("Spark session stopped.")