This tutorial will guide you on how to expose data in a CSV file as a data service by using the Create New Data Service wizard. We will create a data service that can search for data on the file and insert data into the .
Note |
---|
Note that you can only read data from CSV files. That is, WSO2 EI does not support inserting, updating or modifying data in a CSV file. |
We will use the Products.csv
file that is shipped with WSO2 EI by default. The Products.csv
file stored in the <EI_HOME>/samples/data-services/resources
folder contains data about products (cars/motorocyles) that are manufactured in an automobile company. The data table has the following columns: "ID", "Name", "Classification" and "Price".
...
Log in to the management console of WSO2 EI using the following URL on your browser: "https://localhost:82439443/carbon/".
Click Create under the Data Service menu to open the Create Data Service window.
Enter CSV as the data service name as shown below. Leave the default values for the other fields.
Click Next to go to the Datasources screen.
...
- Click Add New Datasource and enter values as shown below.
- Follow the instructions below to fill the datasource details.
- Datasource Id: Enter an ID for you datasource. We will enter CSV as the datasource Id.
- Datasource Type: Select CSV as the datasource type from the list. You can now specify the details of the CSV file.
- CSV File Location : Specify the path to your CSV file. In this tutorial, we are using a sample CSV file that is stored in the following location of your product distribution:
./samples/data-services/resources/Products.csv
. - Column Separator: Add 'comma' as the column separator.
- Start Reading From Row: Specify 2 to start reading from the second row.
- Contains Column Header Row : Select Select true from from the list.
- Header row : Enter '21' as the value.
- Save the datasource.
- Click Click Next to to go to the the Queries screen screen.
Define a query for the datasource
...
- Click Add New Query to open the Add New Query screen.
- In the Query ID field, enter an identifier for the query. We will enter Q1.
- In the Datasource field, select the datasource for which you are going to write a query. Select the CSV datasource that you created previously.
- Now let's specify output mappings, which will determine how the result from your query will be presented when the query is invoked. The sample CSV datasource we are using contains three columns: ID, Name, Category and Price. We will create an output mapping for each of these columns.
- In the Output type field, 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.
- In the Grouped by element field, specify a grouping for all the output mappings. This will be the XML element that will group the query result. We will enter Products.
- In the Row Name field, specify the XML element that should group each individual result. Enter Product in this field.
- Click Add New Output Mapping to start creating the output mapping for the ID column. Enter values as shown below:
- Click Add to save the output mapping. You will now have one output mapping listed for the Q1 query.
Create output mappings for the remaining columns given below.
CategoryMapping Type Element Name Datasource Type Datasource Column Name Parameter Type Schema Type Element Category Column Classification SCALAR string Element Price Column Price SCALAR string Element Name Column Name SCALAR string
Info Find out more about defining Output Mappings.
- Click Next to go to the Operations screen.
...
- Click Add New Operation to open the following screen.
- In the Operation Name field, enter a name for the operation.
- In the Query ID field, select the query that you created.
- Save the operation.
...