Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

CassandraDB Connector

Community

The Anypoint™ Connector for Apache Cassandra allows Mule applications to access the data in Apache Cassandra database instances.

This document describes implementation examples using Anypoint Studio, Mule ESB’s graphical user interface, and includes configuration details for using the XML Editor. 

Prerequisites

This document assumes that you are familiar with Mule, Anypoint Connectors, and the Anypoint Studio Essentials. To increase your familiarity with Studio, consider completing one or more Anypoint Studio Tutorials. Further, this page assumes that you have a basic understanding of Mule flows and Mule Global Elements

Installing Cassandra Connector in Anypoint Studio

You can "test drive" a connector in Anypoint Studio using the instructions in Installing a Connector from Anypoint Exchange

Configuring the Cassandra Connector

To use the CassandraDB connector in your Mule application, you must configure:

  • A global CassandraDB element for use by all the CassandraDB connectors in an application. For more information, see Mule Global Elements.

  • Parameters of each CassandraDB connector instance in the application.

Follow these steps to configure CassandraDB connector in a Mule application:

  1. Click the Global Elements tab at the base of the canvas, then click Create.

  2. In the Choose Global Type menu, use the filter to locate and select CassandraDB, then click OK.

  3. Configure the parameters according to the table below

    Config

    Parameter Description

    Name

    Enter a name for the configuration so it can be referenced later by the config-ref parameter in the flow.

    Username

    Enter a Cassandra username. Use a dummy username if you have disabled authentication.

    Password

    Enter the password. Leave this field empty if you have disabled authentication.

    Host

    Enter host name or IP address of the application.

    Port

    Enter a port number. The default port is 9160.

    Keyspace

    Enter the Cassandra keyspace. A keyspace groups column families together.

    Consistency Level

    Specify the number of replicas that need to respond to a read request before returning data to an application. ONE is the default.

  4. Access the Pooling Profile tab to configure any settings relevant to managing multiple connections using a connection pool.

  5. Access the Reconnection tab to configure any settings relevant to reconnection strategies that Mule should execute if it loses its connection to Cassandra.

  6. Click OK to save the global connector configurations.

  7. Return to the Message Flow tab in Studio.

Configuration in a Flow

Follow these steps to configure the parameters of the CassandraDB connector in the flow:

  1. Drag the CassandraDB connector onto the canvas, then select it to open the Properties Editor console.

  2. Configure these connector parameters:

    Field Description Default

    Display Name

    Enter a unique label for the connector in your application.

    Cassandra DB

    Connector Configuration

    Connect to a global element linked to this connector. Global elements encapsulate reusable data about the connection to the target resource or service. Select the global CassandraDB connector element that you just created.

     — 

    Operation

    Select the action this component must perform.

     — 

  3. Save your configurations.

Include the CassandraDB namespaces in your configuration file:


    
            
         
1
2
3
4
5
6
7
8
9
10
11
12
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:cassandradb="http://www.mulesoft.org/schema/mule/cassandradb"
      xsi:schemaLocation="
               http://www.mulesoft.org/schema/mule/core
               http://www.mulesoft.org/schema/mule/core/current/mule.xsd
               http://www.mulesoft.org/schema/mule/cassandradb
               http://www.mulesoft.org/schema/mule/cassandradb/current/mule-cassandradb.xsd">
 
      <!-- Your flows and configuration elements -->
 
</mule>

