Java Driver 1.0 for Apache Cassandra

Using a session to execute CQL statements

Once you have connected to a Cassandra cluster using a cluster object, you retrieve a session, which allows you to execute CQL statements to read and write data.

Prerequisites

This tutorial uses a CQL3 schema which is described in a post on the DataStax developer blog. Reading that post, could help with some of the new CQL3 concepts used here.

Getting metadata for the cluster is good, but you also want to be able to read and write data to the cluster. The Java driver lets you execute CQL statements using a session instance that you retrieve from the Cluster object. You will add code to your client for:

  • creating tables
  • inserting data into those tables
  • querying the tables
  • printing the results

Procedure

  1. Modify your SimpleClient class.
    1. Add a Session instance field.
      private Session session;
    2. Get a session from your cluster and store the reference to it. Add the following line to the end of the connect method:
      session = cluster.connect();

    You can execute queries by calling the execute method on your session object. The session maintains multiple connections to the cluster nodes, provides policies to choose which node to use for each query (round-robin on all nodes of the cluster by default), and handles retries for failed queries when it makes sense.

    Session instances are thread-safe and usually a single instance is all you need per application. However, a given session can only be set to one keyspace at a time, so one instance per keyspace is necessary. Your application typically only needs a single cluster object, unless you're dealing with multiple physical clusters.

  2. Add an instance method, createSchema, to the SimpleClient class implementation.
    public void createSchema() { }
  3. Add the code to create a new schema.
    1. Execute a statement that creates a new keyspace.

      Add to the createSchema method:

      session.execute("CREATE KEYSPACE simplex WITH replication " + 
            "= {'class':'SimpleStrategy', 'replication_factor':3};");
      

      In this example, you create a new keyspace, simplex.

    2. Execute statements to create two new tables, songs and playlists. Add to the createSchema method:
      session.execute(
            "CREATE TABLE simplex.songs (" +
                  "id uuid PRIMARY KEY," + 
                  "title text," + 
                  "album text," + 
                  "artist text," + 
                  "tags set<text>," + 
                  "data blob" + 
                  ");");
      session.execute(
            "CREATE TABLE simplex.playlists (" +
                  "id uuid," +
                  "title text," +
                  "album text, " + 
                  "artist text," +
                  "song_id uuid," +
                  "PRIMARY KEY (id, title, album, artist)" +
                  ");");
      
  4. Add an instance method, loadData, to the SimpleCient class implementation.
    public void loadData() { }
  5. Add the code to insert data into the new schema.
    session.execute(
          "INSERT INTO simplex.songs (id, title, album, artist, tags) " +
          "VALUES (" +
              "756716f7-2e54-4715-9f00-91dcbea6cf50," +
              "'La Petite Tonkinoise'," +
              "'Bye Bye Blackbird'," +
              "'Joséphine Baker'," +
              "{'jazz', '2013'})" +
              ";");
    session.execute(
          "INSERT INTO simplex.playlists (id, song_id, title, album, artist) " +
          "VALUES (" +
              "2cc9ccb7-6221-4ccb-8387-f22b6a1b354d," +
              "756716f7-2e54-4715-9f00-91dcbea6cf50," +
              "'La Petite Tonkinoise'," +
              "'Bye Bye Blackbird'," +
              "'Joséphine Baker'" +
              ");");
    
  6. In the class main method, add a call to the new createSchema method.
    client.createSchema();
  7. Add an instance method, querySchema, that executes a SELECT statement on the tables and then prints out the results.
    1. Add code to execute the query. Query the playlists table for one of the two records.
      ResultSet results = session.execute("SELECT * FROM simplex.playlists " +
              "WHERE id = 2cc9ccb7-6221-4ccb-8387-f22b6a1b354d;");
      
      The execute method returns a ResultSet that holds rows returned by the SELECT statement.
    2. Add code to iterate over the rows and print them out.
      System.out.println(String.format("%-30s\t%-20s\t%-20s\n%s", "title", "album", "artist",
             "-------------------------------+-----------------------+--------------------"));
      for (Row row : results) {
          System.out.println(String.format("%-30s\t%-20s\t%-20s", row.getString("title"),
          row.getString("album"),  row.getString("artist")));
      }
      System.out.println();
      
  8. In the class main method, add a call to the new querySchema method.
    client.querySchema();