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
- Download the
Cassandrarelease from http://cassandra.apache.org/download/- I downloaded release
1.1.4
- I downloaded release
- Create the directory
/usr/local/cassandraand expand theTARin it- You now have
/usr/local/cassandra/apache-cassandra-1.1.4as theCassandra home directory
- You now have
- Create the default
dataandlogdirectories forCassandra- These are
/var/lib/cassandraand/var/log/cassandrarespectively - To use different directories, update the file
cassandra.yamlin theconfsub-directory
- These are
- Update
Cassandrato listen for connections on the local machine’sIP addressinstead of onlocalhost- This is required to connect to
Cassandrafrom aremote Java client(on a separateWindows 7machine in my case) - Open
cassandra.yamlin theconfsub-directory and update the variableslisten_addressandrpc_addresslisten_address: 192.168.3.133rpc_address: 192.168.3.133- where
192.168.3.133was the address of the machine runningCassandrain my case. Use your machine’sIP addressin your setup.
- This is required to connect to
- (Optional) Set the name of your
Cassandracluster- Open
cassandra.yamlin theconfsub-directory and update the variablecluster_name. E.g,cluster_name: 'Test Cluster'
- Open
- (May be Required) Handling an intermittent
Java exceptionwhile starting or remotely connecting toCassandra- In my case this happened because the
JVMstarted byCassandradid not have access to enoughmemory - Update the file
cassandra-env.shin theconfsub-directory- Change the line:
JVM_OPTS="$JVM_OPTS -Xss160k" - Set the
–Xssflag to a value higher than160, e.g.,256or512
- Change the line:
- In my case this happened because the
CentOS 6.3includes afirewallthat is on by default and blocks incoming connections- Update the
firewallconfiguration and allowport 9160through for incoming connections9160is the defaultRPC portthatCassandralistens on- You can change the port in the
cassandra.yamlfile - Access the
firewallfromSystem / Administration / Firewallin theCentOS menu- Add the
portunderOther Ports
- Add the
- Update the
- That’s it!
Cassandrais set up - Run
Cassandraby executing the command./bin/cassandrafrom theCassandra home directory- You can also execute the command with the
-fflag if you wantCassandrato run in the background
- You can also execute the command with the
Running the Cassandra CLI
- Launch the
Cassandra CLIby executing the command./bin/cassandra-clifrom theCassandra home directory- You will see a
Java exceptionorg.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection refused - This is because you have configured
Cassandrato listen on thelocal IP addressinstead of the defaultlocalhost
- You will see a
- On the
CLI, enter the commandconnect <Local IP Address>/<RPC Port>;- In my case:
connect 192.168.3.133/9160;
- In my case:
- You can review all the
CLI commandsby entering?on the command line - Enter the command
show cluster name;to verify yourcluster name - Find out more about the
CLIhere: http://www.datastax.com/docs/1.1/dml/using_cli
Write and Run your Remote Java Client
Note
- I did this using
Pelopswhich is a greatJava clientforCassandra - You will need the following
JARsapache-cassandra-thrift-1.1.4.jar- From:
/usr/local/cassandra/apache-cassandra-1.1.4/lib
- From:
commons-pool-1.6.jarlibthrift-0.7.0.jar- From:
/usr/local/cassandra/apache-cassandra-1.1.4/lib
- From:
log4j-1.2.16.jarscale7-core-1.3.0.jarscale7-pelops-1.3-1.1.x.jarslf4j-api-1.7.0.jarslf4j-log4j12-1.7.0.jaruuid-3.3.jar
The Code
- Follow the tutorial here: https://github.com/s7/scale7-pelops
- I created a
Key Spacenamedtestand aColumn FamilynamedUsers - My sample
Java clientis below. I ran this inEclipse Juno.
- I created a
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();
}
}