Connecting to a Cassandra cluster

The Java driver provides a Cluster class which is your client application's entry point for connecting to a Cassandra cluster and retrieving metadata.

Prerequisites

This tutorial assumes you have the following software installed, configured, and that you have familiarized yourself with them:

While Eclipse and Maven are not required to use the Java driver to develop Cassandra client applications, they do make things easier. You can use Maven from the command-line, but if you wish to use a different build tool (such as Ant) to run the sample code, you will have to set up your environment accordingly.

See for more information on setting up your programming environment to do this.

About this task

Using a Cluster object, the client connects to a node in your cluster and then retrieves metadata about the cluster and prints it out.

Procedure

  1. In the Eclipse IDE, create a simple Maven project. Use the following data:
    • groupId: com.example.cassandra
    • artifactId: simple-client
  2. Right-click on the simple-client node in the Project Viewer and select Maven > Add Dependency.
    1. Enter datastax in the search text field. You should see several DataStax libraries listed in the Search Results list.
    2. Expand the com.datastax.cassandra cassandra-driver-core library and select the version you want.
  3. Create a new Java class, com.example.cassandra.SimpleClient.
    1. Add an instance field, cluster, to hold Cluster reference.
      private Cluster cluster;
    2. Add an instance method, connect, to your new class.
      public void connect(String node) {}
      The connect method:
      • adds a contact point (node IP address) using the Cluster.Build auxiliary class
      • builds a cluster instance
      • retrieves metadata from the cluster
      • prints out:
        • the name of the cluster
        • the datacenter, host name or IP address, and rack for each of the nodes in the cluster
      public void connect(String node) {
         cluster = Cluster.builder()
               .addContactPoint(node).build();
         Metadata metadata = cluster.getMetadata();
         System.out.printf("Connected to cluster: %s\n", 
               metadata.getClusterName());
         for ( Host host : metadata.getAllHosts() ) {
            System.out.printf("Datacenter: %s; Host: %s; Rack: %s\n",
               host.getDatacenter(), host.getAddress(), host.getRack());
         }
      }
      
    3. Add an instance method, close, to shut down the cluster instance once you are finished with it.
      public void close() {
         cluster.shutdown();
      }
      
    4. In the class main method instantiate a SimpleClient object, call connect on it, and close it.
      public static void main(String[] args) {
         SimpleClient client = new SimpleClient();
         client.connect("127.0.0.1");
         client.close();
      }
      
  4. Right-click in the SimpleClient class editor pane and select Run As > 1 Java Application to run the program.

Code listing

The complete code listing illustrates:
package com.example.cassandra;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;

public class SimpleClient {
   private Cluster cluster;

   public void connect(String node) {
      cluster = Cluster.builder()
            .addContactPoint(node).build();
      Metadata metadata = cluster.getMetadata();
      System.out.printf("Connected to cluster: %s\n", 
            metadata.getClusterName());
      for ( Host host : metadata.getAllHosts() ) {
         System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
               host.getDatacenter(), host.getAddress(), host.getRack());
      }
   }

   public void close() {
      cluster.shutdown();
   }

   public static void main(String[] args) {
      SimpleClient client = new SimpleClient();
      client.connect("127.0.0.1");
      client.close();
   }
}

When run the client program prints out this metadata on the cluster's constituent nodes in the console pane:

Connected to cluster: xerxes
Datatacenter: datacenter1; Host: /127.0.0.1; Rack: rack1
Datatacenter: datacenter1; Host: /127.0.0.2; Rack: rack1
Datatacenter: datacenter1; Host: /127.0.0.3; Rack: rack1