The Lua window in SAS Event Stream Processing enables you to maintain and use event state to generate events.
NOTE: Use these examples with SAS Event Stream Processing 2022.10 and later.
For more information about how to install and use example projects, see Using the Examples.
This example is built on the following scenario:
The Acme Stockbroking firm wants to track and analyze the maximum price for all incoming stock trade data in SAS Event Stream Processing. To do so, the Data Engineer creates an ESP project that performs the following steps:
- Receive the stock trade data.
- Use a Lua window to generate events every time a new stock symbol price exceeds the current maximum price for that stock symbol.
To view the project in its entirety, see the model.xml file.
The file luaStateInput.csv
contains stock trade data. It is formatted as a CSV file to simplify the process for the purposes of this example. In a real-life scenario, this data would likely enter the project via a message broker such as RabbitMQ. To download a ZIP file containing luaStateInput.csv
, click here.
The trades window is a Source window. It receives input data from the file luaStateInput.csv.
The generateEvents window is a Lua window. It contains Lua code that analyzes the input data and generates multiple events. To view the Lua code in context, see the model.xml file.
NOTE:
To optimize performance, the variables symbol
and price
have been selected for the Fields to use in Lua code option. For more information, see Working with Lua Windows in SAS Event Stream Processing Studio.
Step | Lua Code Section |
---|---|
Creates a variable that contains a table with the highest price for each stock symbol. |
|
Defines and initializes local variables for the event and maximum price. |
|
For every key-value pair in the symbolData table, determines whether the maximum price has been exceeded.
If so, generates an event and upsert (update or insert) the maximum price for the symbol. |
|
Returns results as SAS Event Stream Processing events. |
|
When you test the project, the results for each window appear on separate tabs. In the following animated image, the window to the right shows input events as they are streamed into the trades window. The window to the left shows how the Lua code uses those events to periodically update the stock symbols, based on their price.
Now that you have generated events from the stock trade data, you can add windows to the project to analyze the data.
For more information, refer to the following resources: