Event Streaming LabPython · Bytewax

Chapter 20 · Real Bytewax

Enrich a stream

A small lookup changes the meaning of a record.

Prerequisite From streams to tables and back.

What you’ll learn

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.

Enrichment looks up a second source of meaning
  1. OrderJoin keySelect which reference entity is needed.
  2. Reference dataRegion lookupThe fixture has a bounded reference map.
  3. 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.

Ctrl+Enter or ⌘+Enter runs the code.

Edits are saved in this browser when storage is available.

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.

Return to the editor
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.

QuestionMechanism and boundary
WorkOne dictionary lookup and output construction per event; expected O(1) lookup under ordinary hashing assumptions.
StateO(k) reference entries; the collector also retains n output records.
MovementA local dictionary avoids remote round trips. A remote lookup has separate latency, quotas and caching behavior.
ProgressCan emit when the lookup completes; an asynchronous remote lookup may reorder or buffer records.
RecoveryA 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
  1. Hint 1

    Ask what question the report answers.

  2. Hint 2

    A current lookup and a historical lookup answer different questions.

  3. 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 →
Reading marks track what you’ve read independently of your runs.