Contact Us 1-800-596-4880

To Create a Cassandra Table

This procedure creates a table having a compound primary key, id and username, and clustering columns name and email.

  1. Start Cassandra and Anypoint Studio.

  2. Create a new Anypoint Studio project, and in the Package Explorer, open mule-app-properties. Assuming you installed Cassandra locally, add connection properties and values. For example:

    username=cassandra
    password=cassandra
    host=localhost
    keyspace=Excelsior
    port=9042
  3. Create a flow by dragging an HTTP connector from the Mule palette to the canvas, select the connector, set the Path in Basic Properties to /createtable and Allowed Methods to POST. In Connector Configuration, click Plus control.

  4. In HTTP Listener Configuration, set the following options:

    • Host = 0.0.0.0

    • Port = 8081

  5. 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 as follows:

    %dw 1.0
    %output application/java
    ---
    {
      "columns": payload.columns,
      "tableName": payload.tableName,
      "keyspaceName": payload.keyspaceName
    } as :object {
      class : "com.mulesoft.mule.cassandradb.metadata.CreateTableInput"
    }
  6. Drag a Cassandra connector from the Mule palette to the right of the Transform Message component, and select the connector. In Operation, select Create Table, and click Plus control.

  7. In CassandraDB Username/Password Connection, set the following options to placeholder values, and test the connections:

    • Username: ${username}

    • Password: ${password}

    • Host: ${host}

    • Port: ${port}

  8. 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"
        }
      ]
    }
  9. In Postman, click Send, and look for Status: 200 OK. On the cqlsh command line, check that the app created the table:

    use Excelsior;
    describe table users;

    Cassandra output:

    cassandra@cqlsh:excelsior> use Excelsior;
    cassandra@cqlsh:excelsior> describe table users;
    
    CREATE TABLE excelsior.users (
      id int,
      username text,
      email text,
      name text,
      PRIMARY KEY ((id, username))
    ) WITH bloom_filter_fp_chance = 0.01
    ...
View on GitHub