Windowing & Streaming⏱️ ~22 mins
24. Sliding Window Moving Average
Enterprise Architecture Context
In production stream-processing architectures (Google Cloud Dataflow / Flink), pipeline stages must handle parallel transformations without data loss, managing schema mutations and aggregations across distributed worker workers.
Problem Statement
### Business Context
Real-time stock price analytics compute moving averages over overlapping 10-second windows evaluated every 5 seconds to smooth price volatility.
### Problem Statement
Write a function `sliding_average(input_pcoll)` that partitions events into sliding windows of **size 10 seconds** and **period 5 seconds** using `beam.WindowInto(SlidingWindows(10, 5))`.
Key Learning Objectives
- Understand distributed Apache Beam execution DAG stages and pipeline lifecycle.
- Apply idiomatic functional Python transforms using the pipe operator
|. - Ensure data consistency and idempotency across distributed stream workers.
Sample Data Fixtures
Sample Example 1
Input Stream:
[10, 20]
Expected Output:
[10, 10, 20, 20]
Sample Example 2
Input Stream:
[5]
Expected Output:
[5, 5]
Topics:#Windowing#Streaming
solution.pyPython 3.11 (Apache Beam)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input PCollection2 elements
| # | Element / Payload |
|---|---|
| 1 | 10 |
| 2 | 20 |
Expected Output PCollection4 elements
| # | Output Element |
|---|---|
| 1 | 10 |
| 2 | 10 |
| 3 | 20 |
| 4 | 20 |
Windowing & Streaming⏱️ ~22 mins
24. Sliding Window Moving Average
Enterprise Architecture Context
In production stream-processing architectures (Google Cloud Dataflow / Flink), pipeline stages must handle parallel transformations without data loss, managing schema mutations and aggregations across distributed worker workers.
Problem Statement
### Business Context
Real-time stock price analytics compute moving averages over overlapping 10-second windows evaluated every 5 seconds to smooth price volatility.
### Problem Statement
Write a function `sliding_average(input_pcoll)` that partitions events into sliding windows of **size 10 seconds** and **period 5 seconds** using `beam.WindowInto(SlidingWindows(10, 5))`.
Key Learning Objectives
- Understand distributed Apache Beam execution DAG stages and pipeline lifecycle.
- Apply idiomatic functional Python transforms using the pipe operator
|. - Ensure data consistency and idempotency across distributed stream workers.
Sample Data Fixtures
Sample Example 1
Input Stream:
[10, 20]
Expected Output:
[10, 10, 20, 20]
Sample Example 2
Input Stream:
[5]
Expected Output:
[5, 5]
Topics:#Windowing#Streaming