...
In this section, let's see how to add the required configurations to receive events via HTTP in JSON format.
Let's open the
SweetTotalsApp
SweetTotalApp
Siddhi application that you created in the previous tutorial. It currently looks as follows.Code Block language sql @App:name('SweetTotalApp') define stream SweetProductionStream (name string, amount long); @sink(type='log', prefix='Sweet Totals:') define stream SweetTotalStream(name string, totalProduction long); @info(name='SweetTotalQuery') from SweetProductionStream select name, sum(amount) as totalProduction group by name insert into SweetTotalStream;
To receive the events generated by the Sweet Bots via HTTP calls, you need an event source of the
http
type. Let's add it as shown below.Code Block language sql @source(type='http')
The HTTP events need to be received at a specific endpoint. Let's add the URL of this endpoint to the HTTP source configuration as follows:
Panel @source(type='http', receiver.url='http://localhost:5005/SweetProductionEP')
The incoming messages generated by Sweet Bots are in the JSON format. You require a mapping configuration to transform them into a format that can be processed by WSO2 SP. Therefore, let's add an annotation for JSON mapping to the HTTP source as shown below.
Info For the complete list of mapping types supported for WSO2 SP, see Collecting Events - Event Format.
Panel @source(type='http', receiver.url='http://localhost:5005/SweetProductionEP', @map(type = 'json'))
Now the
SweetTotalsApp
SweetTotalApp
Siddhi application looks as follows.Code Block language sql @App:name('SweetTotalApp') @source(type='http', receiver.url='http://localhost:5005/SweetProductionEP', @map(type = 'json')) define stream SweetProductionStream (name string, amount long); @sink(type='log', prefix='Sweet Totals:') define stream SweetTotalStream(name string, totalProduction long); @info(name='SweetTotalQuery') from SweetProductionStream select name, sum(amount) as totalProduction group by name insert into SweetTotalStream;
- Save the
SweetTotalsApp
SweetTotalApp
Siddhi application. Then click the following icon to start it so that it can process the events you send.
To see whether the Siddhi application functions as expected, let's send two events with the following information.
The input events generated by the Sweet Bots are in the JSON format. Therefore, the format is similar to the sample shown below.
Code Block language js { "event": { "name": "Jaffa Cake", "amount": 10 } }
In order to send the two input events to WSO2 SP in the format shown above, issue the following two cURL commands.
Code Block curl -X POST \ http://localhost:5005/SweetProductionEP \ -H 'content-type: application/json' \ -d '{ "event": { "name": "Jaffa Cake", "amount": 10 } }'
Code Block curl -X POST \ http://localhost:5005/SweetProductionEP \ -H 'content-type: application/json' \ -d '{ "event": { "name": "Jaffa Cake", "amount": 15 } }'
The two events sent contains the following information.
Input Event No Name Amount 1 Jaffa Cake
10
2 Jaffa Cake
15 As a result, two output events are expected to be generated with the following information.
Output Event No Name Amount 1 Jaffa Cake
10
2 Jaffa Cake
25 This generates the following output log in the CLI.
...
- To allow the Siddhi application to identify the elements that need to be extracted from the JSON message, add an
@attributes
annotation to the mapping configuration as follows.Panel @map(type = 'json', @attributes(...))
The JSON mapper supports
JsonPath
. Therefore, the values for the attributes annotation can beJsonPath
expressions. TheJsonPath
expressions for the name of the sweet and its total production are as follows.Stream Attribute Name JSON Event Attribute Name JsonPath
Expressionname
sweet
$.sweet
amount
count
$.batch.count
Based on the above
JsonPath
expressions you identified, you can update the@attributes
annotation as follows.Code Block language js @attributes(name = '$.sweet', amount = '$.batch.count')
Once thie above substeps are completed, the mapping configuration looks as follows.
Code Block language js @map(type = 'json', @attributes(name = '$.sweet', amount = '$.batch.count')
The complete Siddhi application looks as follows.
Code Block language sql @App:name('SweetTotalApp') @source(type='http', receiver.url='http://localhost:5005/SweetProductionEP', @map(type = 'json', @attributes(name = '$.sweet', amount = '$.batch.count'))) define stream SweetProductionStream (name string, amount long); @sink(type='log', prefix='Sweet Totals:') define stream SweetTotalStream(name string, totalProduction long); @info(name='SweetTotalQuery') from SweetProductionStream select name, sum(amount) as totalProduction group by name insert into SweetTotalStream;
- Let's save the changes you made to
SweetTotalsApp
SweetTotalApp
Siddhi application. Then click the following icon to start it so that it can process the events you send.
Now you can issue the following cURL commands to send the two incoming events as sent by the Sweet Bots.
Code Block curl -X POST \ http://localhost:5005/SweetProductionEP \ -H 'content-type: application/json' \ -d '{ "sweet": "Jaffa Cake", "batch": { "batch id": "batch1", "count": 10 } }'
Code Block curl -X POST \ http://localhost:5005/SweetProductionEP \ -H 'content-type: application/json' \ -d '{ "sweet": "Jaffa Cake", "batch": { "batch id": "batch1", "count": 15 } }'
...