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.
Prerequisites
Before starting the tutorial, set up a Cassandra environment as explained below.
Install and start Cassandra
A Cassandra server of version 3.0 should be already running in the default port. For instructions, go to Apache Cassandra Documentation.
See the following links for setting up Cassandra as a data store:
- Get familiar with the basics of Cassandra, including the prerequisites for using Cassandra, by going through the following: https://wiki.apache.org/cassandra/GettingStarted.
- Get familiar with using the Cassandra CLI, by going through the following: http://wiki.apache.org/cassandra/CassandraCli.
- Functionality of the underlying connector used for the Cassandra datasource: https://www.datastax.com/documentation/developer/java-driver/2.0/index.html.
- Also, note that Cassandra requires the most stable version of Java 7 or 8 you can deploy; preferably the Oracle/Sun JVM.
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
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.
Creating a query to a Keyspace to Cassandra
Follow the steps given below.
Click Add New Query and enter the following details:
Query ID Enter createKS 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 keyspace.
CREATE KEYSPACE UsersKS WITH replication = {'class':'SimpleStrategy', 'replication_factor':3}
- Save the query.
Creating a query to add a table to the Keyspace
Follow the steps given below.
Click Add New Query and enter the following details:
Query ID Enter createTable 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 keyspace.
CREATE TABLE UsersKS.Users (id uuid, name text, country text, age int, PRIMARY KEY (id))
- Save the query.
Creating a query to ADD user information
Now let's write a query for adding user information to the Users table of the UsersKS keyspace in your Cassandra server.
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.
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 Type to UUID.
- Save the mapping.
- Edit the age column and change the Type to integer.
- Save the mapping and click Main Configuration to return to the query. You will now have the following input mappings:
- Save the query.
Creating a query to GET data
Now let's start writing a query for getting data from the 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 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.
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.
Click Add New Operation and enter the following information.
Operation Name createKSOp Query ID createKS - Save the operation.
Click Add New Operation and enter the following information.
Operation Name createTableOp Query ID createTable - Save the operation.
Click Add New Operation and enter the following information.
Operation Name addUsersOp Query ID addUsers - Save the operation.
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
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 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 Query ID createTable - Save the resource.
Click Add New Resource and enter the following information.
Resource Path Users Resource Method POST Query ID addUsers - Save the resource.
Click Add New Resource and enter the following information.
Resource Path Users/{id} Resource Method GET Query ID getUsersbyID - 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 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.
Post new data
- Select the addUsersOp operation you created earlier.
You need to provide the user details.
Click Send.
Get data
- Select the getUsersbyIDop operation you created earlier.
Click Send to see the details of the user you added previously.