Scenario 4: REST service accepting HTTP POST requests and sending responses - 6.1

Talend Open Studio for Big Data Components Reference Guide

EnrichVersion
6.1
EnrichProdName
Talend Open Studio for Big Data
task
Data Governance
Data Quality and Preparation
Design and Development
EnrichPlatform
Talend Studio

This scenario describes how to build a REST service that accepts HTTP POST requests of Document type from the client, processes it and sends the response back. To do this, two Jobs are built: one to create the REST service and the other to call it.

Creating the REST service Job

Drag and drop the following components from the Palette onto the design workspace: a tRESTRequest, a tXMLMap and a tRESTResponse.

Configuring the tRESTRequest component
  1. Double-click tRESTRequest in the design workspace to display its Basic settings view.

  2. In the REST Endpoint field, keep the default URI "http://localhost:8088/" where the REST-ful web service will be accessible for requests.

  3. Click the [+] button to add one line in the REST API Mapping table.

  4. Select the newly-added line and click the [...] button in the Output Flow column to add a schema for the output flow. Give the name for the schema UserInfo in this scenario.

    The schema editor opens. Click the [+] button to add a new line body of Document type and click OK to save the schema.

  5. Back to tRESTRequest Basic settings view, select POST from the list in the HTTP Verb column.

  6. Fill the field in the URI Pattern column with "/UserInfo". Keep the default settings of the other options.

Configuring the tXMLMap component
  1. Connect tRESTRequest to tXMLMap using the Row > UserInfo connection.

  2. Double-click tXMLMap in the design workspace to open the Map Editor.

  3. Right-click on the root node of the input table and select Create Sub-Element. Name the sub-element as person in the popup dialog box.

  4. Right-click on the person node created in the previous step and select As loop element.

  5. Create two sub-elements to the person node by selecting Create Sub-Element in the contextual menu, id and name in this use case.

  6. Click the [+] button on the top right to add an output and name it as Response.

  7. Click the [+] button on the bottom right to add a column body of Document type for the output.

  8. Right-click the node root in the output table and select Create Sub-Element. Name the sub-element as person in the popup dialog box.

  9. Right-click the person node created in the previous step and select As loop element.

  10. Create three sub-elements to the person node by selecting Create Sub-Element in the contextual menu, id, name and company in this use case.

  11. Select the id column from the input table and drop it onto the Expression field of the id node of the output table.

  12. Select the name column from the input table and drop it onto the Expression field of the name node in the output table.

    Click the Expression field of the name node in the output table, and click [...] in this field to show the [Expression Builder] wizard.

    In the Expression area, enter StringHandling.UPCASE([UserInfo.body:/root/person/name]) to convert the name of the user to upper case. Click OK to close the wizard.

    For further information about using the Expression Builder, see Talend Studio User Guide.

  13. In the Expression field of the company node, enter "Talend".

  14. Click OK to save the settings.

Configuring the tRESTResponse component
  1. Connect tXMLMap to a tRESTResponse using Row > Response connection.

    The schema defined in tXMLMap is retrieved in tRESTResponse automatically.

  2. Select OK(200) from the Return status code list.

  3. Leave the rest of the settings as they are.

Saving and executing the Job

Save the Job and press F6 to execute it. The service is started.

Creating the consumer Job

Drag and drop the following components from the Palette onto the design workspace: a tFixedFlowInput, a tXMLMap, a tRESTClient and a tLogRow.

Configuring the tFixedFlowInput component
  1. Double-click tFixedFlowInput in the design workspace to display its Basic settings view.

  2. Click Edit schema to show the schema editor. In the schema editor, click the [+] button to add two columns of String type, id and name.Click Ok to close the schema editor.

  3. Back to tFixedFlowInput Basic settings view, in the Mode area, select the Use Inline Content option.

  4. In the Content box, enter the id and name of three users, for example:

    1;ford
    2;smith
    3;thomson
Configuring the tXMLMap component
  1. Connect tFixedFlowInput to tXMLMap using the Row > Main connection.

  2. Connect tXMLMap to tRESTClient using the Row > Main connection, and name the output flow request in this example.

  3. Double-click tXMLMap in the design workspace to open the Map Editor.

  4. Right-click on the node root in the output table and select Create Sub-Element. Name the sub-element as person in the popup dialog box.

  5. Right-click on the person node created in the previous step and select As loop element.

  6. Create two sub-elements to the person node by selecting Create Sub-Element in the contextual menu, id, and name in this use case.

  7. Select the id column from the input table and drop it onto the Expression field of the id node of the output table.

  8. Select the name column from the input table and drop it onto the Expression field of the name node in the output table.

  9. Click OK to save the settings.

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

  2. Fill the URL field with the URL of the REST service built in the service Job, "http://localhost:8088/".

  3. Fill the Relative Path with the "UserInfo".

  4. From the HTTP Method list, select POST.

  5. Leave the rest of the settings as they are.

  6. Connect tRESTClient to tLogRow using the Row > Response connection. Keep the default settings of the tLogRow to monitor the message exchanges.

Saving and executing the Job

Save the Job and press F6 to execute it. The user information is shown in the console, including id, name, and company. The name of the user has been converted to upper case.