Chapter 20 · Real Bytewax
Enrich a stream
A small lookup changes the meaning of a record.
Prerequisite From streams to tables and back.
Explain stream-table enrichment and choose a policy for missing keys.
The question
A regional dashboard needs the region of each shop. Order records carry a shop name, so each order must look up a second piece of information.
Enrichment adds context to an event. A stream-table lookup uses a current table to enrich each arriving record. This differs from a stream-stream join, which must decide how long to wait for a matching event and how long to retain unmatched state.
- OrderJoin keySelect which reference entity is needed.
- Reference dataRegion lookupThe fixture has a bounded reference map.
- Enriched recordOrder + regionA missing lookup follows an explicit policy.
Conceptual diagram. A current lookup is different from historical as-of semantics. Inspect the fixture before interpreting a result as historical truth.
Predict the result
The editor prepares a one-entry dictionary for North and imports all five orders, including two from South.
The lookup contains only North. Should South disappear, fail the pipeline, or keep an explicit unknown region?
Run the experiment
The editor contains the published example. Select Run with the code unchanged.
Python prepares in the background while you read. First use can download about 84 MiB. Your code runs only when you select Run; each run starts with clean experiment state.
A saved draft is open. Select Restore code to see the published example. Recover draft brings back your draft.
Ctrl+Enter or ⌘+Enter runs the code.
Edits are saved in this browser when storage is available.
Technical error details
Your result
Console output
Troubleshooting
If Python stops responding, restart the environment and try again. Your code and last completed result are kept.
View the published result
The course build ran the published code to generate this output.
[
{
"id": "a",
"shop": "North",
"region": "Yorkshire"
},
{
"id": "b",
"shop": "South",
"region": "unknown"
},
{
"id": "c",
"shop": "North",
"region": "Yorkshire"
},
{
"id": "d",
"shop": "North",
"region": "Yorkshire"
},
{
"id": "e",
"shop": "South",
"region": "unknown"
}
]Read the result
Find the output rows whose shop is South and read their region. The code’s fallback puts unknown on those rows. Compare that result with a missing key in the dictionary.
Change the experiment
Replace regions = {'North': 'Yorkshire'} with regions = {'North': 'Yorkshire', 'South': 'London'} in the editor. Select Run again. Check the region in the South rows. Next select Restore code, then replace regions.get(order['shop'], 'unknown') with regions[order['shop']]. Select Run. Read Execution error, then confirm the console still shows the prior successful result.
Runtime, source code and downloads
Runtime and reset details explain what stays ready and what is isolated on each run. Download Python packages the published helpers with the code in your editor.
Browse the sources for Bytewax windows and authored models, Kafka log and group exercises, live Kafka input, checkpoint recovery and processing patterns.
A missing lookup is evidence
An unknown region might mean absent reference data, a mismatched key or an out-of-order reference update. Repeatedly retrying a permanent key mismatch wastes work and can block progress. Preserve the original event and enough reference-version provenance to investigate rather than silently filtering it away.
Work, state and recovery costs
Where used, n counts input events, k counts retained distinct keys, r counts overlapping windows per event and z counts output matches. Bounds assume the stated algorithm. Collecting, sorting and displaying results adds work.
| Question | Mechanism and boundary |
|---|---|
| Work | One dictionary lookup and output construction per event; expected O(1) lookup under ordinary hashing assumptions. |
| State | O(k) reference entries; the collector also retains n output records. |
| Movement | A local dictionary avoids remote round trips. A remote lookup has separate latency, quotas and caching behavior. |
| Progress | Can emit when the lookup completes; an asynchronous remote lookup may reorder or buffer records. |
| Recovery | A mutable latest lookup can change replayed answers even with identical source events. |
Think it through
If a shop changes region tomorrow, should replaying yesterday’s orders use the old region or the new one?
Show hints
Hint 1
Ask what question the report answers.
Hint 2
A current lookup and a historical lookup answer different questions.
Hint 3
A temporal lookup needs version history and a time rule.
Show explanation
It depends on the report’s contract. Current-organisation reporting may use the latest mapping. Historical reporting needs the mapping valid at the event’s time. A plain dictionary has no version history, so it cannot make that decision for you.
ReferencesFunction documentation and helper source
References open in a new tab; course helpers are documented separately from library APIs. Bytewax links target the official 0.21.1 definitions and API docstrings.
Look up a field in the mapper
The mapper performs a Python dictionary lookup. This is a current local lookup, not a versioned temporal join.
Graph construction and input interfaces
Read these signatures for the graph and finite test input constructed in the editor. The course collection helper attaches an output and starts execution.
Course helper implementation
These are functions written for this course, not additional library APIs. Inspect their parameters, operator calls and authored policies. The source shown is generated from the same Python files packaged with Run and Download Python.
Experiment scope
This is an in-memory stream-table lookup, not a distributed or temporal join.
Primary reference →