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

Reading and writing to the Cassandra cluster


We're able to connect to our Cassandra server. Now let's write some data to it and read it back. The Session class provides a method called execute(), which is used to send read/write queries to Cassandra and get the result set back.

We can use CQL statements in the execute statement as strings. In the following example, we create a function that will create keyspace and a table for us. We're using CQL statements for creating keyspace and tables as string arguments to the execute method. Fetch the Session object by using the getSession() method created earlier, as follows:

public static void createSchema() {
  // create keyspace
  getSession().execute(
    "CREATE KEYSPACE IF NOT EXISTS apachecassandra "
    + "WITH replication = {'class':'SimpleStrategy', 'replication_factor':1 "
  + "};");
  // create table
  getSession().execute(
    "CREATE TABLE IF NOT EXISTS apachecassandra"
    + ".status_updates_by_user (" + "userid UUID,"
    + "updated_on...