This example demonstrates how you can define useful functions in a Lua module at project level and reference those functions from other Lua code in the project.
NOTE: Use this example with SAS Event Stream Processing 2023.12 and later.
For more information about how to install and use example projects, see Using the Examples.
The Lua window in this project performs division calculations and logs an error message when there is an attempt to divide a number by zero. To log the error, the Lua window references a Lua module that defines the logging functionality.
The lua_module.csv file contains information about calculations.
The following figure shows the diagram of the project:
-
Open the project in SAS Event Stream Processing Studio and click on the toolbar. Project-level properties are displayed in the right pane.
-
Expand Lua Modules
-
In the table in this section, double-click the logger row.
-
In the Edit Lua Code Module window, explore the Lua code.
Step Lua Code Section Create a table named Logger. Logger = { _context = nil }
Define the Logger class. function Logger:new(context) o = {} setmetatable(o,self) self.__index = self self._context = context or "mylogger" return o end
Define the logging functions that are used to create log messages at different severity levels. These functions use a SAS Event Stream Processing function called esp_logMessage. function Logger:error(msg,line) esp_logMessage(self._context,msg,"error",line) end function Logger:warn(msg,line) esp_logMessage(self._context,msg,"warn",line) end function Logger:fatal(msg,line) esp_logMessage(self._context,msg,"fatal",line) end function Logger:info(msg,line) esp_logMessage(self._context,msg,"info",line) end function Logger:debug(msg,line) esp_logMessage(self._context,msg,"debug",line) end function Logger:trace(msg,line) esp_logMessage(self._context,msg,"trace",line) end
The Source window streams information from the lua_module.csv file to the Lua window.
Explore the settings for this window:
- Select the Source window on the workspace.
- To examine the window's output schema, on the right toolbar, click . Observe the following fields:
id
: This is the ID that is assigned to each calculation. It is also selected as the Key.dividend
: This is a number that is to be divided by the number in thedivisor
field.divisor
: This is a number that is used as the divisor in the calculation.quotient
: This is the result of the division.
- Click .
The Lua window performs division calculations.
Explore the settings for this window:
-
Select the Lua window in the workspace.
-
In the right pane, expand Lua Settings.
- To increase the efficiency of the Lua code processing, you can specify which fields to copy from the source file and which fields to use in the Lua code.
- The Fields to copy field shows that the fields
id
anddividend
, anddivisor
are copied from the source file. - The Fields to use in Lua code field shows that the fields
dividend
anddivisor
are used in the Lua code.
- The Fields to copy field shows that the fields
- The Events function field shows the Lua function that is used (in this case, the
create
function). - The Required modules field specifies that the
logger
module is required by this window.
- To increase the efficiency of the Lua code processing, you can specify which fields to copy from the source file and which fields to use in the Lua code.
-
Scroll down in the right pane, to view the Lua code that performs the calculations for this example:
Step Lua Code Section Define a function called create and set three variables to represent an event and information about the dividend and the divisor. function create(data) local e = {} local dividend = data["dividend"] local divisor = data["divisor"]
Check whether the divisor is zero. If so, use the Logger class (defined in the Lua module) to log an error message. if (divisor == 0) then local logger = Logger:new("modules.example") logger:error("division by 0, "..tostring(dividend).."/"..tostring(divisor)) return nil end
If the divisor is not zero, perform the calculation and return a SAS Event Stream Processing event. e["quotient"] = dividend / divisor return(e) end
When you test the project in SAS Event Stream Processing Studio, the results for each window appear in separate tabs. The following figure shows the Lua tab. The lua_module.csv file contains four events, but only three events appear in the Lua tab. The fourth event, where the number five was to be divided by zero, has caused the following error to be logged in the Log pane: division by 0, 5/0
In this simple example, the Lua module is referenced from one location. In a real-life scenario, creating a Lua module is most useful when you want to reference common Lua code from multiple locations.
For more information, see SAS Help Center: Using Lua Modules and SAS Help Center: ESP Server Log Debugging.