Skip to content

How to Expose a Datasource as a Service

What you'll build

A data service provides a web service interface to access data that is stored in various datasources. The following sections describe how you can use Micro Integrator Extension for Visual Studio Code (MI for VS Code) to work with data services' artifacts.

Tip

Note that this feature is currently supported in Micro Integrator Extension for Visual Studio Code (MI for VS Code) for relational datasources and CSV files.

Let's get started!

Step 1: Set up the workspace

The following software and configurations are required to proceed with this tutorial:

  • Visual Studio Code (VS Code): with the Micro Integrator extension installed.
  • Java Development Kit (JDK): Ensure the JDK is properly configured in your system's PATH environment variable.
  • Apache Maven: Ensure Apache Maven is installed and its path is correctly set within the system's PATH environment variable.

Info

Follow the Install Micro Integrator for VS Code documentation for a complete installation guide.

  • MySQL server: Follow the below steps to set up MySQL server.

    1. Install the MySQL server.
    2. Download the JDBC driver for MySQL from here. You will need this when you configure the MySQL server with the Micro Integrator.

    3. Create a database named Employees.

      CREATE DATABASE Employees;
      
    4. Create a user and grant the user access to the Database.

      CREATE USER 'user'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON Employees.* TO 'user'@'localhost';

    5. Create the Employee table inside the Employees database:

      USE Employees;
      CREATE TABLE Employees (EmployeeNumber int(11) NOT NULL, FirstName varchar(255) NOT NULL, LastName varchar(255) DEFAULT NULL, Email varchar(255) DEFAULT NULL, Salary varchar(255));
      INSERT INTO Employees (EmployeeNumber, FirstName, LastName, Email, Salary) values (3, "Edgar", "Code", "[email protected]", 100000);
      

Step 2: Create a data service

Follow the steps given below to create a new data service.

Create an Integration project

The Integration project will contain all the required artifacts for the integration solution.

  1. Launch VS Code with the Micro Integrator extension installed.

  2. Click on the Micro Integrator icon on the Activity Bar of the VS Code editor.

    MI VS Code Extension

  3. Click Create New Project on Design View.

    Design View Pane Create New Project

    Next, the Project Creation Form will be opened.

  4. In the Project Creation Form, enter DataSourceServiceTutorial as the Project Name.

  5. Provide a location under Select Project Directory.

    create new project

  6. Click Create.

Create a data service with a data source

  1. Navigate to the MI Project Explorer > Data Services.

  1. Hover over Data Services and click the + icon that appears.

  1. Enter a name for the data service:

    Property Description
    Data Service Name RDBMSDataService

  2. Click Add Datasource.

  1. To create the datasource connection specify the following values to create the new datasource:

    Datasource Properties
    Property Value
    Datasource ID Datasource
    Datasource Type RDBMS
    Datasource Type (Default/External) Leave Default selected.
    Database Engine MySQL
    Driver Class com.mysql.jdbc.Driver
    URL jdbc:mysql://localhost:3306/Employees
    User Name user
    Password password

  1. Click Add.

  2. Click Create to create a data service with datasource.

Create a resource

Now, let's create a REST resource that can be used to invoke the query.

  1. Click new Data Service created in the previous step.

  2. Click + Resources.

  3. Enter the following resource details.

    Property Description
    Resource Path Employee/{EmployeeNumber}
    Resource Method GET

  4. Click Add.

Tip

Alternatively, you can generate a data service from a datasource. For more information, refer to Generate Data Services.

Configure data service

Let's write an SQL query to GET data from the MySQL datasource that you configured in the previous step:

  1. Click new Resource created in the previous step.

  2. Click Input Mapping in DataService View.

  3. Click Add Parameter.

  4. Specify the following values

    Property Description
    Mapping Name EmployeeNumber
    Query Parameter EmployeeNumber
    Parameter Type SCALAR
    SQL Type STRING

  5. Click Add. Then Click Submit.

  6. Click Query in DataService View.

  7. Specify the following values in the query details:

    Parameter Value
    Query ID GetEmployeeDetails
    Datasource Datasource
    SQL Query select EmployeeNumber, FirstName, LastName, Email from Employees where EmployeeNumber=:EmployeeNumber

  8. Click Submit.

  9. Click Transformation in DataService View.

  10. Specify the following value:

    Property Description
    Grouped by Element Employees

  11. Click Submit.

  12. Click Output Mapping in DataService View.

  13. Click Add Parameter. Specify the following values:

    Property Description
    Mapping Type Element
    Datasource Type column
    Output Field Name EmployeeNumber
    Datasource Column Name EmployeeNumber
    Parameter Type Scalar
    Schema Type String

  14. Save the parameter.

  15. Follow the same steps to create the following output parameters:

    Mapping Type Datasource Type Output Field Name Datasource Column Name Parameter Type Schema Type
    Element column FirstName FirstName Scalar string
    Element column LastName LastName Scalar string
    Element column Email Email Scalar string

  1. Click Submit.

Step 3: Configure the Micro Integrator server

To add the MySQL database driver to the server:

  1. Copy the MySQL driver JAR (see Setting up the Workspace) to /project-path/deployment/libs.

If the driver class does not exist in the relevant directory, you will get an exception such as Cannot load JDBC driver class com.mysql.jdbc.Driver when the Micro Integrator starts.

Step 4: Build and run the artifacts

Prerequisites

Before you begin, install Micro Integrator on your machine:

  1. Go to the WSO2 Micro Integrator web page, click Download, provide necessary details, and then click Zip Archive to download the Micro Integrator distribution as a ZIP file.

  2. Extract the ZIP file. The extracted folder will be referred as the <MI_HOME> folder.

Once you have downloaded and set up the Micro Integrator locally, follow the steps given below. Use one of the below two methods.

  1. Click on the Command Palette on the top of the VS Code.

  2. Type > to show the available commands.

  3. Select MI: Add MI server.

  4. Select Add MI server.

  5. Select the folder where <MI_HOME> is located. This wll be set as the current server path.

    Current server path

  6. Run the project.

    Click the Build and Run icon located in the top right corner of the VS Code.

    Build and run

Step 5: Test the data service

Let's test the use case by sending a simple client request that invokes the service.

Send the client request

Let's send a request to the API resource. You can use Postman or any other HTTP Client:

  1. Open the Postman application. If you do not have the application, download it from here : Postman.

  2. Add the request information as given below and click the Send button.

    Method GET
    URL http://localhost:8290/services/RDBMSDataService.HTTPEndpoint/Employee/3

If you want to send the client request from your terminal:

  1. Install and set up cURL as your REST client.
  2. Execute the following command.
    curl -X GET http://localhost:8290/services/RDBMSDataService.HTTPEndpoint/Employee/3
    

Analyze the response

You will see the following response received by your HTTP Client:

<Employees xmlns="http://ws.wso2.org/dataservice">
  <EmployeeNumber>3</EmployeeNumber>
  <FirstName>Edgar</FirstName>
  <LastName>Code</LastName>
  <Email>[email protected]</Email>
</Employees>