Contact Free trial Login

Cassandra Studio Configuration - Mule 4

To configure Anypoint Connector for Cassandra (Cassandra Connector) in Anypoint Studio:

  1. Add the connector to your project.

  2. Configure the connector.

  3. Configure an input source for the connector.

Add the Connector to Your Studio Project

Anypoint Studio provides two ways to add the connector to your Studio project: from the Exchange button in the Studio taskbar or from the Mule Palette view.

Add the Connector Using Exchange

  1. In Studio, create a Mule project.

  2. Click the Exchange icon (X) in the upper-left of the Studio task bar.

  3. In Exchange, click Login and supply your Anypoint Platform username and password.

  4. In Exchange, search for "cassandra".

  5. Select the connector and click Add to project.

  6. Follow the prompts to install the connector.

Add the Connector in Studio

  1. In Studio, create a Mule project.

  2. In the Mule Palette view, click (X) Search in Exchange.

  3. In Add Modules to Project, type "cassandra" in the search field.

  4. Click this connector’s name in Available modules.

  5. Click Add.

  6. Click Finish.

Authentication

If a Cassandra database has authentication enabled, you must provide credentials when accessing it from Cassandra Connector. Cassandra supports basic authentication only. To provide credentials to a Cassandra database, enter the corresponding values in the Cassandra Connector global element.

Create a Cassandra Keyspace using Studio

This example creates a Cassandra keyspace that groups column families, and then creates a Cassandra table.

Create a Keyspace

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

    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, then set the path to: /test/createKeyspace.

  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:

    %dw 2.0
    output application/java
    ---
    {
     "keyspaceName": payload.keyspaceName,
     "replicationFactor": payload.replicationFactor,
     "replicationStrategyClass": payload.replicationStrategyClass
    }
    as Object {
     class: "org.mule.modules.cassandradb.api.CreateKeyspaceInput"
    }
  4. 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.

  5. 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 retest.

  6. 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

Create a Cassandra Table

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

    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:

    %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.

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

  6. 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.

    {
      "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"
        }
      ]
    }
  7. In Postman, click Send, and look for Status: 200 OK.