com.datastax.driver.core
Class Cluster

java.lang.Object
  extended by com.datastax.driver.core.Cluster

public class Cluster
extends Object

Informations and known state of a Cassandra cluster.

This is the main entry point of the driver. A simple example of access to a Cassandra cluster would be:

   Cluster cluster = Cluster.builder().addContactPoint("192.168.0.1").build();
   Session session = cluster.connect("db1");

   for (Row row : session.execute("SELECT * FROM table1"))
       // do something ...
 

A cluster object maintains a permanent connection to one of the cluster node that it uses solely to maintain informations on the state and current topology of the cluster. Using the connection, the driver will discover all the nodes composing the cluster as well as new nodes joining the cluster.


Nested Class Summary
static class Cluster.Builder
          Helper class to build Cluster instances.
static interface Cluster.Initializer
          Initializer for Cluster instances.
 
Method Summary
static Cluster.Builder builder()
          Creates a new Cluster.Builder instance.
static Cluster buildFrom(Cluster.Initializer initializer)
          Build a new cluster based on the provided initializer.
 Session connect()
          Creates a new session on this cluster.
 Session connect(String keyspace)
          Creates a new session on this cluster and sets a keyspace to use.
 Configuration getConfiguration()
          The cluster configuration.
 Metadata getMetadata()
          Returns read-only metadata on the connected cluster.
 Metrics getMetrics()
          The cluster metrics.
 void shutdown()
          Shutdown this cluster instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

buildFrom

public static Cluster buildFrom(Cluster.Initializer initializer)
Build a new cluster based on the provided initializer.

Note that for building a cluster programmatically, Cluster.Builder provides a slightly less verbose shortcut with Cluster.Builder.build().

Also note that that all the contact points provided by initializer must share the same port.

Parameters:
initializer - the Cluster.Initializer to use
Returns:
the newly created Cluster instance
Throws:
NoHostAvailableException - if no host amongst the contact points can be reached.
IllegalArgumentException - if the list of contact points provided by initiazer is empty or if not all those contact points have the same port.
AuthenticationException - if while contacting the initial contact points an authencation error occurs.

builder

public static Cluster.Builder builder()
Creates a new Cluster.Builder instance.

This is a shortcut for new Cluster.Builder().

Returns:
the new cluster builder.

connect

public Session connect()
Creates a new session on this cluster.

Returns:
a new session on this cluster sets to no keyspace.

connect

public Session connect(String keyspace)
Creates a new session on this cluster and sets a keyspace to use.

Parameters:
keyspace - The name of the keyspace to use for the created Session.
Returns:
a new session on this cluster sets to keyspace keyspaceName.
Throws:
NoHostAvailableException - if no host can be contacted to set the keyspace.

getMetadata

public Metadata getMetadata()
Returns read-only metadata on the connected cluster.

This includes the know nodes (with their status as seen by the driver) as well as the schema definitions.

Returns:
the cluster metadata.

getConfiguration

public Configuration getConfiguration()
The cluster configuration.

Returns:
the cluster configuration.

getMetrics

public Metrics getMetrics()
The cluster metrics.

Returns:
the cluster metrics, or null if metrics collection has been disabled (see Configuration.isMetricsEnabled()).

shutdown

public void shutdown()
Shutdown this cluster instance. This closes all connections from all the sessions of this Cluster instance and reclaim all resources used by it.

This method has no effect if the cluster was already shutdown.



Copyright © 2013. All Rights Reserved.