This tutorial will guide you on how to expose data from a MongoDB datasource as an OData service using the ESB profile of WSO2 Enterprise Integrator (WSO2 EI).
Follow the steps given below. Also, see the samples in Data Integration Samples/wiki/spaces/EI6xx/pages/49616348.
Table of Contents
Install and start MongoDB
...
Download the product installer from here, and run the installer.
Insert excerpt Installing the Product Installing the Product nopanel true Start the ESB profile:
Panel borderColor #542989 bgColor #ffffff borderWidth 1 Localtabgroup Localtab active true title On MacOS/Linux/CentOS Open a terminal and execute the following command:
Code Block wso2ei-6.46.0-integrator
Localtab title On Windows Go to Start Menu -> Programs -> WSO2 -> Enterprise Integrator 6.46.0 Integrator. This will open a terminal and start the ESB profile. - Log in to the management console of ESB profile of WSO2 EI using the following URL on your browser: "https://localhost:9443/carbon/".
- Click Create under the Data Service menu to open the Create Data Service window.
Enter the following data service name.
Data Service Name MongoDB - Leave the default values for the other fields.
- Click Next to go to the Datasources screen
...
Click Add New Query and enter the following details:
Query ID Enter mongo_insert as the query ID. Datasource Select the datasource for which you are going to write a query. Select the mongo datasource that you created previously. SPARQLExpression In this field, enter the SPARQL query the query describing the data that should be retrieved from the RDF datasource. We will use the following query:
Code Block things.insert("{id:#, name:#}")
Now let's specify input mappings for the data in the MongoDB database. We will create output mappings for the id and name fields.
- Click Add New Input Mapping to start creating the input mapping. We want to add data into the database using the id and name fields. First, create the input mapping for id.
- Click Add to save the input mapping.
- Create the input mapping for the name field.
- Click Add New Input Mapping to start creating the input mapping. We want to add data into the database using the id and name fields. First, create the input mapping for id.
- Save the query.
...
Click Add New Query, and enter the following data:
Query ID Enter mongo_find as the query ID. Datasource Select the datasource for which you are going to write a query. Select the mongo datasource that you created previously. SPARQLExpression In this field, enter the SPARQL query the query describing the data that should be retrieved from the MongoDB datasource. We will use the following query:
Code Block things.find()
- Define Output Mapping: Now, let's specify how the data fetched from the datasource should be displayed in the output. We will create output mappings for the Data field.
Start by giving the following information:
Output type Specify the format in which the query results should be presented. You can select XML, JSON or RDF. We will use XML for this tutorial. Grouped by element Specify a grouping for all the output mappings. This will be the XML element that will group the query result. Enter Documents in this field. Row Name Specify the XML element that should group each individual result. Enter Document in this field. - Click Add New Output Mapping to start creating the output mapping. Enter the following values:
Click Main Configuration to return to the Query screen.
- Save the query, and click Next to go to the Operations screen.
...
Now, let's create REST resources to invoke the query created above. Alternatively, you can create SOAP operations to invoke the queries. See the previous section for instructions.
Click Add New Resource and enter the following information.
Resource Path insert/{id} Resource Method POST Query ID mongo_insert - Save the resource.
Click Add New Resource and enter the following information.
Resource Path find Resource Method GET Query ID mongo_find - Save the resource.
...
Code Block |
---|
https://localhost:8243/odata/{data_service_name}/{data_source_id}/{table_name}('_id') DELETE https://localhost:8243/odata/MongoODataSampleService/MongoID/PROJECT('5c29b6f577ec332a94d8fecc') Accept: application/json Content-Type: application/json { "location" : ”Kandy” } |
To invoke a particular query, send the HTTP request as shown below. In this example, we are going to retrieve the id and name of all the projects.
Code Block |
---|
https://localhost:8243/odata/{data_service_name}/{data_source_id}/{table_name}?$select={attribute_name_1, attribute_name_2} GET https://localhost:8243/odata/MongoODataSampleService/MongoID/project?$select=name,id Accept: application/json |
...