Change the Schema During a Run
Add, rename and retype a field at known times, and write a label record for each change: an order gains a currency field at 60 seconds, total becomes amount at 120 seconds, and the amount becomes text at 180 seconds.
Before You Start
-
The commands run from the root of a clone of the repository, where the files are in
docs/snippets/generate-data/. The Full Files block of this page holds the same files, for a reader who saves them by hand. -
First Simulation explains arrivals, tasks and the records a run writes.
- Live Parameters explains scenario steps and label records.
Steps
Describe the Arrival
An order arrives every 10 seconds.
| docs/snippets/generate-data/change_the_schema_during_a_run_builder.py:arrival | |
|---|---|
| docs/snippets/generate-data/change_the_schema_during_a_run_low_level.py:arrival | |
|---|---|
Sampler(seed=42) supplies every draw: a random generator for each payload field and for the gap. The stream line names the random sequence the blueprint uses for this arrival, so the three forms draw the same gaps.
Change the Fields at Set Times
omit_when leaves a field out of a record while its condition is true, and t is the simulation time. currency is left out before 60 seconds. total is left out from 120 seconds, when amount replaces it, and amount is left out before then. From 180 seconds when writes the amount as text with format. A vars entry draws the value once for each order, so total and amount hold the same number.
Label Each Change
A step of the scenario with a record writes a label record at its time. The second egress has when: labels, so it receives only the labels, and the first receives only the orders. The labels say what changed and when without mixing into the data.
| docs/snippets/generate-data/change_the_schema_during_a_run.yaml:scenario | |
|---|---|
| docs/snippets/generate-data/change_the_schema_during_a_run_builder.py:scenario | |
|---|---|
| docs/snippets/generate-data/change_the_schema_during_a_run_low_level.py:changes | |
|---|---|
| docs/snippets/generate-data/change_the_schema_during_a_run_low_level.py:egress | |
|---|---|
The Low-level file publishes each label with env.publish_label from a process of its own, and labels=[False, True] sends the second sink only the labels.
Check the Result
Run the model for 240 simulated seconds and print its first 19 orders. The block keeps an order on each side of each change, and the three labels.
This prints every record of the run as an [EVT] line with the same fields and values.
This prints every record of the run as an [EVT] line with the same fields and values.
Order 6, at 60 seconds, is the first with currency. Order 12, at 120 seconds, is the first with amount in place of total. Order 18, at 180 seconds, is the first with an amount that is text. Each label carries the sim_ts of its change. The docs test checks all three changes and that the labels reach only the labels sink.