Follow these steps to configure a CassandraDB connector in your application:

  1. Create a global CassandraDB configuration outside and above your flows, using this global configuration code:

    Build your application flow, then add a CassandraDB connector using one of the following operations.

    The following table provides details on each operation:  

    Operation Description

    <cassandradb:add>

    Increments a CounterColumn consisting of (name, value) at the given ColumnParent.

    <cassandradb:batch-mutable>

    Executes the specified batch mutations on the keyspace.

    <cassandradb:describe-cluster-name>

    Gets the name of the cluster.

    <cassandradb:describe-keyspace>

    Gets information about the specified keyspace.

    <cassandradb:describe-keyspaces>

    Gets a list of all the keyspaces configured for the cluster.

    <cassandradb:describe-partitioner>

    Gets the name of the partitioner for the cluster.

    <cassandradb:describe-ring>

    Gets the token ring; a map of ranges to host addresses.

    <cassandradb:describe-schema-versions>

    Returns a list of nodes per version for each schema version present in a cluster.

    <cassandradb:describe-snitch>

    Gets the name of the snitch used for the cluster. A snitch indicates which datacenter and rack that data is written to and from.

    <cassandradb:describe-version>

    Gets the Thrift API version.

    <cassandradb:execute-cql-query>

    Executes a CQL (Cassandra Query Language) statement and returns a CqlResult containing the results.

    <cassandradb:get>

    Gets Column or SuperColumn by the path.

    <cassandradb:get-count>

    Counts the columns present in column_parent within the predicate.

    <cassandradb:get-indexed-slices>

    Returns a list of slices, but uses IndexClause instead of KeyRange.

    <cassandradb:get-range-slices>

    Replaces get_range_slices.

    <cassandradb:get-row>

    Gets Column or SuperColumn by the path.

    <cassandradb:get-slice>

    Gets the group of columns contained by column_parent (either a ColumnFamily name or a ColumnFamily and SuperColumn name pair) specified by the given SlicePredicate (start, finish, reversed and count) parameters.

    <cassandradb:insert>

    Inserts a Column consisting of name, value, timestamp, and ttl (time to live) for a ColumnParent.

    <cassandradb:insert-from-map>

    Inserts an object into the database.

    <cassandradb:multiget-count>

    Provides a combination of multiget_slice and get_count.

    <cassandradb:multiget-slice>

    Retrieves slices for column_parent and predicate on each of the given keys in parallel.

    <cassandradb:remove>

    Removes data from a row specified by a key at the granularity specified by column_path, and the given timestamp.

    <cassandradb:remove-counter>

    Removes a counter from the row specified by a key at the granularity specified by column_path.

    <cassandradb:set-query-keyspace>

    Sets the keyspace to use for subsequent requests.

    <cassandradb:system-add-column-family-from-object>

    Adds a column family from an object.

    <cassandradb:system-add-column-family-from-object-with-simple-names>

    Adds a column family from an object that has a simple name.

    <cassandradb:system-add-column-family-with-params>

    Adds a column family to the current keyspace.

    <cassandradb:system-add-keyspace-from-object>

    Creates a new keyspace and any column families defined with it.

    <cassandradb:system-add-keyspace-with-params>

    Creates a new keyspace with the provided name with all the defaults values

    <cassandradb:system-drop-column-family>

    Drops a column family.

    <cassandradb:system-drop-keyspace>

    Drops a keyspace.

    <cassandradb:system-update-column-family>

    Updates properties of a ColumnFamily.

    <cassandradb:system-update-keyspace>

    Updates properties of a keyspace.

    <cassandradb:truncate>

    Removes all the rows from a column family.

Example Use Case

Adds a new keyspace in the Apache Cassandra database with default values. A keyspace groups column families together.

