Database Reference
In-Depth Information
Writing your application
Cassandra provides the API for almost all the main stream programming languages. Devel-
oping applications for Cassandra is nothing more than actually executing CQL through an
API and collection result set or iterator for the query. This section will give you a glimpse
of the Java code for the example we discussed earlier. It uses the DataStax Java driver for
Cassandra. The full code is available at https://github.com/naishe/mastering-cassandra-v2 .
Getting the connection
An application creates a single instance of the Cluster object and keeps it for its life
cycle. Every time you want to execute a query or a bunch of queries, you ask for a ses-
sion object from the Cluster object. In a way, it is like a connection pool. Let's take a
look at the following example:
public class CassandraConnection {
private static Cluster cluster = getCluster();
public static final Session getSession(){
if ( cluster == null ){
cluster = getCluster();
}
return cluster.connect();
}
private static Cluster getCluster(){
Cluster clust = Cluster
.builder()
.addContactPoint(Constants.HOST)
.build();
return clust;
}
[-- snip --]
Executing queries
Query execution is barely different from what we did in the command prompt earlier:
private static final String BLOGS_TABLE_DEF =
"CREATE TABLE IF NOT EXISTS "
Search WWH ::




Custom Search