Installing, Configuring, and Testing Apache Cassandra

Note

This article was written using Cassandra 1.1.4 on CentOS 6.3 with a remote Java Client on Windows 7 Ultimate

Setting up Cassandra

  1. Download the Cassandra release from http://cassandra.apache.org/download/
    • I downloaded release 1.1.4
  2. Create the directory /usr/local/cassandra and expand the TAR in it
    • You now have /usr/local/cassandra/apache-cassandra-1.1.4 as the Cassandra home directory
  3. Create the default data and log directories for Cassandra
    • These are /var/lib/cassandra and /var/log/cassandra respectively
    • To use different directories, update the file cassandra.yaml in the conf sub-directory
  4. Update Cassandra to listen for connections on the local machine’s IP address instead of on localhost
    • This is required to connect to Cassandra from a remote Java client (on a separate Windows 7 machine in my case)
    • Open cassandra.yaml in the conf sub-directory and update the variables listen_address and rpc_address
      • listen_address: 192.168.3.133
      • rpc_address: 192.168.3.133
      • where 192.168.3.133 was the address of the machine running Cassandra in my case. Use your machine’s IP address in your setup.
  5. (Optional) Set the name of your Cassandra cluster
    • Open cassandra.yaml in the conf sub-directory and update the variable cluster_name. E.g, cluster_name: 'Test Cluster'
  6. (May be Required) Handling an intermittent Java exception while starting or remotely connecting to Cassandra
    • In my case this happened because the JVM started by Cassandra did not have access to enough memory
    • Update the file cassandra-env.sh in the conf sub-directory
      • Change the line: JVM_OPTS="$JVM_OPTS -Xss160k"
      • Set the –Xss flag to a value higher than 160, e.g., 256 or 512
  7. CentOS 6.3 includes a firewall that is on by default and blocks incoming connections
    • Update the firewall configuration and allow port 9160 through for incoming connections
      • 9160 is the default RPC port that Cassandra listens on
      • You can change the port in the cassandra.yaml file
      • Access the firewall from System / Administration / Firewall in the CentOS menu
        • Add the port under Other Ports
  8. That’s it! Cassandra is set up
  9. Run Cassandra by executing the command ./bin/cassandra from the Cassandra home directory
    • You can also execute the command with the -f flag if you want Cassandra to run in the background

Running the Cassandra CLI

  1. Launch the Cassandra CLI by executing the command ./bin/cassandra-cli from the Cassandra home directory
    • You will see a Java exception org.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection refused
    • This is because you have configured Cassandra to listen on the local IP address instead of the default localhost
  2. On the CLI, enter the command connect <Local IP Address>/<RPC Port>;
    • In my case: connect 192.168.3.133/9160;
  3. You can review all the CLI commands by entering ? on the command line
  4. Enter the command show cluster name; to verify your cluster name
  5. Find out more about the CLI here: http://www.datastax.com/docs/1.1/dml/using_cli

Write and Run your Remote Java Client

Note

  1. I did this using Pelops which is a great Java client for Cassandra
  2. You will need the following JARs

The Code

  1. Follow the tutorial here: https://github.com/s7/scale7-pelops
    • I created a Key Space named test and a Column Family named Users
    • My sample Java client is below. I ran this in Eclipse Juno.
import java.util.List;

import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.scale7.cassandra.pelops.Bytes;
import org.scale7.cassandra.pelops.Cluster;
import org.scale7.cassandra.pelops.Mutator;
import org.scale7.cassandra.pelops.Pelops;
import org.scale7.cassandra.pelops.Selector;

public class Test {

  public static void main(String[] args) {

    Cluster cassandraCluster = new Cluster("192.168.3.133", 9160);
    Pelops.addPool("pool", cassandraCluster, "test");

    Mutator mutator = Pelops.createMutator("pool");
    mutator.writeColumns(
      "Users", "user",
      mutator.newColumnList(
        mutator.newColumn("name", "Dan"),
        mutator.newColumn("age", Bytes.fromInt(33))
      )
    );
    mutator.execute(ConsistencyLevel.ONE);

    Selector selector = Pelops.createSelector("pool");
    List columns = selector.getColumnsFromRow(
      "Users", "user", false, ConsistencyLevel.ONE);

    System.out.println("Name: " + Selector.getColumnStringValue(columns, "name"));
    System.out.println("Age: " + Selector.getColumnValue(columns, "age").toInt());

    Pelops.shutdown();

  }
}

References