-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspark_analytics.py
More file actions
60 lines (49 loc) · 1.86 KB
/
Copy pathspark_analytics.py
File metadata and controls
60 lines (49 loc) · 1.86 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
from pyspark.sql import SparkSession
from pyspark.sql.functions import from_json, col, to_timestamp
from pyspark.sql.types import StructType, StructField, StringType
spark = SparkSession.builder \
.appName("EcommerceRealTimeAnalytics") \
.config("spark.sql.shuffle.partitions", "2") \
.getOrCreate()
# 1. Schéma JSON
schema = StructType([
StructField("type", StringType(), True),
StructField("guestId", StringType(), True),
StructField("timestamp", StringType(), True),
StructField("url", StringType(), True),
StructField("payload", StringType(), True)
])
kafka_ip = "10.132.0.3"
# 2. Lecture du flux Kafka
df = spark.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", f"{kafka_ip}:9092") \
.option("subscribe", "web-events") \
.option("startingOffsets", "latest") \
.load()
# 3. Parsing et typage du timestamp
events = df.selectExpr("CAST(value AS STRING)") \
.select(from_json(col("value"), schema).alias("data")) \
.select("data.*") \
.withColumn("event_time", to_timestamp(col("timestamp")))
# --- CONFIGURATION DU DATA LAKE (GCS) ---
# def bucket créé sur GCP
bucket_name = "tp-bigdata-datalake"
checkpoint_path = f"gs://{bucket_name}/checkpoints/web_events"
storage_path = f"gs://{bucket_name}/data/web_events"
# 4. Écriture dans le Data Lake au format Parquet
# Le checkpoint est OBLIGATOIRE pour le streaming (évite de perdre le fil si crash)
query_storage = events.writeStream \
.format("parquet") \
.option("path", storage_path) \
.option("checkpointLocation", checkpoint_path) \
.partitionBy("type") \
.start()
# 5. Affichage console
query_console = events.writeStream \
.outputMode("append") \
.format("console") \
.start()
print(f">>> Analyseur Spark actif. Stockage vers gs://{bucket_name}...")
# Attente des deux flux
spark.streams.awaitAnyTermination()