Exposing MongoDB as a Data Service
This tutorial will guide you on how to expose data from a MongoDB datasource as a data service using the ESB profile of WSO2 Enterprise Integrator (WSO2 EI).
Follow the steps given below. Also, see the samples in Data Integration Samples.
Install and start MongoDB
A MongoDB server v2.4.x or v2.2.x should be already running in the default port.
Create a collection as below in the command shell.
Creating a data service
Now, let's start creating the data service from scratch:
Download the product installer from here, and run the installer.
Let's call the installation location of your product the <EI_HOME> directory.If you installed the product using the installer, this is located in a place specific to your OS as shown below:
OS Home directory Mac OS /Library/WSO2/EnterpriseIntegrator/6.5.0
Windows C:\Program Files\WSO2\EnterpriseIntegrator\6.5.0\
Ubuntu /usr/lib/wso2/EnterpriseIntegrator/6.5.0
CentOS /usr/lib64/EnterpriseIntegrator/6.5.0
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
Connecting to the datasource
When you get to the Add New Data Source screen, click Add New Datasource to open the corresponding screen.
Start by entering the following values:
Datasource ID MongoDB Datasource Type MongoDB You can then specify the connection details to the MongoDB database you set up previously. The fields available for the MongoDB datasource type are as follows:
Servers Enter localhost. This can be a comma separated list of server hosts and ports where the database is running. E.g.: "localhost" - "125.10.5.3, 125.10.5.4" - "192.168.3.1:27017, 192.168.3.2:27017". Database Name Enter mydb. The name of the database to which you want to connect. Write Concern Select NONE from the list. The write concern value to control the write behavior as well as exception raising on error conditions:
Read Preference Select PRIMARY from the list. The read preference value, which describes how MongoDB clients route read operations to members of a replica set. It has the following options.
Auto Connect Retry Controls whether or not to connect. That is, the system retries to connect automatically. Connection Timeout Connection timeout in milliseconds. 0 is default and infinite. Max. Wait Max wait time of a blocking thread for a connection. Socket Timeout Socket timeout value. 0 is default and infinite. Connections per Host If the number of connections allowed per host is exceeded, further connections will be blocked. Threads Allowed to Block For Connection Multiplier The value in this field, multiplied by the connections per host, gives the maximum number of threads that may be waiting for a connection to become available from the pool. All further threads will get an exception. For example, if connections per host are 10 and the 'threads allowed to block for connection multiplier' is 5, up to 50 threads can wait for a connection.
Creating a query to POST data
Follow the steps given below.
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. SPARQL In this field, enter the SPARQL query describing the data that should be retrieved from the RDF datasource. We will use the following query:
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.
Creating a query to GET data
Now, let's start writing a query for getting data from the MongoDB datasource. The query will specify the data that should be fetched by this query, and the format that should be used to display data when the query is invoked.
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. SPARQL In this field, enter the SPARQL query describing the data that should be retrieved from the MongoDB datasource. We will use the following query:
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.
Creating SOAP operations to invoke the queries
To invoke the query, you need to define an operation.
Click Add New Operation and enter the following information.
Operation Name mongo_insert_op Query ID mongo_insert - Save the operation.
Click Add New Operation and enter the following information.
Operation Name mongo_find_op Query ID mongo_find - Save the operation.
You can now invoke the data service query using SOAP.
Creating REST resources to invoke the queries
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.
You can now invoke the data service query using REST.
Finish creating the data service
Once you have defined the operation, click Finish to complete the data service creation process. You will now be taken to the Deployed Services screen, which shows all the data services deployed on the server.
Invoking your data service using SOAP
You can try the data service you created by using the TryIt tool that is in your product by default.
- Go to the Deployed Services screen.
- Click the Try this service link for the MongoDB data service. The TryIt Tool will open with the MongoDB service.
- Select the mongo_insert_op operation and enter values for ID and Name.
- Click Send to execute the operation. The data will be inserted to the database.
- Now, select the mongo_find_op operation and click Send. The response will be published in the TryIt tool
Invoking your data service using REST
You can send an HTTP GET request to invoke the data service using cURL as shown below.
curl -X GET http://localhost:8280/services/MongoDB.HTTPEndpoint/find
This will return the response in XML.