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

Get Apache Cassandra Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.