Data Integration¶
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 WSO2 Integration Studio to work with data services' artifacts.
Tip
Note that this feature is currently supported in WSO2 Integration Studio for relational datasources and CSV files.
Let's get started!¶
Step 1: Set up the workspace¶
To set up the tools:
-
Download the relevant WSO2 Integration Studio based on your operating system. The path to the extracted/installed folder is referred to as
MI_TOOLING_HOME
throughout this tutorial.Tip
If you do not see the features given below in the WSO2 Integrations Studio that you are using, you can get the latest updates.
-
Optionally, you can set up the CLI tool for artifact monitoring. This will later help you get details of the artifacts that you deploy in your Micro Integrator.
- Go to the WSO2 Micro Integrator website.
- Click Download -> Other Resources and click CLI Tooling to download the tool.
- Extract the downloaded ZIP file. This will be your
MI_CLI_HOME
directory. - Export the
MI_CLI_HOME/bin
directory path as an environment variable. This allows you to run the tool from any location on your computer using themi
command. Read more about the CLI tool.
To demonstrate how data services work, we will use a MySQL database as the datasource. Follow the steps given below to set up a MySQL database:
- Install the MySQL server.
-
Download the JDBC driver for MySQL from here and copy it to the
lib
directory of the embedded Micro Integrator of WSO2 Integration Studio.Note
The
lib
directory of the embedded Micro Integrator of WSO2 Integration Studio is located inMI_TOOLING_HOME/Contents/Eclipse/runtime/microesb/
(for MacOS/CentOS) orMI_TOOLING_HOME/runtime/microesb/lib/
(for Windows/Linux).If the driver class does not exist in the relevant directory when you create the datasource, you will get an exception such as
Cannot load JDBC driver class com.mysql.jdbc.Driver
. -
Create a database named
Employees
.CREATE DATABASE Employees;
-
Create the
Employee
table inside theEmployees
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: Creating a data service¶
Follow the steps given below to create a new data service.
Creating a data service project¶
All the data services' artifacts that you create should be stored in a Data Service project. Follow the steps given below to create a project:
-
Open WSO2 Integration Studio and click Data Service → Create Data Service Project in the Getting Started tab as shown below.
-
In the New Data Service Project wizard that opens, give a name for the project and click Next.
- Click Finish. The new project will be listed in the project explorer.
Creating the data service¶
Follow the steps given below to create the data service file:
-
Select the already created Data Service Project in the project explorer, right-click and go to New -> Data Service.
The New Data Service window will open as shown below.
-
Select Create New Data Service and click Next to go to the next page.
-
Enter a name for the data service and click Finish:
Property Description Data Service Name RDBMSDataService
A data service file (DBS file) will now be created in your data service project as shown below.
Creating the datasource connection¶
- Click Data Sources to expand the section.
- Click Add New to open the Create Datasource page.
-
Enter the datasource connection details given below.
Parameter Description Datasource ID Enter Datasource
.Datasource Type
Select RDBMS
from the list.Datasource Type (Default/External) Leave Default
selected.Database Engine Select MySQL
from the list.Driver Class Enter com.mysql.jdbc.Driver
.URL Enter jdbc:mysql://localhost:3306/Employees
.User Name Enter root
. -
Click Test Connection to expand the section.
-
Click the Test Connection button to verify the connectivity between the MySQL datasource and the data service.
- Save the datasource.
Creating a query¶
Let's write an SQL query to GET data from the MySQL datasource that you configured in the previous step:
- Click Queries to expand the section.
- Click Add New to open the Add Query page.
-
Enter the following query details:
Parameter Description Query ID Enter GetEmployeeDetails
.Datasource Select Datasource
from the list.SQL Query Enter the following SQL statement: select EmployeeNumber, FirstName, LastName, Email from Employees where EmployeeNumber=:EmployeeNumber
-
Click Input Mappings to expand the section.
- Click Add New to open the Add Input Mapping page.
-
Enter the following input mapping details:
Parameter Description Mapping Name Enter EmployeeNumber
.Parameter Type Select SCALAR
from the list.SQL Type Select STRING
from the list. -
Save the input mapping.
- Click Result (Output Mappings) to expand the section.
-
Enter the following value to group the output mapping:
Property Description Grouped by Element Employees -
Click Generate to generate output mappings automatically.
Tip
Alternatively, you can manually add the mappings:
- Click Add New to open the Add Output Mapping page.
-
Enter the following output element details.
Property Description Datasource Type column Output Field Name EmployeeNumber Datasource Column Name EmployeeNumber Schema Type String -
Save the element.
Follow the same steps to create the following output elements:
Datasource Type | Output Field Name | Datasource Column Name | Schema Type |
---|---|---|---|
column | FirstName | FirstName | string |
column | LastName | LastName | string |
column | string |
You will now have the following output elements created:
Creating a resource to invoke the query¶
Now, let's create a REST resource that can be used to invoke the query.
- Click Resources to expand the section.
- Click Add New to open the Create Resource page.
-
Enter the following resource details.
Property Description Resource Path Employee/{EmployeeNumber} Resource Method GET Query ID GetEmployeeDetails -
Save the resource.
Step 3: Package the artifacts¶
Create a new composite application project:
- Open the Getting Started view and click Miscellaneous → Create New Composite Application.
- In the New Composite Application Project wizard that opens, select the data service file, and click Finish.
Package the artifacts in your composite application project to be able to deploy the artifacts in the server.
- Open the
pom.xml
file in the composite application project POM editor. - Ensure that your data service file is selected in the POM file.
- Save the project.
Step 4: Build and run the artifacts¶
To test the artifacts, deploy the packaged artifacts in the embedded Micro Integrator:
- Right-click the composite application project and click Export Project Artifacts and Run.
- In the dialog box that opens, select the composite application project that you want to deploy.
- Click Finish. The artifacts will be deployed in the embedded Micro Integrator and the server will start. See the startup log in the Console tab.
Step 5: Testing the data service¶
Let's test the use case by sending a simple client request that invokes the service.
Get details of deployed artifacts (Optional)¶
Let's use the CLI Tool to find the URL of the data service (that is deployed in the Micro Integrator) to which you send a request.
Tip
Be sure to set up the CLI tool for your work environment as explained in the first step of this tutorial.
-
Open a terminal and execute the following command to start the tool:
mi
-
Log in to the CLI tool. Let's use the server administrator user name and password:
mi remote login admin admin
You will receive the following message: Login successful for remote: default!
-
Execute the following command to find the data services deployed in the server:
mi dataservice show
Read more about using the CLI tool.
Send the client request¶
Let's send a request to the API resource to make a reservation. You can use the embedded HTTP Client of WSO2 Integration Studio as follows:
-
Open the HTTP Client of WSO2 Integration Studio.
Tip
If you don't see the HTTP Client tab, go to Window -> Show View - Other and select HTTP Client to enable the tab.
-
Enter the request information as given below and click the Send icon ().
Method GET
URL http://localhost:8290/services/RDBMSDataService.HTTPEndpoint/Employee/3
If you want to send the client request from your terminal:
- Install and set up cURL as your REST client.
- 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 to your HTTP Client:
<Employees xmlns="http://ws.wso2.org/dataservice">
<EmployeeNumber>3</EmployeeNumber>
<FirstName>Edgar</FirstName>
<LastName>Code</LastName>
<Email>[email protected]</Email>
</Employees>
Top