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
Cassandra
release from http://cassandra.apache.org/download/- I downloaded release
1.1.4
- I downloaded release
- Create the directory
/usr/local/cassandra
and expand theTAR
in it- You now have
/usr/local/cassandra/apache-cassandra-1.1.4
as theCassandra home directory
- You now have
- Create the default
data
andlog
directories forCassandra
- These are
/var/lib/cassandra
and/var/log/cassandra
respectively - To use different directories, update the file
cassandra.yaml
in theconf
sub-directory
- These are
- Update
Cassandra
to listen for connections on the local machine’sIP address
instead of onlocalhost
- This is required to connect to
Cassandra
from aremote Java client
(on a separateWindows 7
machine in my case) - Open
cassandra.yaml
in theconf
sub-directory and update the variableslisten_address
andrpc_address
listen_address: 192.168.3.133
rpc_address: 192.168.3.133
- where
192.168.3.133
was the address of the machine runningCassandra
in my case. Use your machine’sIP address
in your setup.
- This is required to connect to
- (Optional) Set the name of your
Cassandra
cluster- Open
cassandra.yaml
in theconf
sub-directory and update the variablecluster_name
. E.g,cluster_name: 'Test Cluster'
- Open
- (May be Required) Handling an intermittent
Java exception
while starting or remotely connecting toCassandra
- In my case this happened because the
JVM
started byCassandra
did not have access to enoughmemory
- Update the file
cassandra-env.sh
in theconf
sub-directory- Change the line:
JVM_OPTS="$JVM_OPTS -Xss160k"
- Set the
–Xss
flag to a value higher than160
, e.g.,256
or512
- Change the line:
- In my case this happened because the
CentOS 6.3
includes afirewall
that is on by default and blocks incoming connections- Update the
firewall
configuration and allowport 9160
through for incoming connections9160
is the defaultRPC port
thatCassandra
listens on- You can change the port in the
cassandra.yaml
file - Access the
firewall
fromSystem / Administration / Firewall
in theCentOS menu
- Add the
port
underOther Ports
- Add the
- Update the
- That’s it!
Cassandra
is set up - Run
Cassandra
by executing the command./bin/cassandra
from theCassandra home directory
- You can also execute the command with the
-f
flag if you wantCassandra
to run in the background
- You can also execute the command with the
Running the Cassandra CLI
- Launch the
Cassandra CLI
by executing the command./bin/cassandra-cli
from theCassandra 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 thelocal IP address
instead 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 commands
by entering?
on the command line - Enter the command
show cluster name;
to verify yourcluster name
- 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
- I did this using
Pelops
which is a greatJava client
forCassandra
- You will need the following
JARs
apache-cassandra-thrift-1.1.4.jar
- From:
/usr/local/cassandra/apache-cassandra-1.1.4/lib
- From:
commons-pool-1.6.jar
libthrift-0.7.0.jar
- From:
/usr/local/cassandra/apache-cassandra-1.1.4/lib
- From:
log4j-1.2.16.jar
scale7-core-1.3.0.jar
scale7-pelops-1.3-1.1.x.jar
slf4j-api-1.7.0.jar
slf4j-log4j12-1.7.0.jar
uuid-3.3.jar
The Code
- Follow the tutorial here: https://github.com/s7/scale7-pelops
- I created a
Key Space
namedtest
and aColumn Family
namedUsers
- My sample
Java client
is 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();
}
}