-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoin_stream_data.py
More file actions
45 lines (37 loc) · 1.54 KB
/
Copy pathjoin_stream_data.py
File metadata and controls
45 lines (37 loc) · 1.54 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
from pyspark.sql import SparkSession
from pyspark.sql.functions import from_json, col
from pyspark.sql.types import StructType, StructField, StringType, TimestampType
# Create a SparkSession
spark = SparkSession.builder \
.appName("JoinStreamingAndStatic") \
.master("local[3]") \
.config("spark.sql.shuffle.partitions", "2") \
.config("spark.streaming.stopGracefullyOnShutdown", "true") \
.config("spark.jars.packages", "org.apache.spark:spark-sql-kafka-0-10_2.12:3.0.0") \
.getOrCreate()
# Read the stream from Kafka
df = spark.readStream.format("kafka") \
.option("kafka.bootstrap.servers", "localhost:9092") \
.option("subscribe", "fruit_data") \
.option("startingOffsets", "latest") \
.load()
# Define the schema of the JSON data
schema = StructType([
StructField("id", StringType()),
StructField("value", StringType()),
StructField("timestamp", TimestampType())
])
# Parse the JSON data and select the fields
df = df.select(from_json(col("value").cast("string"), schema).alias("data")).select("data.*")
# Read the static dataset from hdfs path
df_static = spark.read.csv("/tmp/input_data/fruit_dim.csv", inferSchema=True, header=True)
# Join the streaming data with the static dataset
df_joined = df.join(df_static, df.id == df_static.id,'inner').drop(df_static.id)
# Write the output to the console in append mode
query = df_joined.writeStream \
.outputMode("append") \
.format("console") \
.option("truncate", "false") \
.start()
# Wait for the query to terminate
query.awaitTermination()