Scenario 3: REST service accepting HTTP POST requests - 6.1

Talend Components Reference Guide

Talend Big Data
Talend Big Data Platform
Talend Data Fabric
Talend Data Integration
Talend Data Management Platform
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for Big Data
Talend Open Studio for Data Integration
Talend Open Studio for Data Quality
Talend Open Studio for ESB
Talend Open Studio for MDM
Talend Real-Time Big Data Platform
Talend Studio
Data Governance
Data Quality and Preparation
Design and Development

This scenario describes a Job composed of two subjobs: the first subjob exposes a REST service that accepts HTTP POST requests from REST clients, writes data to a database upon receiving an HTTP request, and displays the server-client exchange information on the Run console; the second subjob displays the results of database updates.

Setting up the Job

  1. Create a Job and add the following components to the Job by typing their names in the design workspace or dropping them from the Palette: a tRESTRequest, a tMysqlOutput, a tXMLMap, and a tRESTResponse.

  2. Connect the tRESTRequest to the tMysqlOutput component using a Row > Main connection, and give the output flow a name, request in this example.

  3. Connect the tMysqlOutput component to the tXMLMap component using a Row > Main connection.

  4. Connect the tXMLMap component to the tRESTResponse component using a Row > Main connection, and give it a name, response in this example. Click OK in the pop-up dialog box to accept the schema propagation from the tRESTResponse component.

  5. Add a tMysqlInput component and a tLogRow component to the Job by typing their names in the design workspace or dropping them from the Palette, and connect the tMysqlInput to the tLogRow using a Row > Main connection.

  6. Connect the tMysqlOutput to the tMysqlInput using a Trigger > OnComponentOk connection to link the two subjobs.

Configuring the components

Configuring REST request parameters

  1. Double-click the tRESTRequest component to open its Basic settings view.

  2. Fill the REST Endpoint field with the URI location where the REST service will be accessible for requests, "http://localhost:8045/users" in this example.

  3. Click the output flow name, which is request in this example, in the Output Flow column of the REST API Mapping table to show the [...] button, and click the button to open the [Schema] dialog box. Then define the schema for the request flow as follows:

    • id, type Integer, 2 characters long, set as the key column

    • first_name, type String

    • last_name, type String

    When done, click OK to validate the schema setting and close the dialog box.

  4. Click the HTTP Verb column and select POST from the list.

  5. In the URI Pattern column, enter the acceptable URI pattern of POST requests, "/post/{id}/{first_name}/{last_name}" in this example.

    Leave the rest parameters as they are.

  6. Click the Advanced settings tab, and select the Log messages check box.

Configuring the database and the response parameters

  1. Double-click the tMysqlOutput component to open its Basic settings view.

  2. Leave the Property Type as Built-in and fill in the database connection details manually in the Host, Port, Database, Username and Password fields.

    If you have centralized your connection information to the database in the Metadata > DB Connections node of the Repository, you can select Repository from the Property Type list and browse to the centralized connection to have the fields automatically filled in. For more information about storing metadata, see Talend Studio User Guide.

  3. Fill in the Table field with the target database table name, users in this example.

  4. Select the actions to be carried out on the database table and data according to your needs from the corresponding lists. In this example, the target table will be created if it does not exist in the specified database, and data from client requests will be inserted, or updated if it already exists, to the database table.

  5. Click the Sync columns button to synchronize the table schema with the input schema.

  6. Double-click the tXMLMap component to open the Map Editor.

  7. Rename the root node in the output table: right-click the node, select Rename from the contextual menu, and specify a new name in the pop-up dialog box, user in this example.

  8. Select all the three columns in the input table and drop them onto the user node, and select the Create as sub-element of target node option from the pop-up dialog box to set these columns as sub-elements of the user node. When done, click OK to validate the mappings and close the Map Editor.

  9. Double-click the tRESTResponse component and set the response parameters according to your needs. In this example, we keep the default settings for all the parameters.

Configuring result display

  1. Double-click the tMysqlInput component to open its Basic settings view.

  2. Specify the database connection details, table name and table schema, which are the same as in the tMysqlOutput component.

  3. Click the Guess Query button to fill the Query field with the query statement.

  4. Double-click the tLogRow component and select the Table option to display the content of the database in table cells.

Saving the executing the Job

  1. Press Ctrl+S to save your Job.

  2. Click the Run button on the Run tab or press F6 to run it.

    The console displays the service implementation information, including the service URL.

    When an HTTP POST request is received from a client, the console displays the relevant exchange information and the database update result.

    For a use case that calls this REST service using the HTTP POST method, see Scenario 2: Updating user information by interacting with a RESTful service.