Enterprise Java

Getting Started with Apache Cassandra and Java (Part I)

On this page, you’ll learn just enough to get started with NoSQL Apache Cassandra and Java, including how to install, try out some basic commands and what to do next.

Requirements

To follow this tutorial, you should already have a running Cassandra instance, and have gone through the 10 minute walkthrough here: http://planetcassandra.org/create-a-keyspace-and-table/.

If you’ve already reviewed part I, be sure to check out Getting Started with Apache Cassandra and Java Part II.

You should have the demo keyspace and schema still set up, we will be referring to it below.

Setup

For this demo, we’re going to be creating a simple console application.  Open a text editor and create a java file with a “GettingStarted” class and a single main method.

public class GettingStarted {
 
        public static void main(String[] args) {

We also need to download the driver jar file from the downloads page. Click here. Once it’s downloaded, you need to expand it in your working directory. Then we have to ensure we include it in the classpath when we compile our .java file.

For example:

javac -classpath cassandra-java-driver-2.0.2/cassandra-driver-core-2.0.2.jar:. GettingStarted.java

When we run the file:

java -classpath cassandra-java-driver-2.0.2/*:cassandra-java-driver-2.0.2/lib/*:. GettingStarted

Try it Out

All of our code will be going into our main method. First we need to create cluster and session instance fields to hold the references. A session will manage the connections to our cluster.

Cluster cluster;
Session session;

Connect to your instance using the Cluster.builder method. It will add a contact point and build a cluster instance. Get a session from your cluster, connecting to the “demo” keyspace.

// Connect to the cluster and keyspace "demo"
cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
session = cluster.connect("demo");

Now that you are connected to the “demo” keyspace, let’s insert a user into the “users” table

// Insert one record into the users table
session.execute("INSERT INTO users (lastname, age, city, email, firstname) VALUES ('Jones', 35, 'Austin', 'bob@example.com', 'Bob')");

Using the Java driver, we can easily pull the user back out

// Use select to get the user we just entered
ResultSet results = session.execute("SELECT * FROM users WHERE lastname='Jones'");
for (Row row : results) {
System.out.format("%s %d\n", row.getString("firstname"), row.getInt("age"));
}

Since it’s Bob’s birthday, we are going to update his age.

// Update the same user with a new age
session.execute("update users set age = 36 where lastname = 'Jones'");
// Select and show the change
results = session.execute("select * from users where lastname='Jones'");
for (Row row : results) {
System.out.format("%s %d\n", row.getString("firstname"), row.getInt("age"));
 
}

Now let’s delete Bob from the table. Then we can print out all the rows. You’ll notice that Bob’s information no longer comes back after being deleted (others might, if you have inserted users previously).

// Delete the user from the users table
session.execute("DELETE FROM users WHERE lastname = 'Jones'");
// Show that the user is gone
results = session.execute("SELECT * FROM users");
for (Row row : results) {
System.out.format("%s %d %s %s %s\n", row.getString("lastname"), row.getInt("age"),  row.getString("city"), row.getString("email"), row.getString("firstname"));
}

Make sure that the connection closes once you are done.

// Clean up the connection by closing it
cluster.close();
    }
}

CQL is very similar to SQL, in many cases the same syntax will work.  This makes querying for data very straightforward if you have a background with relational databases.

You have just managed to connect to a Cassandra cluster and perform queries against a live (local) database. Hopefully this demonstrates just how easy it is to use Cassandra using the Java driver.  A Gist of the complete console application for this sample is available on GitHub.

More Resources

Getting Started with Apache Cassandra and Java Part II.

Read the documentation for the Java driver.

Learn more about prepared statements for security and performance.

Read up more on CQL.

Rebecca Mills

Rebecca Mills is a Junior Evangelist at DataStax, the company that delivers Apache Cassandra™ to the enterprise. She is interested in the new Cassandra user experience and is involved making it more approachable for everyone. She holds a B.Sc in biochemistry from Memorial University of Newfoundland and has been known to go on a bit about genome analysis.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button