the Cluster object is the main entry point of the driver. It holds the known state of the actual Cassandra cluster
(notably the Metadata). This class is thread-safe, you should create a single instance (per target
Cassandra cluster), and share it throughout your application;

the Session is what you use to execute queries. Likewise, it is thread-safe and should be reused;

we use execute to send a query to Cassandra. This returns a ResultSet, which is essentially a collection of Row
objects. On the next line, we extract the first row (which is the only one in this case);

we extract the value of the first (and only) column from the row;

finally, we close the cluster after we’re done with it. This will also close any session that was created from this
cluster. This step is important because it frees underlying resources (TCP connections, thread pools…). In a real
application, you would typically do this at shutdown (for example, when undeploying your webapp).

Note: this example uses the synchronous API. Most methods have asynchronous equivalents.

Creating an instance

Alternatively, you might want to retrieve the settings from an external source (like a properties file or a web
service). You’ll need to provide an implementation of Initializer that loads these settings:

Creation options

The only required option is the list of contact points, i.e. the hosts that the driver will initially contact to
discover the cluster topology. You can provide a single contact point, but it is usually a good idea to provide more, so
that the driver can fallback if the first one is down.

Cluster initialization

A freshly-built Cluster instance does not initialize automatically; that will be triggered by one of the following
actions:

an explicit call to cluster.init();

a call to cluster.getMetadata();

creating a session with cluster.connect() or one of its variants;

calling session.init() on a session that was created with cluster.newSession().

The initialization sequence is the following:

initialize internal state (thread pools, utility components, etc.);

try to connect to each of the contact points in sequence. The order is not deterministic (in fact, the driver shuffles
the list to avoid hotspots if a large number of clients share the same contact points). If no contact point replies,
a NoHostAvailableException is thrown and the process stops here;

otherwise, the successful contact point is elected as the control host. The driver negotiates
the native protocol version with it, and queries its system tables to discover the addresses of
the other hosts.

Note that, at this stage, only the control connection has been established. Connections to other hosts will only be
opened when a session gets created.

You might be tempted to open a separate session for each keyspace used in your application; however, note that
connection pools are created at the session level, so each new session will consume additional system
resources:

// Warning: creating two sessions doubles the number of TCP connections opened by the driverSessionsession1=cluster.connect("ks1");Sessionsession2=cluster.connect("ks2");

Also, there is currently a known limitation with named sessions, that causes the driver to
unexpectedly block the calling thread in certain circumstances; if you use a fully asynchronous model, you should use a
session with no keyspace.

Finally, if you issue a USE statement, it will change the default keyspace on that session:

Running queries

As shown here, the simplest form is to pass a query string directly. You can also pass an instance of
Statement.

Processing rows

Executing a query produces a ResultSet, which is an iterable of Row. The basic way to process all rows is to use
Java’s for-each loop:

for(Rowrow:rs){// process the row}

Note that this will return all results without limit (even though the driver might use multiple queries in the
background). To handle large result sets, you might want to use a LIMIT clause in your CQL query, or use one of the
techniques described in the paging documentation.

When you know that there is only one row (or are only interested in the first one), the driver provides a convenience
method:

Rowrow=rs.one();

Reading columns

Row provides getters to extract column values; they can be either positional or named:

Rowrow=session.execute("select first_name, last_name from users where id = 1").one();// The two are equivalent:StringfirstName=row.getString(0);StringfirstName=row.getString("first_name");

DataStax, Titan, and TitanDB are registered trademark of DataStax, Inc.
and its subsidiaries in the United States and/or other countries.

Apache Cassandra, Apache, Tomcat, Lucene, Solr, Hadoop, Spark, TinkerPop,
and Cassandra are trademarks of the Apache
Software Foundation or its subsidiaries in Canada, the United States
and/or other countries