Nav

Cassandra Connector

Select

The Cassandra connector implements the DataStax Java Driver for Apache Cassandra 3.1. The connector exposes multiple operations in the Cassandra Connector Reference listed in the See Also section of this document. Some of the operations accept some custom POJOs as input parameters, which are needed for the DataSense functionality.

The Cassandra operations are: addNewColumn, changeColumnType, createKeyspace, createTable, deleteColumnsValue, deleteRows, dropColumn, dropKeyspace, dropTable, executeCQLQuery, getTableNamesFromKeyspace, renameColumn, select, and update,

Prerequisites

This document assumes that you are familiar with Cassandra, Mule, Anypoint Connectors, Anypoint Studio, Mule concepts, elements in a Mule flow, and Global Elements.

You need login credentials to test your connection to your target resource.

For compatibility information, see the Connector Release Notes.

To use this connector with Maven, view the pom.xml dependency information in the Dependency Snippets in Anypoint Exchange.

What’s New in this Connector

Support for Mule 4 has been added.

To Configure Cassandra Authentication

The Cassandra connector configuration needs to match the Cassandra configuration with regard to Basic Authentication. By default, accessing the Cassandra database does not require credentials. You need to configure Cassandra to enable authentication. After enabling authentication, you can run the cqlsh command from the Cassandra installation home directory as shown in this example that uses default credentials with username cassandra and password cassandra:


         
      
