Ingesting and processing the data · Topic 3 of 4
Streaming, Windowing, Late Data and AI Enrichment
Dataflow streaming: event time, watermarks, fixed, sliding and session windows, late data and triggers, processing logic, AI enrichment with RunInference.
- 5 min
- Use event time for business results; the watermark tracks event-time progress.
- Fixed for intervals, sliding for rolling metrics, session for activity bursts.
- Allowed lateness plus late firings update results; later data is dropped.
What it is
A streaming pipeline processes an unbounded flow of events as they arrive. Because events can arrive out of order or late, it groups them into windows by when they happened and uses watermarks and triggers to decide when a result is ready. The same pipeline can enrich each event with reference data or a machine learning prediction.
How it works
Event time, processing time and watermarks
- Event time is when the event happened, carried in the data.
- Processing time is when the pipeline sees it.
- The watermark is the pipeline's estimate that all events up to a given event time have arrived.
Business metrics almost always need event time. Counting by processing time puts an offline phone's morning events into the afternoon.
Window types
| Window | Shape | Example |
|---|---|---|
| Fixed (tumbling) | Equal, non-overlapping intervals | Orders per minute |
| Sliding (hopping) | Fixed size, starting every period, so they overlap | 5-minute average updated every minute |
| Session | Per key, closed after a gap of inactivity | User visits ending after 30 minutes idle |
| Global | One window for all data | Batch-style totals with custom triggers |
Late arriving data and triggers
| Setting | Meaning |
|---|---|
| Default trigger | Emit once when the watermark passes the end of the window |
| Early firings | Speculative results before the watermark, for fresh dashboards |
| Allowed lateness | How long after the watermark late events are still accepted |
| Late firings | Updated results when late events arrive inside allowed lateness |
| Accumulating mode | Each firing contains the full result so far |
| Discarding mode | Each firing contains only new data since the last firing |
Events later than the allowed lateness are dropped. If late data must never be lost, also write the raw events to a sink you can reprocess.
import json
import apache_beam as beam
from apache_beam import window
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.transforms.trigger import (
AccumulationMode, AfterCount, AfterProcessingTime, AfterWatermark)
with beam.Pipeline(options=PipelineOptions(streaming=True)) as p:
(p
| beam.io.ReadFromPubSub(
subscription="projects/prod-data/subscriptions/clicks-sub",
timestamp_attribute="event_ts") # use event time
| beam.Map(lambda msg: (json.loads(msg)["page"], 1))
| beam.WindowInto(
window.FixedWindows(60),
trigger=AfterWatermark(early=AfterProcessingTime(10), late=AfterCount(1)),
allowed_lateness=600, # accept events up to 10 minutes late
accumulation_mode=AccumulationMode.ACCUMULATING)
| beam.CombinePerKey(sum)
| beam.Map(lambda kv: {"page": kv[0], "views": kv[1]})
| beam.io.WriteToBigQuery("prod-data:web.page_views",
schema="page:STRING,views:INTEGER"))
Other window functions: window.SlidingWindows(size=300, period=60) and window.Sessions(gap_size=1800).
Processing logic
| Need | Beam approach |
|---|---|
| Per-element change | Map, FlatMap, ParDo |
| Aggregate per key | CombinePerKey (more efficient than GroupByKey then summing) |
| Join two streams or datasets | CoGroupByKey on a shared key within windows |
| Small reference data | Side input loaded once per window or refreshed periodically |
| Per-key memory across events | Stateful DoFn with timers |
Dataflow streaming provides exactly-once processing by default and removes Pub/Sub redeliveries of the same message. A publisher that retries can still send one event twice with different message IDs, so deduplicate on a unique event ID. Pub/Sub ordering keys keep messages with the same key in order.
AI data enrichment
| Approach | Use it when |
|---|---|
| Beam RunInference | Run a model inside the pipeline on each element or batch |
| Beam Enrichment transform | Look up extra fields, for example from Bigtable, per element |
Call an AI API from a DoFn | Translation, entity extraction or Gemini prompts, batched to respect quotas |
BigQuery ML functions such as ML.GENERATE_TEXT | Enrichment can wait until data is in BigQuery |
If the stem needs predictions within seconds of an event, enrich in the stream. If hourly results are fine, enrich in BigQuery with SQL; it is simpler and easier to rerun.
A worked example
A game studio counts scores per player per minute. Phones lose signal and send events up to 10 minutes late, and the leaderboard must also tag abusive chat messages.
The engineer reads Pub/Sub with the event timestamp attribute and uses fixed 1-minute windows with 10 minutes of allowed lateness and accumulating late firings. Chat messages pass through RunInference with a classification model, and flagged messages go to a moderation topic.
How the exam asks about this
- "Devices buffer events offline; reports must count each event in the minute it occurred." Event-time fixed windows using the event timestamp, with allowed lateness for late data.
- "Group clicks into visits that end after 30 minutes without activity." Session windows with a 30-minute gap.
- "Show a rolling 5-minute average refreshed every minute." Sliding windows of 5 minutes with a 1-minute period.
What to remember for the exam
- Use event time for business results; the watermark tracks event-time progress.
- Fixed for intervals, sliding for rolling metrics, session for activity bursts.
- Allowed lateness plus late firings update results; later data is dropped.
- Side inputs for small lookups; CombinePerKey for efficient aggregation.
- RunInference and Enrichment add AI and lookups inside Beam pipelines.
Processing-time windows are simpler but give wrong business numbers when events arrive late or out of order. Choose them only when the stem says arrival time is what matters.
Common mistakes
Ignoring allowed lateness. With the default of zero, late events are dropped silently.
Grouping a stream in the global window with the default trigger. The window never closes, so Beam rejects the pipeline; set a window or a trigger.
Calling an external API for every single element. Batch requests and respect quotas, or enrich later in BigQuery.
Check yourself
Which window type suits user visits separated by inactivity?
Session windows, with the gap set to the inactivity period.
What happens to an event that arrives after the allowed lateness?
It is dropped from the windowed result, so keep a raw copy if every event must be reprocessable.
Which Beam transform runs a machine learning model inside a pipeline?
RunInference.
Topic 3 of 4 in Ingesting and processing the data
Test yourself on Ingesting and processing the data
The full Google Cloud Professional Data Engineer question bank has 733 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Google Cloud. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.