Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This tutorial will guide you on how to expose data stored in Cassandra as a data serviceAlso, 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.

...

titleProperties 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:

  1. Log in to the management console of ESB profile of WSO2 EI using the following URL on your browser:  "https://localhost:9443/carbon/".
  2. Click Create under the Data Service menu to open the Create Data Service window.
  3. Enter the following data service name.

    Data Service NameCassandraDataService
  4. Leave the default values for the other fields.
  5. 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 Query and enter the following details:

    Query IDEnter createTable as the query ID.DatasourceSelect 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.

    Code Block
    CREATE TABLE UsersKS.Users (id uuid, name text, country text, age int, PRIMARY KEY (id))
    Save the query

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}

...

Creating a query to ADD user information

...

  1. Click Add New Query and enter the following details:

    Query IDEnter addUsers as the query ID.
    DatasourceSelect 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)
  2. Click Generate Input Mapping to create the input mappings.


  3. Edit the id column and change the SQL Type to UUID.
  4. Save the mapping.
  5. Edit the age column and change the SQL Type to integer INTEGER.
  6. Save the mapping and click Main Configuration to return to the query. You will now have the following input mappings:

  7. Save the query.

...

  1. Click Add New Query and enter the following details:

    Query IDEnter getUsersbyID as the query ID.
    DatasourceSelect 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
  2. Click Generate Input Mapping to create the input mapping. The id is the input as shown below.

    Image Removed

  3. 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. 
    Image Removed

  4. Save the query.
  5. 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 NamecreateKSOpQuery IDcreateKS
  1. Save the operation.
  2. Click Add New Operation and enter the following information.

    Operation NamecreateTableOpQuery IDcreateTable
  3. Save the operation.
  4. Click Add New Operation and enter the following information.

    Operation NameaddUsersOpQuery IDaddUsersSave the operation.

    create the input mapping. The id is the input as shown below.

    Image Added

  5. Edit the id column and change the SQL Type to UUID.
  6. Save the mapping and click Main Configuration to return to the query.
  7. 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. 

    Image Added

  8. Save the query.
  9. Click Next to open the Operations screen.

Creating SOAP operationa to invoke the queries

  1. Click Add New Operation and enter the following information.

    Operation NamegetUsersbyIDop
    Query IDgetUsersbyID
  2. Save the operation.

You can now invoke the data service query using SOAP.

Creating a REST resource to invoke the query

...

  1. addUsersOp
    Query IDaddUsers
  2. Save the operation.
  3. Click Add New ResourceOperation and enter the following information.

    Resource PathKeyspaceResource MethodPOSTQuery ID createKS
  4. Save the resource.
  5. Click Add New Resource and enter the following information.

    Resource PathTableResource MethodPOSTQuery IDcreateTableSave the resource


    Operation NamegetUsersbyIDOp
    Query IDgetUsersbyID
  6. 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.

  1. Click Add New Resource and enter the following information.

    Resource PathUsersusers
    Resource MethodPOST
    Query ID addUsers
  2. Save the resource.
  3. Click Add New Resource and enter the following information.

    Resource PathUsersusers/{id}
    Resource MethodGET
    Query IDgetUsersbyID
  4. 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. 

  1. Go to the Deployed Services screen.
  2. Click the Try this service link for the Cassandra data service. The TryIt Tool will open with the data service.

Create the Keyspace

  1. Select the createKSOp operation you created earlier. 

  2. Click Send.

The UsersKS keyspace is now added to the database.

Create the table in the Keyspace

  1. Select the createTableOp operation you created earlier. 

  2. Click Send.

The Users keyspace is now added to the UsersKS keyspace

default. 

  1. Go to the Deployed Services screen.
  2. Click the Try this service link for the Cassandra data service. The TryIt Tool will open with the data service.

Post new data

  1. Select the addUsersOp operation you created earlier. 
  2. You need to provide the user details.  Be sure to enter a UUID value as the user ID.

  3. Click Send.

The data is now added to the database.

Get data

  1. Select the getUsersbyIDop getUsersbyIDOp operation you created earlier. 
  2. Enter the UUID value that you entered as the user's ID previously.
  3. Click Send to see the details of the user you added previously.

Excerpthiddentrue


Invoking your data service using REST

The HTTP requests sent for each of the resources using cURL would be as follows:

Post new data

  1. Create a file called

employee
  1. 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
    <_
postemployee> <EmployeeNumber>3</EmployeeNumber>
  1. postusers>
        
<FirstName>Will</FirstName>
  1. <id>a05cdd7a-3d6a-11e8-b467-0ed5f89f718b</id>
        <name>Peter 
<LastName>Smith<
  1. Parker</
LastName>
  1. name>
        
<Email>will@google.com<
  1. <country>USA</
Email>
  1. country>
        
<Salary>15500.0<
  1. <age>20</
Salary>
  1. age>
    </_
postemployee>
  1. postusers>
  2. Send the following HTTP request from the location where the

employee
  1. users-payload.xml file is stored:

    Code Block
    curl -X POST -H 'Accept: application/xml'  -H 'Content-Type: application/xml' --data "
@employee
  1. @users-payload.xml" http://localhost:8280/services/
RDBMSDataService
  1. CassandraDataService/
employee
  1. 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/
RDBMSDataService
CassandraDataService.HTTPEndpoint/
Employee/3
users/a05cdd7a-3d6a-11e8-b467-0ed5f89f718b