Book Image

Apache Cassandra Essentials

By : Nitin Padalia
Book Image

Apache Cassandra Essentials

By: Nitin Padalia

Overview of this book

Apache Cassandra Essentials takes you step-by-step from from the basics of installation to advanced installation options and database design techniques. It gives you all the information you need to effectively design a well distributed and high performance database. You’ll get to know about the steps that are performed by a Cassandra node when you execute a read/write query, which is essential to properly maintain of a Cassandra cluster and to debug any issues. Next, you’ll discover how to integrate a Cassandra driver in your applications and perform read/write operations. Finally, you’ll learn about the various tools provided by Cassandra for serviceability aspects such as logging, metrics, backup, and recovery.
Table of Contents (14 chapters)
Apache Cassandra Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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...