cassandra

  1. Drag an HTTP Connector into a new flow, open it’s properties editor and Create a new Connector Configuration element by clicking the green plus sign.

  2. Fill in the two required fields: Host and Port. Set the host to localhost and leave the port as the default value 8081. Also set the Path to cassandra

    Field Value

    Name

    HTTP_Listener_Configuration

    Host

    localhost

    Port

    8081

    Path

    cassandra

  3. Back in the connector’s properties editor, set the path to addKeyspace

  4. Drag the CassandraDB connector onto the canvas, then select it to open the properties editor console.

  5. Click the + sign next to the Connector Configuration field to add a new global connector configuration:

    addconfig

  6. Configure the global element:

    Field Value

    Name

    CassandraDB (or any other name you prefer)

    Username

    <Your Cassandra username> (You can use a dummy username if you disabled authentication)

    Password

    <Your Cassandra password> ( You can leave this element blank if you disabled authentication)

    Host

    localhost

    Port

    9160 (default is 9160)

    Keyspace

    <Cassandra Keyspace>

    Consistency Level

    ONE (default)

  7. In the properties editor of the CassandraDB connector, configure the remaining parameters:

    cassandra+conf+1

    Field Value

    Display Name

    Add-Keyspace (or any other name you prefer)

    Connector  Configuration

    CassandraDB (name of the global element you have created)

    Operation

    System adds a keyspace with parameters

    Keyspace Name

    #[message.inboundProperties.'http.query.params'.keyspace]

  8. Run the project as a Mule Application (right-click project name, then select Run As > Mule Application).

  9. From a browser, navigate to` http://localhost:8081/?keyspace=`<keyspacename> 

  1. Add a cassandradb:config element to your project, then configure its attributes according to the table below.

    
           
                   
                
    1
    
    &lt;cassandradb:config name="Cassandradb" username="dummy"  keyspace="system" doc:name="Cassandradb"/&gt;
    Attribute Value

    name

    CassandraDB

    doc:name

    CassandraDB

    username

    <Your Cassandra username>

    keyspace

    system

  2. Create a Mule flow with an HTTP endpoint, configuring the endpoint according to the table below.

    
           
                   
                
    1
    
    &lt;http:listener config-ref="HTTP_Listener_Configuration" path="addKeyspace" doc:name="HTTP"/&gt;
    Attribute Value

    config-ref

    HTTP_Listener_Configuration

    path

    addKeyspace

    doc:name

    HTTP

  3. The config-ref attribute in the connector references a global element, you must now create this global element outside your flow.

    
           
                   
                
    1
    
    &lt;http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" basePath="cassandra" doc:name="HTTP Listener Configuration"/&gt;
    Attribute Value

    name

    HTTP_Listener_Configuration

    host

    localhost

    port

    8081

    basePath

    cassandra

    doc:name

    HTTP Listener Configuration

  4. Add a cassandradb:system-add-keyspace-with-params element to your flow, configuring the attributes according to the table below.

    
           
                   
                
    1
    2
    
    &lt;cassandradb:system-add-keyspace-with-params config-ref="Cassandradb" keyspaceName="#[message.inboundProperties.'http.query.params'.keyspace]"  doc:name="Add-Keyspace"&gt;
            &lt;/cassandradb:system-add-keyspace-with-params&gt;
    Attribute Value

    config-ref

    Cassandradb

    keyspaceName

    #[message.inboundProperties.'http.query.params'.keyspace]

    doc:name

    Add-Keyspace

  5. Run the project as a Mule Application (right-click project name, then select Run As > Mule Application).

  6. From a browser, navigate to` http://localhost:8081/?keyspace= `<keyspacename>

  7. Mule conducts the query, and adds the keyspace with the specified name.

Example Code


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:cassandradb="http://www.mulesoft.org/schema/mule/cassandradb" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/cassandradb http://www.mulesoft.org/schema/mule/cassandradb/current/mule-cassandradb.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
 
    <cassandradb:config name="Cassandradb" username="dummy" keyspace="system" doc:name="Cassandradb"/>
    <cassandradb:config name="CassandradbNuevo" username="dummy"  keyspace="NewUserKeyspace" doc:name="Cassandradb"/>
 
    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" basePath="cassandra" doc:name="HTTP Listener Configuration"/>
    <flow name="AddKeyspace" doc:name="AddKeyspace">
        <http:listener config-ref="HTTP_Listener_Configuration" path="addKeyspace" doc:name="HTTP"/>
        <cassandradb:system-add-keyspace-with-params config-ref="Cassandradb" keyspaceName="#[message.inboundProperties.'http.query.params'.keyspace]" doc:name="Add-Keyspace">
        </cassandradb:system-add-keyspace-with-params>
        <set-payload value="New keyspaces #[message.inboundProperties.'http.query.params'.keyspace] was added. New schema key #[payload]" doc:name="Set Payload"/>
    </flow>
    <flow name="Batch-mutable" doc:name="Batch-mutable">
        <http:listener config-ref="HTTP_Listener_Configuration" doc:name="HTTP"/>
        <cassandradb:add config-ref="" doc:name="Cassandradb" columnParent="" counterName="" counterValue="" rowKey=""/>
    </flow>

Note: In this code example, spring-beans-current.xsd is a placeholder. To locate the correct version, see http://www.springframework.org/schema/beans/[http://www.springframework.org/schema/beans/].

See Also