Skip to content

Latest commit

 

History

History

lua_state

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Use the Lua Window to periodically output events

Overview

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.

Example

Use Case

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:

  1. Receive the stock trade data.
  2. 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.

Source Data

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.

Workflow

trades

The trades window is a Source window. It receives input data from the file luaStateInput.csv.

generateEvents

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.
local   symbolData = {}
Defines and initializes local variables for the event and maximum price.
function create(data,context)
  local   event = nil
  local   maxPrice = symbolData[data.symbol]
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.

if (maxPrice == nil or data.price > maxPrice)
   then
      symbolData[data.symbol] = data.price
      event = {}
      event.symbol = data.symbol
      event.max_price = data.price
      event._opcode = "upsert"
   end
Returns results as SAS Event Stream Processing events.
    return(event)
end

Test the Project and View the Results

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.

luaState project output

Next Steps

Now that you have generated events from the stock trade data, you can add windows to the project to analyze the data.

Additional Resources

For more information, refer to the following resources: