Database Reference
In-Depth Information
Java
Though there are several Java drivers out there, the current driver that fully sup-
ports the CQL 3 Binary Protocol is the driver distributed by DataStax. Using your
preferred IDE, you need to first add com.datastax.driver as a dependency
to your project.
To start the example, we will first create an empty class with a private cluster
variable and import our required packages. Listing 9.1 outlines how to import the
required packages and create our sample class.
Listing 9.1 Creating the Sample Java Class
Click here to view code image
package com.example.cassandra;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
public class SampleApp {
private Cluster cluster;
private Session session;
}
Connecting
When connecting to the cluster, we need to specify only a single node. The driver
will automatically query for information about the cluster and build a connection
to each of the nodes in the cluster. Listing 9.2 shows how to connect to a cluster
and print out information about the connections to the cluster.
Listing 9.2 Using Java to Connect to a Cluster
Click here to view code image
public void connect(String node) {
cluster =
 
 
Search WWH ::




Custom Search