Within a Lua window of SAS Event Stream Processing, you write Lua code to programmatically generate events. The following examples show how to code a Lua window to parse a JSON string in order to generate multiple events from a single event stream.
- The basic example project is designed to show how event generation works. This example is included in the
lua_parse_simple<release>.zip
project package and can be installed directly from SAS Event Stream Processing Studio. For more information, see Using the Examples. - The advanced example project is intended to provide a more realistic use case of event generation.
NOTE: Use these examples with SAS Event Stream Processing 2022.10 and later.
The following figure illustrates the complete flow of events through the SAS Event Stream Processing project.
- The Source window (source) receives the sample input data from a CSV file.
- The JSON data embedded in the CSV file is streamed to the Lua window (createMulti).
- The Lua window generates multiple events from the JSON data.
The source window is a Source window that streams data from the file luaParseJsonSimple.csv. That file contains JSON data that is encapsulated within a CSV string.
NOTE: The first two columns in the CSV file contain the opcodes for SAS Event Stream Processing.
The JSON data consists of sensor data for two devices. One of the devices has data from one sensor, and the other device has data from two sensors, as illustrated in the following figure.
The createMulti window is a Lua window. It contains Lua code that performs the following steps:
Step | Lua Code Section |
---|---|
Creates an event ID for each event, beginning with the value 1. |
|
Places the parsed JSON data into a Lua table. |
|
Extracts key-value pairs from the Lua table. |
|
Returns results as SAS Event Stream Processing events. |
|
To view project in its entirety and the Lua code in context, see the luaParseJsonSimple.xml file.
To see a demo of how to test this project and view the results, watch the following video:
lua-parse-basic.mp4
Typically, sensor data streams into SAS Event Stream Processing within a JSON opaque string that is streamed over a message broker such as RabbitMQ. You can set up a Lua window to parse the opaque string and use the data to generate events.
In this example, simulated sensor data from a train (speed, location, car ID, and date) within an opaque JSON string is received from RabbitMQ. The project transforms this data into multiple events. Specifically, the project does the following:
- Configures a message broker to receive the JSON objects.
- Sends the JSON object via
opaquestring
to a Lua window for processing. - Uses Lua code to parse the JSON string and generate multiple events.
To run this example project, you must configure a RabbitMQ message broker to work with the project in SAS Event Stream Processing Studio.
The following figure illustrates the complete flow of events through the SAS Event Stream Processing project.
- The first Source window (train_data) receives the sample input data from a CSV file.
- RabbitMQ subscribes to train_data and converts it to a JSON opaque string.
- The JSON opaque string enters the second Source window (pub_win).
- The JSON string is streamed to the Lua window (parseJson).
- The Lua window generates multiple events from the JSON string.
- For demonstration purposes, sensor data input is simulated by uploading a CSV file to a Source window (train_data). The CSV data enters this Source window through a file and socket connector. To view the CSV data, see the trainData.csv file.
- The message broker RabbitMQ connects to train_data using a subscriber connector. RabbitMQ transforms the CSV data into a JSON string.
The order in which connectors start is specified with connector groups. For more information about subscriber and publisher connectors, see SAS Event Stream Processing: Overview to Connectors.
- RabbitMQ publishes a JSON opaque string named "sensorinfo" to the pub_win Source window.
- The JSON opaque string is streamed to the Lua window (parseJson).
The parseJson window is a Lua window. It contains Lua code that performs the following steps:
Step | Lua Code Section |
---|---|
Places the parsed JSON opaque string into a Lua table. |
|
Extracts key-value pairs from the Lua table. |
|
Returns results as SAS Event Stream Processing events. |
|
To view the project in its entirety and see the Lua code in context, see the project file luaParseJson.xml.
To see a demo of how to test this project and view the results, watch the following video:
lua-parse-advanced.mp4
You can add windows to the project to further analyze the data. For example, you might add a Filter window to extract speeds above a certain value.
For more information, refer to the following resources: