Step 1: Connecting to Cassandra from a web application

The simplest version of Playlist shows how to connect to Cassandra and display information from the system table.

The most basic version of Playlist doesn't have any real functionality, but does show how to connect to Cassandra using the Cassandra Java driver and retrieve information from the system table.

There are two Java classes that are used in this step to connect to a Cassandra node and retrieve information about the Cassandra cluster. The playlist.model.CassandraData class defines the getSession method used throughout Playlist to establish a connection to Cassandra, represented by the com.datastax.driver.core.Session object. CassandraData is the parent class of all the other model classes.

public static Session getSession() {

  if (cassandraSession == null) {
    cassandraSession = createSession();
  }

  return cassandraSession;

}

The getSession method calls the protected createSession method if there is no current Session object. The createSession method connects to the local Cassandra node and returns the Session object.

protected static Session createSession() {
  Cluster cluster = Cluster.builder().addContactPoint("localhost").build();
  return cluster.connect();
}

If you are connecting to a remote cluster, or are using a multi-node cluster, replace localhost with an IP address of the remote node in addContactPoint.

The playlist.model.CassandraInfo class stores information about the Cassandra cluster used by Playlist. CassandraInfo stores the cluster name and Cassandra version used by the cluster. The constructor retrieves this information using a CQL query run against the system table on the local Cassandra node.

public CassandraInfo() {

  Row row = getSession().execute("select cluster_name, release_version from system.local").one();
  cassandraVersion = row.getString("release_version");
  clusterName = row.getString("cluster_name");

}

The com.datastax.driver.core.Row class represents a row in a Cassandra table. The Row class provides methods for retrieving the column data by type. In this case, Row.getString() is used to retrieve the Cassandra version and cluster name.

The information stored in CassandraInfo is then made available to the view JSP files through the playlist.controller.HomeServlet controller. HomeServlet responds to HTTP GET requests and creates a new instance of CassandraInfo.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  String javaVersion = System.getProperty("java.version");
  CassandraInfo cassandraInfo = new CassandraInfo();

  request.setAttribute("java_version", javaVersion);
  request.setAttribute("cassandra_info", cassandraInfo);
  getServletContext().getRequestDispatcher("/home.jsp").forward(request,response);

}

HomeServlet then adds the information from CassandraInfo and the Java runtime version to the request headers before forwarding the response to the home.jsp view file to display to the user.

<h1>Playlist</h1>
<img class="dim" src="images/favicon.png"/>
Welcome to the playlist application. <br/>
Java version: ${java_version} <br/>
Cassandra version: ${cassandra_info.cassandraVersion} <br/>
Cluster name: ${cassandra_info.clusterName}

Prerequisites

You must have set up your environment before starting this step.

Procedure

  1. In a terminal, navigate to the playlist Git workspace.
    cd playlist
  2. Check out the step1 branch using git.
    git checkout step1
  3. Build and start the Playlist example application using mvn.

    This step compiles the Java source files, assembles the Playlist web application, and deploys it to a Jetty local instance.

    mvn verify cargo:run

    You will see the output of the Maven plugins as it compiles and assembles Playlist, and then the following when it is ready to be tested:

    [INFO] [talledLocalContainer] Jetty 9.2.11.v20150529 started on port [8080]
    [INFO] Press Ctrl-C to stop the container...
  4. In a web browser, go to: http://localhost:8080/playlist

    You will see the Java version and Cassandra cluster information displayed at the top of the page.

  5. Stop the Jetty instance by pressing Ctrl+C in your terminal.
    [INFO] Press Ctrl-C to stop the container...
    ^C[INFO] [talledLocalContainer] Jetty 9.2.11.v20150529 is stopping...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 34:26.970s
    [INFO] Finished at: Sat Jun 06 04:32:59 EDT 2015
    [INFO] Final Memory: 14M/183M
    [INFO] ------------------------------------------------------------------------
    ...

What's next

Proceed to Step 2: Adding artist and track features