Connecting to a Cassandra cluster
In order to make a connection with a Cassandra cluster, the Cluster
, Cluster.Builder
, and Session
classes are used. The Cluster
class can be used to connect to the cluster and fetch information about the cluster, like keyspaces, partitioner being used, all nodes in the cluster, the cluster name, and so on. While connecting, the class Cluster.Builder
is used to specify cluster configuration options such as contact points, socket options, load balancing policy, retry policy, and so on.
In our example we're using Cassandra driver version 2.1.4 following are dependency details for our code examples:
private void connect() { Cluster cluster = Cluster.builder() .addContactPoint("127.0.0.1") .build(); // session variable is an instace of Session Class session = cluster.connect(); }
In the following example, we connect to a cluster with a contact point at the local machine using loopback address...