Difficulty: Beginner
Estimated Time: 5 - 10 minutes

The Datastax Java Driver for Apache Cassandra™ is a modern, feature-rich and highly tunable Java client library for Apache Cassandra® (2.1+) and DataStax Enterprise (4.7+), and DataStax Astra, using exclusively Cassandra’s binary protocol and Cassandra Query Language (CQL) v3.

In this scenario, you will:

  • Connect to an Apache Cassandra™ instance with the DataStax Java driver
  • Use the driver to execute CQL statements against Apache Cassandra™ that do the following:
    • Create the a keyspace and table programmatically
    • Insert a user into a table
    • Select that user back out
    • Update the user's email
    • Delete the user from the table
  • Process CQL query results programmatically

Congratulations! You now have the basic knowledge of using the Java driver for Apache Cassandra™.

Get your hands on the DataStax Drivers at DataStax Downloads

Help us make this better! Please give this scenario a ranking (click on the stars above).

tshirt

Try It Out: DataStax Java Driver for Apache Cassandra™

Step 1 of 6

Java project Setup

Interact with Cassandra in the Java programming language using The DataStax Java Driver! This short scenario walks through using CRUD operations against a Cassandra instance in Java.

Wait until Cassandra has finished starting up. Enter the quickstart-java directory. The Maven build automation tool manages our dependencies.

cd ~/quickstart-java

The pom.xml file declares the dependencies , including the latest version of Java driver.

<dependency>
  <groupId>com.datastax.oss</groupId>
  <artifactId>java-driver-core</artifactId>
  <version>${driver.version}</version>
</dependency>

Review the dependencies with cat pom.xml if you wish, otherwise continue with connecting to Cassandra.

To make things easy, we will be using jshell. The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. Start jshell from the project directory.

mvn jshell:run

Once jshell is running, you are going to need to import the CqlSession class to connect to the cluster, as well as the cql package:

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.*;


Now that Apache Cassandra is running, let's configure our connection to the instance.