1
2
3
4
bin/cqlsh localhost -u cassandra -p cassandra
Connected to Test Cluster at localhost:9042.
[cqlsh 5.0.1 | Cassandra 3.10 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
  1. After installing Cassandra, open conf\cassandra.yaml file for editing.

  2. Optionally, change the value of AllowAllAuthenticator to PasswordAuthenticator.

  3. Restart Cassandra.

  4. In a separate terminal tab, check that Cassandra is running:

    bin/nodetool status

To Connect in Design Center

  1. In Design Center, click Set Up > Upload, browse for and select the driver for this connector on your file system, and upload it. Alternatively, search for and select a driver that is already uploaded.

  2. Click a trigger. You can create a global element by selecting this connector as they trigger. If a global element is not needed, you can use an HTTP Listener or a Scheduler as the trigger.

    Trigger options

  3. To create an HTTP global element for the connector, set these fields:

    HTTP Listener configuration

    Field Description

    Protocol

    Protocol selected for the HTTP endpoint, it can be HTTP or HTTPS (secure).

    Host

    IP address where the Mule application listens for requests.

    Port

    Port address where the Mule application listens for requests.

    Base Path

    Path where the Mule application listens for requests.

  4. Select the plus sign to add a component.

  5. Select the connector as a component.

  6. Select the operation.

    connector operations

    Operation Description

    Add New Column

    Input for this operation is:

    • Parameter: A table name

    • Optional keyspace name

    • Payload: an AlterColumnInput POJO

      The POJO has the following fields:

      • Name of the column to be created

      • The data type for the new column

    Change Column Type

    Input for this operation is:

    • Parameter: A table name

    • Optional keyspace name

    • Payload: an AlterColumnInput POJO

    Create Keyspace

    Input for this operation using SimpleStrategy is:

    • Keyspace name

    • Replication factor

    • Replication Strategy Class

    Create Table

    Input for this operation is:

    • Column names

    • Table names

    • Optional keyspace name

    Delete Columns Value

    Input for this operation is:

    • Parameter: A table name as a parameter

    • Optional keyspace name

    • Payload: a Map<String, Object> having two records with the keys where and columns

    Delete Rows

    Input for this operation is:

    • Parameter: A table name

    • Optional keyspace name

    • Payload a Map<String, Object> with one record having the key where and a value Map<String, Object> containing the WHERE clause.

    Drop Keyspace

    This operation accepts a String parameter representing the keyspace name.

    Drop Table

    Input for this operation is:

    • A table name

    • The keyspace that contains the table

    Execute CQL Query

    Transform Message Payload.

    Get Table Names from Keyspace

    Input for this operation is:

    Parameter: A table name as a parameter Payload: A Map<String, Object> representing the entity to be inserted into the table. ** Optional keyspace name

    Insert Examples

    Transform the Message Payload.

    Rename column

    Parameters for this operation are:

    • A table name

    • The old column name

    • The new column name

    Update

    Input for this operation is:

    • Parameter: A table name

    • Optional keyspace name

    • Payload: Map<String, Object> with two records having the keys where and columns.

  7. Configure the Global element for the connector.

    connector config

    Field Description

    Host

    Enter a host name, or IP address, of a Cassandra node.

    Port

    Enter a port number. The default port is 9042.

    Keyspace

    Enter the Cassandra keyspace. A keyspace groups column families.

    Username

    Enter a Cassandra user name if you enabled PasswordAuthenticator in the Cassandra YAML file. If AllowAllAuthenticator is enabled, leave this value blank.

    Password

    Enter the password if you enabled PasswordAuthenticator; otherwise, leave this value blank.

  8. Optionally configure a connection to a Cassandra cluster.

    connector cluster

    Field Description

    Cluster name

    The Cassandra cluster name.

    Protocol version

    The version of the native protocol supported by the driver.

    Max schema agreement wait seconds

    The maximum time to wait for schema agreement before returning from a DDL query.

    Compression

    The compression to use for the transport.

    SSL

    Enable or disable the use of SSL for the cluster.

To Connect in Anypoint Studio 7

You can use this connector in Anypoint Studio by adding it as a dependency in your Mule application.

To Install this Connector in Studio

  1. Open your Mule project in Anypoint Studio.

  2. Add the connector as a dependency in the pom.xml file:

    
                 
              
    1
    2
    3
    4
    5
    6
    
    <dependency>
      <groupId>org.mule.connectors</groupId>
      <artifactId>mule-module-cassandradb</artifactId>
      <version>3.0.0</version>
      <classifier>mule-plugin</classifier>
    </dependency>

To Configure in Anypoint Studio

  1. Drag and drop the operation connector to the Studio canvas.

    Operation Description

    Add New Column

    Input for this operation is:

    • Parameter: A table name

    • Optional keyspace name

    • Payload: an AlterColumnInput POJO

      The POJO has the following fields:

      • Name of the column to be created

      • The data type for the new column

    Change Column Type

    Input for this operation is:

    • Parameter: A table name

    • Optional keyspace name

    • Payload: an AlterColumnInput POJO

    Create Keyspace

    Input for this operation using SimpleStrategy is:

    • Keyspace name

    • Replication factor

    • Replication Strategy Class

    Create Table

    Input for this operation is:

    • Column names

    • Table names

    • Optional keyspace name

    Delete Columns Value

    Input for this operation is:

    • Parameter: A table name as a parameter

    • Optional keyspace name

    • Payload: a Map<String, Object> having two records with the keys where and columns

    Delete Rows

    Input for this operation is:

    • Parameter: A table name

    • Optional keyspace name

    • Payload a Map<String, Object> with one record having the key where and a value Map<String, Object> containing the WHERE clause.

    Drop Keyspace

    This operation accepts a String parameter representing the keyspace name.

    Drop Table

    Input for this operation is:

    • A table name

    • The keyspace that contains the table

    Execute CQL Query Examples

    a Transform Message Payload.

    Get Table Names from Keyspace

    Input for this operation is:

    * Parameter: A table name as a parameter * Payload: a Map<String, Object> representing the entity to be inserted into the table. * Optional keyspace name

    Insert Examples

    Transform Message Payload.

    Rename column

    Parameters for this operation are:

    • A table name

    • The old column name

    • The new column name

    Update

    Input for this operation is:

    • Parameter: A table name

    • Optional keyspace name

    • Payload: Map<String, Object> with two records having the keys where and columns.

  2. Configure the global element for the connector.

    config connector

    Field Description

    Host

    Enter a host name, or IP address of a Cassandra node.

    Port

    Enter a port number. The default port is 9042.

    Keyspace

    Enter the Cassandra keyspace. A keyspace groups column families.

    Username

    Enter a Cassandra user name if you enabled PasswordAuthenticator in the Cassandra YAML file. If AllowAllAuthenticator is enabled, leave this value blank.

    Password

    Enter the password if you enabled PasswordAuthenticator; otherwise, leave this value blank.

  3. Optionally configure a connection to a Cassandra cluster.

    config connector

    Field Description

    Cluster name

    The Cassandra cluster name.

    Protocol version

    The version of the native protocol supported by the driver.

    Max schema agreement wait seconds

    The maximum time to wait for schema agreement before returning from a DDL query.

    Compression

    The compression to use for the transport.

    SSL

    Enable or disable the use of SSL for the cluster.

Use Case: Studio

This use case creates a Cassandra keyspace, which groups column families, and then creates a Cassandra table.

To Create a Keyspace

Create a Keyspace Studio Flow

  1. Create a new Mule Project in Anypoint Studio and fill in the Cassandra credentials in src/main/resources/mule-app.properties.

    
                 
              
    1
    2
    3
    4
    5
    
    config.host=<HOST>
    config.port=<PORT>
    config.keyspace=<KEY_SPACE>
    config.username=<USERNAME>
    config.password=<PASSWORD>
  2. Drag an HTTP connector onto the canvas and leave the default values for Host and Port and set the path to /test/createKeyspace.

    
                 
              
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    %dw 2.0
    output application/java
    ---
    {
      "keyspaceName": payload.keyspaceName,
      "replicationFactor": payload.replicationFactor,
      "replicationStrategyClass": payload.replicationStrategyClass
    } as Object {
      class : "org.mule.modules.cassandradb.api.CreateKeyspaceInput"
    }
  3. Drag a Cassandra component for Create Keyspace onto the canvas.

    Field Description

    Host

    Enter a host name, or IP address of a Cassandra node.

    Port

    Enter a port number. The default port is 9042.

    Keyspace

    Enter the Cassandra keyspace. A keyspace groups column families.

    Username

    Enter a Cassandra user name if you enabled PasswordAuthenticator in the Cassandra YAML file. If AllowAllAuthenticator is enabled, leave this value blank.

    Password

    Enter the password if you enabled PasswordAuthenticator; otherwise, leave this value blank.

    Note: Click the Test Connection option to confirm that Mule can connect with the Cassandra instance. If the connection is successful, click OK to save the configuration. Otherwise, review and correct any invalid parameters and test again.

  4. Run the application. In a browser, use the following URL to enter a query parameter for the keyspace name (ks_name), replication factor (rf), and replication strategy class (rs_class):

    http://localhost:8081/simple?ks_name=simple_keyspace&rf=3&rs_class=SimpleStrategy

To Create a Cassandra Table

Create a Cassandra Table Studio Flow

  1. Create a new Mule Project in Anypoint Studio and fill in the Cassandra credentials in src/main/resources/mule-app.properties.

    
                 
              
    1
    2
    3
    4
    5
    
    config.host=<HOST>
    config.port=<PORT>
    config.keyspace=<KEY_SPACE>
    config.username=<USERNAME>
    config.password=<PASSWORD>
  2. Drag an HTTP connector onto the canvas and leave the default values for Host and Port and set the path to /test/createTable.

  3. Drag a Transform Message component from the Mule palette to the right (process) side of the flow, select the component, and set the output payload. For example:

    
                 
              
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    %dw 2.0
    output application/java
    ---
    {
      "columns": payload.columns,
      "tableName": payload.tableName,
      "keyspaceName": payload.keyspaceName
    } as Object {
      class : "org.mule.modules.cassandradb.api.CreateTableInput"
    }
  4. Drag a Cassandra component for the Create Table operation onto the canvas.

    Field Description

    Host

    Enter a host name, or IP address, of a Cassandra node.

    Port

    Enter a port number. The default port is 9042.

    Keyspace

    Enter the Cassandra keyspace. A keyspace groups column families.

    Username

    Enter a Cassandra user name if you enabled PasswordAuthenticator in the Cassandra YAML file. If AllowAllAuthenticator is enabled, leave this value blank.

    Password

    Enter the password if you enabled PasswordAuthenticator; otherwise, leave this value blank.

    Note: Click the Test Connection option to confirm that Mule can connect with the Cassandra instance. If the client is successful, click OK to save the configuration. Otherwise, review or correct any invalid parameters and test again.

  5. Run the Mule app. In Postman, select POST. Select Body > Raw, select the JSON (application/json) MIME type, and enter the following table description using uppercase for data types.

    
                 
              
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    
    {
      "tableName": "users",
      "keyspaceName": "Excelsior",
      "columns":
      [
        {
          "name": "id",
          "type": "INT",
          "primaryKey": "true"
        },
        {
          "name": "username",
          "type": "TEXT",
          "primaryKey": "true"
        },
        {
          "name": "name",
          "type": "TEXT",
          "primaryKey": "false"
        },
        {
          "name": "email",
          "type": "TEXT",
          "primaryKey": "false"
        }
      ]
    }
  6. In Postman, click Send, and look for Status: 200 OK.

Use Case: XML


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:cassandra-db="http://www.mulesoft.org/schema/mule/cassandra-db"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core
http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/cassandra-db
http://www.mulesoft.org/schema/mule/cassandra-db/current/mule-cassandra-db.xsd">

  <configuration-properties file="mule-app.properties" />
  <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config">
    <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
  <cassandra-db:config name="CassandraDB_Config" doc:name="CassandraDB Config">
    <cassandra-db:connection host="${config.host}" port="${config.port}" keyspace="${config.keyspace}" username="${config.username}" password="${config.password}"/>
        </cassandra-db:config>
  <flow name="HTMLForm">
    <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/"/>
    <parse-template doc:name="Parse Template" location="form.html"/>
  </flow>
  <flow name="CreateKeyspace">
    <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/createKeyspace"/>
    <ee:transform doc:name="Transform Message">
      <ee:message >
        <ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
{
  "keyspaceName": payload.keyspaceName,
  "replicationFactor": payload.replicationFactor,
  "replicationStrategyClass": payload.replicationStrategyClass
} as Object {
  class : "org.mule.modules.cassandradb.api.CreateKeyspaceInput"
}]]></ee:set-payload>
      </ee:message>
    </ee:transform>
    <cassandra-db:create-keyspace doc:name="Create keyspace" config-ref="CassandraDB_Config"/>
    <ee:transform doc:name="Transform Message">
      <ee:message >
        <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
      </ee:message>
    </ee:transform>
  </flow>
  <flow name="CreateTable">
    <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/createTable"/>
    <ee:transform doc:name="Transform Message">
      <ee:message>
        <ee:set-payload><![CDATA[%dw 2.0
output application/java
---
{
  "columns": payload.columns,
  "tableName": payload.tableName,
  "keyspaceName": payload.keyspaceName
} as Object {
  class : "org.mule.modules.cassandradb.api.CreateTableInput"
}]]></ee:set-payload>
      </ee:message>
    </ee:transform>
    <cassandra-db:create-table doc:name="Create table" config-ref="CassandraDB_Config"/>
    <ee:transform doc:name="Transform Message">
      <ee:message>
        <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
      </ee:message>
    </ee:transform>
  </flow>
</mule>

See Also