Look Up Other Records
Join records as they are made: an order names a customer written earlier, a payment reads the order that led to it, and a report summarises the last 10 orders.
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.
- Task Flow explains
next.lookup,upstreamandaggregatelist the keys of the three generators.
Steps
Describe the Arrivals
Five customers are written when the run starts (initial: 5), and another about every 10 seconds. An order arrives about once a second, and a report every 10 seconds.
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.
Read Other Records
Three generators read the rows that other tasks have written:
lookupgives an order thecustomer_idof a customer already written. The five customers written at the start make sure that every order finds one. An order that starts before any customer has a row is dropped, so no record is written for it.upstreamgives the payment theorder_idandtotalof the order thatnext: paymentrouted to it.aggregategives a report the mean and the largest total of the last 10 orders (last: 10).
A task that something looks up or aggregates keeps its rows, and history= says how many. A Builder task needs it, because a Template payload is not searched for them. A blueprint keeps 1,000,000 rows by default.
A Low-level script keeps the rows that a lookup or an aggregate reads in lists of its own, and picks a customer and takes the last 10 orders itself.
Check the Result
Run the model for 60 simulated seconds and print its first 40 records. The block keeps three of them: the first order, the payment that follows it and the first report. The order names a customer that exists, the payment repeats the order's id and total as amount, and the report, at 10 seconds, gives the mean and the largest total of the first 10 orders.
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.
The docs test checks every order against the customers written by then, every payment against its order, and every report against the last 10 orders.
Full Files
Full files
| docs/snippets/generate-data/look_up_other_records_builder.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
| docs/snippets/generate-data/look_up_other_records_low_level.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |