This topic explains the purpose of using custom datasources and how you can define custom datasource implementations using the management console. Alternatively, you can simply create datasources using the default RDBMS configuration provided in WSO2 products.
About custom datasources
Custom datasources allows you to define your own datasource implementation. There are two options for writing a custom datasource, and these two options cover most of the common business use cases as follows:
- Custom tabular datasources: Used to represent data in tables, where a set of named tables contain data rows that can be queried later. A tabular datasource is typically associated with an SQL data services query. This is done by internally using our own SQL parser to execute SQL against the custom datasource. You can use the org.wso2.carbon.dataservices.core.custom.datasource.TabularDataBasedDS interface to implement tabular datasources. For a sample implementation of a tabular custom datasource, see org.wso2.carbon.dataservices.core.custom.datasource.InMemoryDataSource . Also, this is supported in Carbon datasources with the following datasource reader implementation:
- Custom query datasources: Used when the datasource has some form of query expression support. Custom query datasources are implemented using the org.wso2.carbon.dataservices.core.custom.datasource.CustomQueryBasedDS interface. You can create any non-tabular datasource using the query-based approach. Even if the target datasource does not have a query expression format, you can create your own. For example, you can support any NoSQL type datasource this way. For a sample implementation of a query-based custom datasource, see org.wso2.carbon.dataservices.core.custom.datasource.EchoDataSource. This is supported in Carbon datasources with the following datasource reader implementation:
- InMemoryDSSample is a sample data service (shipped with DSS by default), which contains both datasource implementations (InMemoryDataSource and EchoDataSource) explained above. See a demonstration of this sample here.
- Also, you can find a sample configuration file containing the InMemoryDSSample in the
Creating custom datasources
You can create custom data sources as shown below.
- Go to the Configure tab on the management console and click Data Sources to open the Data Sources screen.
- Then click Add Data Source. The following screen will open:
- Enter "Custom" as the datasource type.
- In the Custom Data Source Type field, enter "DS_CUSTOM_TABULAR" (to store data in tables) or "DS_CUSTOM_QUERY" (to store non-tabular data accessed through a query).
- In the Name and Description fields, enter a unique name for the datasource.
- In the Configuration section, specify the xml configuration of the datasource. See the examples given below.
XML configuration for a custom tabular datasource (DS_CUSTOM_TABULAR type):
XML configuration for a custom query datasource (DS_CUSTOM_QUERY):
In the "init" methods of all custom datasources, user-supplied properties will be parsed to initialize the datasource accordingly. Also, a property named "__DATASOURCE_ID__", which contains a UUID to uniquely identify the current datasource, will be passed. This can be used by custom datasource authors to identify the datasources accordingly, such as datasource instances communicating within a server cluster for data synchronization.
After creating datasources, they appear on the Data Sources page. You canas needed by clicking the Edit or Delete links.