Step 2: Create the Siddhi Application¶
Let's create your first Siddhi application.
For this purpose, you can use the same example of temperature readings via a sensor mentioned in Streaming Integration Overview. You can consider a simple use case where you need to do view the temperature readings on your terminal. Also, instead of viewing all the temperature readings, you want to view the average of every three readings in a sliding manner.
The following image depicts the procedure to be followed by the Siddhi application you create.
Extract the Streaming Integrator Tooling pack to a preferred location. Hereafter, the extracted location is referred to as
Navigate to the
<SI_TOOLING_HOME>/bindirectory and issue the appropriate command depending on your operating system to start the Streaming Integration tooling.
Access the Streaming Integration Tooling via the
The default URL is
The Streaming Integration Tooling opens as shown below.
Open a new Siddhi file by clicking New.
The new file opens as follows.
Specify a name for the new Siddhi application via the
@App:nameannotation, and a description via the
@App:name("TemperatureApp") @App:description("This application captures the room temperature and analyzes it, and presents the results as logs in the output console.")
The details to be captures include the room ID, device ID, and the temperature. To specify this, define an input stream with attributes to capture each of these details.
define stream TempStream(roomNo string, deviceID long, temp double)
The technicians need to know the average temperature with each new temperature reading. To publish this information, define an output stream including these details as attributes in the schema.
define stream AverageTempStream(roomNo string, deviceID long, avgTemp double)
The average temperature needs to be logged. Therefore, connect a sink of the
logtype to the output stream as shown below.
@sink(type = 'log', @map(type = 'passThrough')) define stream AverageTempStream (roomNo string, deviceID long, avgTemp double);
passThroughis specified as the mapping type because in this scenario, the attribute names are received as they are defined in the stream and do not need to be mapped.
To get the input events, calculate the average temperature and direct the results to the output stream, add a query below the stream definitions as follows:
To name the query, add the
@infoannotation and enter
CalculateAverageTemperatureas the query name.
@info(name = 'CalculateAvgTemp')
To indicate that the input is taken from the
TempStreamstream, add the
fromclause as follows:
#window.length(3)is a length window connected to the
TempStreaminput stream indicates that the calculations are applied to every three input events in a sliding manner.
For more information about all the supported window types, see Siddhi Query Guide - Siddhi Execution Unique.
Specify how the values for the output stream attributes are derived by adding a
selectclause as follows.
select roomNo, deviceID, avg(temp)
Here, you are applying the
avg()function to the
tempattribute in order to calculate the average for that attribute. However, this average calculation is applied to every subset of three events due to the length window connected to the
To insert the results into the output stream, add the
insert intoclause as follows.
insert into AverageTempStream;
The completed Siddhi application is as follows:
@App:name('TemperatureApp') @App:description('This application captures the room temperature and analyzes it, and presents the results as logs in the output console.') define stream TempStream (roomNo string, deviceID string, temp double); @sink(type = 'log', @map(type = 'passThrough')) define stream AverageTempStream (roomNo string, deviceID string, avgTemp double); @info(name = 'CalculateAvgTemp') from TempStream#window.length(3) select roomNo, deviceID, avg(temp) as avgTemp insert into AverageTempStream;
To test the
TemperatureApp Siddhi application you created, proceed to Step 3: Test the Siddhi Application.