Handle late readings and enrich text without changing its grain
A sensor sends event E17 at 09:59. The network delivers it at 10:03 and again at 10:04. Operators want the hourly count, plus a category for each maintenance note. How should the pipeline treat the copies?
Handle late readings and enrich text without changing its grain
An event has a business identifier, an event timestamp and a processing timestamp. They answer different questions. A watermark indicates estimated progress through event time. Late corrections and deduplication require an explicit policy both in the transform and at the final sink.
In the recording
Classify riders’ free-text comments with a Gemini-backed BigQuery function and review a sample before running it across the table.
Work through the decision
A sensor sends event E17 at 09:59. The network delivers it at 10:03 and again at 10:04. Operators want the hourly count, plus a category for each maintenance note. How should the pipeline treat the copies?
Check your reasoning
Assign the reading to its event-time interval. Declare how long the window accepts corrections and deduplicate by a durable event ID. Enrich each accepted note separately and inspect null, unexpected and low-quality classifications before downstream use.
Decisions and tradeoffs
- Event-time window
- Group records by when the measurement happened. A watermark estimates event-time progress and an allowed-lateness policy bounds subsequent updates.
- Keyed deduplication
- Use the stable event identifier within an appropriate retention horizon so repeated delivery contributes once.
- Batch transformation
- Run a bounded computation when the full input is available. Use a managed Spark job for existing Spark code or SQL for warehouse-native transformations.
- AI enrichment
- Evaluate model-derived labels on a reviewed sample, constrain the output schema and budget for inference before scaling to the whole table.
Decide where the transformation should run
Pub/Sub and Kafka provide message intake and delivery. A Beam pipeline on Dataflow can apply event-time windows and state to a stream. An existing Spark job can process bounded inputs, and a warehouse SQL transformation suits data already loaded into BigQuery. A managed integration pipeline can connect disparate sources. Include retry semantics, the volume of late events and the ability to investigate individual rejections in the decision.
Practice the guide skills
- Validate required fields and route invalid inputs with reasons.
- Match Pub/Sub, Kafka, Beam/Dataflow, Spark or SQL to ordering, latency and existing code.
- Compare a bounded recompute with a live window.
- Test an event just inside and outside the lateness policy.
- Run a repeat delivery through the transformation logic.
- Evaluate generated labels with a held-out reviewed sample.
- Check the initial import against a manifest.
- Test schema compatibility before admitting a second producer.
Official objectives for this lesson
Count events in their business hour
Save the script as stream.py and run it with Python 3. Before running it, predict the two hourly totals. The operator permits ten minutes of lateness after the hour closes. Times are minutes since midnight and identifiers are unique for the day.
from collections import Counter
# Event ID, event minute, arrival minute, units
deliveries = [("a", 59, 61, 4), ("a", 59, 62, 4),
("b", 58, 69, 3), ("c", 57, 71, 8),
("d", 62, 72, 2)]
totals = Counter()
seen = set()
late = []
for key, event_minute, arrival_minute, units in deliveries:
hour = event_minute // 60
deadline = (hour + 1) * 60 + 10
if key in seen:
continue
if arrival_minute > deadline:
late.append(key)
continue
seen.add(key)
totals[hour] += units
print(sorted(totals.items()))
print("late:", late)
Check the result
[(0, 7), (1, 2)] late: ['c']
The second delivery of a adds nothing. Both a and b belong to hour zero. Move c’s arrival from 71 to 70 and run it again. The first total becomes 15 and the late list is empty. This fixture uses a fixed deadline to isolate the business rule. A streaming runner uses watermarks, triggers and sink update semantics to implement its chosen policy.
Related lesson · Move and clean source data under real transfer constraints
Handle late readings and enrich text without changing its grain quiz
Answer 3 questions. Check each answer for an explanation.
Worked example · Handle late readings and enrich text without changing its grain