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
What's next
Proceed to Step 2: Adding artist and track features