QuestionQ172

VerifiedStructured Streaming

A data engineer needs to process a streaming DataFrame that receives sensor readings every second, with the columns sensor_id, temperature, and timestamp. The engineer must calculate each sensor’s average temperature over the previous 5 minutes while the data continues streaming.

Which code implementation meets this requirement?

Explanation

A Structured Streaming event-time aggregation is expressed by grouping on the sensor identifier and a time-window column, then applying avg to the temperature values. withWatermark("timestamp", "5 minutes") defines event-time watermarking for the streaming DataFrame, and window("timestamp", "5 minutes") creates the five-minute aggregation windows. Grouping by both values produces one average temperature per sensor for each window.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!