# How to use batch node

This tutorial we will take you through the use case were we have database result set returning an JSON array. We need to sequence the result set in to a batch of 5 records each before sending to an external system.

  1. Drag and drop an Inject node. This is to trigger the flow for testing

  2. Place a function node from the transformation section in the pallette.

    TIP

    We will emulate the database result set inside a function node

  3. Double click the function node to open the function editor.

    Provide the name of the function as DataInput.

    Paste the below provided code inside the function node in order to emulate the result set from a database which will be later batched by the batch node. In the below code the json array will be iterated to split and send one by one to batch node for re sequencing \ batching. The database result payload we having has 9 records.

    var testdata =  
             [
                 {"car":"ford"},
                 {"car":"benz"},
                 {"car":"mcg"},
                 {"car":"hyundai"},
                 {"car":"tesla"},
                 {"car":"duxter"},
                 {"car":"toyota"},
                 {"car":"camry"},
                 {"car":"volvo"}
             ];
         for(var attributename in testdata){
              node.send({payload: testdata[attributename]});
         }
    return msg;
    

    Click Save to close the editor

  4. Wire the Inject node to the DataInput function node.

  5. Drop the Batch node from the routing section in the pallete to the canvas.

    Double click the batch node to open the editor and provide the name for the bath node as Batching. In this use case we will be batching the records statically based on the number of messages. Here we will select the option Group by number of messages and keep the Number of messages as 5. We are not going to set the overlap number.

    Click Save to close the editor.

  6. Now wire the function node to batch node

  7. Drag and drop the Join node from the routing section in the pallette to the canvas.

TIP

Its is always required for batch node to be used in conjunction with join node. All the values of the join node will be kept default in this use case.

  1. Wire the batch node to join node.

  2. Now let finally place a Debug node at the end just to mark the completion by printing the batched message. Open the debug node and provide the name of the debug node as Completed

  3. Click on the Inject node to trigger the test .

Observation

On the debug panel (right side of the editor) you should see the default input and output of each node that got executed . Along with that you should see debug node with the name Completed printed twice . This means the database result set which we have emulated contains 9 records and based on our setting on the batch node (number of message as 5) it has grouped the records as two batches of 5 records each.

Click on the Complete node entry on the debug panel and and see the batched array of records.