This tutorial will guide you on how to expose data stored in Cassandra as a data service. Also, see the samples in Data Integration Samples.
Table of Contents
Prerequisites
Before starting the tutorial, set up a Cassandra environment as explained below.
...
Set up the Cassandra server with a keyspace and table for this tutorial:
Installing and starting Cassandra
A Cassandra server of version 3.0 should be already running in the default port. For instructions, go to Apache Cassandra Documentation.
Info |
---|
See the following links for setting up Cassandra as a data store:
|
Creating a
...
Now, let's start creating the data service from scratch:
...
Enter the following data service name.
...
Connecting to the datasource
Click Add New Datasource. and enter the following details:
...
The rest of the datasource properties are optional. See the descriptions given below.
...
title | Properties for a Cassandra datasource |
---|
...
keyspace and a table
Execute the SQL commands as given below.
Create the keyspace named UsersKS:
Code Block CREATE KEYSPACE UsersKS WITH replication = {'class':'SimpleStrategy', 'replication_factor':3};
Create the table named Users in the UsersKS keyspace:
Code Block CREATE TABLE UsersKS.Users (id uuid, name text, country text, age int, PRIMARY KEY (id));
Creating a data service
Now, let's start creating the data service from scratch:
- 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 CassandraDataService - Leave the default values for the other fields.
- Click Next to go to the Datasources screen
Connecting to the datasource
Creating a query to add a table to the Keyspace
Follow the steps given below.
Click Add New Datasource. and enter the following details:
Datasource ID Cassandra Datasource Type Select Cassandra as the datasource. Cassandra Server Enter localhost. The rest of the datasource properties are optional. See the descriptions given below.
Expand title Properties for a Cassandra datasource The following describes the properties supported by the Cassandra datasource.
Data Source Property Description Cassandra Servers* Host names of Cassandra servers in a comma-separated list. Keyspace The default key space used by the Cassandra session. Port The port of Cassandra servers. Cluster Name The Cassandra cluster name. Compression Compression used in communication. User Name The authenticating user's username. Password The authenticating user's password. Load Balancing Policy The client load balancing policy on how calls should be made to the provided servers. Enable JMX Reporting Enable JMX statistics for the connector. Enable Metrics Enable metrics for the connector. Local Core Connections Per Host Connection pooling: Local core connections per server host. Remote Core Connections Per Host Connection pooling: Remote core connections per server host.
Local Max.Connections Per HostAnchor Max_Con_Loc_Host Max_Con_Loc_Host Connection pooling: Maximum local connections per server host.
Remote Max.Connections Per HostAnchor Rem_Max_Con_Host Rem_Max_Con_Host Connection pooling: Remote max connections per server host. Local New Connection Threshold This property determines the threshold in the connection pool, which will trigger the creation of a new connection when the connection pool has not reached the maximum capacity for local hosts. Generally, it will not be required to change the default value for this parameter. Remote New Connection Threshold This property determines the threshold in the connection pool, which will trigger the creation of a new connection when the connection pool has not reached the maximum capacity for remote hosts. Generally, it will not be required to change the default value for this parameter. Local Max Requests Per Connection This property allows you to throttle the number of concurrent requests per connection for local hosts. Remote Max Requests Per Connection This property allows you to throttle the number of concurrent requests per connection for remote hosts. Protocol Version The native protocol version to use. By default, it auto connects. "2" for Cassandra versions 2.0 and upwards, and "1" for Cassandra version 1.2.x. Consistency Level The consistency level used for queries. Fetch Size Fetch size used by queries. Serial Consistency Level The serial consistency level used for queries. Reconnection Policy The reconnection policy used for the cluster. Constant Reconnection Policy Delay If "ConstantReconnectionPolicy" is used for Reconnection Policy, this represents the constant wait time between reconnection attempts in milliseconds. Exponential Reconnection Policy Base Delay If "ExponentialReconnectionPolicy" is used for Reconnection Policy, this represents the base delay in milliseconds. Exponential Reconnection Policy Max. Delay If "ExponentialReconnectionPolicy" is used for Reconnection Policy, this represents the maximum delay in milliseconds. Retry Policy Configured the retry policy in this cluster. Connection Timeout The socket connection timeout in milliseconds. Keep Alive Set if socket keeps alive. Read Timeout Set per host socket read timeout in milliseconds. Receive Buffer Size The socket receive buffer size. Send Buffer Size Thesocketsendbuffersize. Reuse Address The socket re-use address. So Linger The socket linger on value. TCP no Delay Set socket TCP to no delay. Enable SSL Enable SSL. Enable OData In WSO2 EI OData protocol version 4 (OASIS standards) is supported, which mainly provides support for CRUD operations. You can easily expose databases as an odata service by selecting this check box. Currently, Odata service feature support is available for RDBMS datasources and Cassandra datasources. If you have enabled Odata for your data service, you can complete the data service creation process. That is, you are not required to define queries or operations for the service. This Odata service will now be accessible from the following endpoints:
- For super tenant:
http://localhost:<port>/odata/{dataserviceName}/{datasourceId}/
- For normal tenants:
http://localhost:<port>/odata/t/{tenantId}/{dataserviceName}/{datasourceId}/
Disable Native Batch Requests Disables native Cassandrabatchrequests,and reverts to emulated batch requests. * Mandatory fields.
- For super tenant:
Click Add New Query and enter the following details:
In this field, enter the SQL statement describing the keyspace.
Code Block |
---|
CREATE TABLE UsersKS.Users (id uuid, name text, country text, age int, PRIMARY KEY (id)) |
Creating a query to a Keyspace to Cassandra
Follow the steps given below.
Click Add New Query and enter the following details:
...
In this field, enter the SQL statement describing the keyspace.
Code Block |
---|
CREATE KEYSPACE UsersKS WITH replication = {'class':'SimpleStrategy', 'replication_factor':3} |
...
batch requests. * Mandatory fields.
Creating a query to ADD user information
...
Click Add New Query and enter the following details:
Query ID Enter addUsers as the query ID. Datasource Select the datasource for which you are going to write a query. Select the Cassandra datasource that you created previously. SQL In this field, enter the SQL statement describing the data that should be added to the Cassandra datasource.
Code Block INSERT INTO UsersKS.Users (id, name, country, age) values (:id, :name, :country, :age)
Click Generate Input Mapping to create the input mappings.
- Edit the id column and change the SQL Type to UUID.
- Save the mapping.
- Edit the age column and change the SQL Type to integer INTEGER.
- Save the mapping and click Main Configuration to return to the query. You will now have the following input mappings:
- Save the query.
...
Click Add New Query and enter the following details:
Query ID Enter getUsersbyID as the query ID. Datasource Select the datasource for which you are going to write a query. Select the Cassandra datasource that you created previously. SQL In this field, enter the SQL statement describing the data that should be retrieved from the Cassandra datasource.
Code Block SELECT id, age, country, name FROM UsersKS.Users WHERE id = :id
Click Generate Input Mapping to create the input mapping. The id is the input as shown below.
Click Generate Response to create the output mapping. This defines how the employee details retrieved from the datasource will be presented in the result. Note that, by default, the output type is XML.
- Save the query.
- Click Next to open the Operations screen.
Creating SOAP operationa to invoke the queries
To invoke the query, you need to define an operation.
- Save the operation.
- Save the operation.
Click Add New Operation and enter the following information.
Operation Name addUsersOp Save the operation.Query ID addUsers create the input mapping. The id is the input as shown below.
- Edit the id column and change the SQL Type to UUID.
- Save the mapping and click Main Configuration to return to the query.
Click Generate Response to create the output mapping. This defines how the employee details retrieved from the datasource will be presented in the result. Note that, by default, the output type is XML.
- Save the query.
- Click Next to open the Operations screen.
Click Add New Operation and enter the following information.
Click Add New Operation and enter the following information.
Creating SOAP operationa to invoke the queries
Click Add New Operation and enter the following information.
Operation Name getUsersbyIDop Query ID getUsersbyID - Save the operation.
You can now invoke the data service query using SOAP.
Creating a REST resource to invoke the query
...
addUsersOp Query ID addUsers - Save the operation.
Click Add New ResourceOperation and enter the following information.
Resource Path Keyspace Resource Method POST Query ID createKS - Save the resource.
Click Add New Resource and enter the following information.
Resource Path Table Resource Method POST Save the resourceQuery ID createTable Operation Name getUsersbyIDOp Query ID getUsersbyID - Save the operation.
You can now invoke the data service query using SOAP.
Creating a REST resource to invoke the query
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 Usersusers Resource Method POST Query ID addUsers - Save the resource.
Click Add New Resource and enter the following information.
Resource Path Usersusers/{id} Resource Method GET Query ID getUsersbyID - Save the resource.
...
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 Cassandra data service. The TryIt Tool will open with the data service.
Create the Keyspace
Select the createKSOp operation you created earlier.
Click Send.
Create the table in the Keyspace
Select the createTableOp operation you created earlier.
Click Send.
default.
- Go to the Deployed Services screen.
- Click the Try this service link for the Cassandra data service. The TryIt Tool will open with the data service.
Post new data
- Select the addUsersOp operation you created earlier.
You need to provide the user details. Be sure to enter a UUID value as the user ID.
Click Send.
Get data
- Select the getUsersbyIDop getUsersbyIDOp operation you created earlier.
- Enter the UUID value that you entered as the user's ID previously.
Click Send to see the details of the user you added previously.
Invoking your data service using REST
The HTTP requests sent for each of the resources using cURL would be as follows:
Post new data
Create a file called
users-payload.xml file, and define the XML payload for posting new data as shown below. Note that the id is a UUID value.
Code Block <_
postusers>
<id>a05cdd7a-3d6a-11e8-b467-0ed5f89f718b</id> <name>Peter
Parker</
name>
<country>USA</
country>
<age>20</
age> </_
postusers>
Send the following HTTP request from the location where the
users-payload.xml file is stored:
Code Block curl -X POST -H 'Accept: application/xml' -H 'Content-Type: application/xml' --data "
@users-payload.xml" http://localhost:8280/services/
CassandraDataService/
users
Get data
The service can be invoked in REST-style via curl (http://curl.haxx.se). Shown below is the curl command to invoke the GET resource:
Code Block |
---|
curl -X GET http://localhost:8280/services/ RDBMSDataServiceCassandraDataService.HTTPEndpoint/Employee/3 users/a05cdd7a-3d6a-11e8-b467-0ed5f89f718b |