Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Corrected siddhi app name.

...

In this section, let's see how to add the required configurations to receive events via HTTP in JSON format.

  1. Let's open the SweetTotalsAppSweetTotalApp Siddhi application that you created in the previous tutorial. It currently looks as follows.

    Code Block
    languagesql
    @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;
  2. 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
    languagesql
    @source(type='http')
  3. 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')

  4. 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
    languagesql
    @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;
  5. Save the SweetTotalsAppSweetTotalApp Siddhi application. Then click the following icon to start it so that it can process the events you send.
     
  6. 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
    languagejs
    {
      "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 NoNameAmount
    1Jaffa Cake10
    2Jaffa Cake15

    As a result, two output events are expected to be generated with the following information.

    Output Event NoNameAmount
    1Jaffa Cake10
    2Jaffa Cake25

    This generates the following output log in the CLI.

...

  1. 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(...))



  2. The JSON mapper supports JsonPath. Therefore, the values for the attributes annotation can be JsonPath expressions. The JsonPath expressions for the name of the sweet and its total production are as follows.

    Stream Attribute NameJSON Event Attribute NameJsonPath Expression
    namesweet$.sweet
    amountcount$.batch.count

    Based on the above JsonPath expressions you identified, you can update the @attributes annotation as follows.

    Code Block
    languagejs
    @attributes(name = '$.sweet', amount = '$.batch.count')

    Once thie above substeps are completed, the mapping configuration looks as follows.

    Code Block
    languagejs
    @map(type = 'json', @attributes(name = '$.sweet', amount = '$.batch.count')

    The complete Siddhi application looks as follows.

    Code Block
    languagesql
    @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;
  3. Let's save the changes you made to SweetTotalsAppSweetTotalApp Siddhi application. Then click the following icon to start it so that it can process the events you send.
     
  4. 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
      }
    }'
    
    

...