-
Notifications
You must be signed in to change notification settings - Fork 3
/
consumer_park_cr.py
46 lines (39 loc) · 1.4 KB
/
consumer_park_cr.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
from pyspark.sql import SparkSession
from pyspark.sql.functions import from_json, col, avg, count
from pyspark.sql.types import StructType, StringType, DoubleType, LongType
# Crear la sesión de Spark
spark = SparkSession.builder \
.appName("KafkaSparkStreaming") \
.getOrCreate()
# Configurar el esquema de los datos recibidos
schema = StructType() \
.add("sensor_id", StringType()) \
.add("temperature", DoubleType()) \
.add("humidity", DoubleType()) \
.add("timestamp", LongType())
# Leer los datos desde Kafka
df_kafka = spark.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", "localhost:9092") \
.option("subscribe", "tarea3") \
.option("startingOffsets", "earliest") \
.load()
# Convertir los datos en formato JSON al esquema definido
df_kafka_value = df_kafka.selectExpr("CAST(value AS STRING) as json") \
.select(from_json(col("json"), schema).alias("data")) \
.select("data.*")
# Realizar el análisis: contar eventos y calcular el promedio de temperatura y humedad
df_agg = df_kafka_value \
.groupBy() \
.agg(
count("*").alias("event_count"),
avg("temperature").alias("avg_temperature"),
avg("humidity").alias("avg_humidity")
)
# Mostrar los resultados en la consola
query = df_agg.writeStream \
.outputMode("complete") \
.format("console") \
.start()
#Ver los datos
df_kafka_value.printSchema()