Cassandra Interfaces Tutorial

6.1 Lesson 6-Cassandra Interfaces

Hello and welcome to the sixth lesson of the Apache CassandraTM course offered by Simplilearn. This lesson will provide information on Cassandra interfaces.

6.2 Course Map

The Apache Cassandra™ course by Simplilearn is divided into eight lessons, as listed. • Lesson 0—Course Overview • Lesson 1—Overview of Big Data and NoSQL Database • Lesson 2—Introduction to Cassandra • Lesson 3—Cassandra Architecture • Lesson 4—Cassandra Installation and Configuration • Lesson 5—Cassandra Data Model • Lesson 6—Cassandra Interfaces • Lesson 7—Cassandra Advanced Architecture and Cluster Management • Lesson 8—Hadoop Ecosystem around Cassandra This is the sixth lesson, ‘Cassandra Interfaces.’

6.3 Objectives

After completing this lesson, you will be able to list the various interfaces to Cassandra, describe the command line interface in Cassandra and describe options and commands in the Cassandra command line interface. You will also be able to explain the Java interface to Cassandra, write a Java program to connect to Cassandra, and explain the steps to compile and run a Java Program for Cassandra.

6.4 Cassandra Interfaces

There are many interfaces with which you can access data from Cassandra, such as: The command line interface known as Cassandra Query Language shell or cqlsh, the Java interface called DataStax Driver, and the ODBC, PHP, Python, Ruby, and C# and .Net interfaces. In this lesson, you will learn about the command line, Java, and ODBC interfaces for Cassandra.

6.5 Cassandra Command Line Interface

You have already learned about connecting to Cassandra using the command line interface. The command line interface in Cassandra is called cqlsh. In cqlsh, the host and port connect to Cassandra using the CQLSH_HOST and CQLSH_PORT environment variables respectively. The default host is localhost or and the default port is 9160. Cqlsh is part of the package installation and installed to a bin directory on Linux. Its working is also similar to that of the Linux shell. Cqlsh supports the Cassandra Query Language or CQL. Start cqlsh using the command cqlsh. It gives the Cassandra cqlsh prompt as output.

6.6 Cqlsh Options

Cqlsh supports many options. The table shows some of these. cqlsh -h or --help displays helpful information on various cqlsh commands. Cqlsh --version shows the version number of the cqlsh program. To authenticate using username, type --username= and enter the username. To authenticate using password, type --password= and enter the password. --Keyspace can be used to specify a particular keyspace on login. You can execute the commands from a file using --file=FILENAME and execute a given statement using the –e statement.

6.7 Cqlsh Commands

You can also use various commands to interact with cqlsh. The cqlsh prompt is shown on entry. Keyspace name is appended to the prompt after you change the keyspace. Triple dots signify the secondary prompt. The secondary prompt is shown when the command is not complete and more input of semicolon is expected. Use // and /* */ to provide comments, semicolon to end a command, and arrows keys to see the history of commands. The diagram shows the command prompt on entering cqlsh and the changed command prompt after changing to keyspace testDB. It also shows the secondary prompt when a partial statement, in this case ‘select * from stocks‘, is issued. The query is executed when you type the rest of the command and enter a semicolon at the end.

6.8 Cqlsh Shell Commands

Cqlsh supports commands written in Cassandra query language or CQL. Apart from CQL commands, cqlsh also supports a few of its own commands. These commands are explained in the table shown. The CAPTURE command is used to append the command output to a given file. It can be turned off with CAPTURE OFF. The COPY command is used to import and export data. The DESCRIBE command is used to provide information on the cluster, keyspaces, and tables. HELP provides detailed help on many CQL and cqlsh commands. SOURCE can be used to execute commands from a file. Lastly, EXIT is used to exit from cqlsh.

6.9 Cqlsh Help

Let us discuss the help command in this screen. The help command in cqlsh can be used to get helpful information on a particular command. If you just type help without any arguments, you can view commands for which help is available. If you type help with an argument, you can view information on that particular entity. For example, if you want to receive detailed help for the update command, you can issue a command as shown. It shows the command’s usage, description, and other related help topics available. Note that many Cassandra documents refer to tables as columnFamily.

6.10 Java Interfaces

You learned about the Command Line Interface, cqlsh, until now. Next, we will discuss the Java interface. In Cassandra, the Java interface is provided by the DataStax Java Driver. It uses CQL binary protocol and provides asynchronous processing so that clients do not have to wait. It also provides automatic nodes discovery. This means the driver finds and uses all the nodes in the cluster. The Java driver is also fault tolerant. If a Cassandra node processing the request fails, the driver will automatically connect to other nodes in the cluster. Lastly, the Java driver provides Cassandra keyspace schema in a usable manner.

6.11 Using the Java Interface

The Java driver and associated software can be downloaded from DataStax at the given link. You need to register before downloading the software. The download contains all the required jars for the driver. Further, you need to create a Java program to connect to Cassandra and compile a Java program with the jar files.

6.12 Querying Cassandra using Java

Following are the steps to run a query on Cassandra using the Java interface. First, get a handler for the cluster. Second, get a session handler by connecting to the cluster. Third, send a query to Cassandra using the session handler. Fourth, retrieve the results and iterate through the result set, one row at a time. Lastly, close the session.

6.13 Java Example

The example given illustrates the first part of a program for connecting to Cassandra from Java. Get a handler for the cluster with node using the Cluster builder, and then initiate a session by connecting to the cluster and getting a session handler.

6.14 Java Example (contd.)

The remaining steps of the program are shown here. These steps execute the query “select ticker, value from testdb.stocks”, assuming that the stocks table created in the demo already exists. Next, we loop over the query result one row at a time and print the column values.

6.15 Full Program

The complete program to connect to Cassandra using Java is shown. This program,, is available for download at the Simplilearn website. The program code is also presented here to help you understand the flow. The program contains the class StockSelect that has two member functions and a main function. The connect() member function takes a node and initiates a session. The getStocks() method uses the database connection to execute the select query. It also loops over each row of the result and prints them on the screen. The main function acts as a driver and calls the above member functions to connect and get the data from the Cassandra cluster. Note that it passes as the node to the connect function.

6.16 Compiling the Java Program

Once the Java program is created, you can compile and create a Jar on the virtual machine. You need the Datastax Java driver. Download the Java driver using the wget command as shown. Once the download is done, unzip the archive with the command, tar –xzf cassandra-java-driver-2.1.4.tar.gz Create the Java program shown earlier as The file name should match the class name.

6.17 Compiling the Java Program (contd.)

Use the commands shown to compile and create the jar file. The tar command mentioned earlier would have expanded the tar archive into the directory, cassandra-java-driver-2.1.4. Set the environment variable DRIVER to this value. Now set the CLASSPATH to the Java driver class libraries and the current directory. For compiling the program, you need a directory to hold the classes. Create a directory StockSelect_classes. Then compile the Java program with the options as shown. If you get any errors in compilation, check and correct them. Once the program is compiled without any errors, you can create a jar file with the command shown.

6.18 Running the Java Program

Use the following command to run the Java program and see the output: java com.example.cassandra.StockSelect Note that the full path name of the class is specified. Since you defined CLASSPATH earlier, you don’t need to define it again. This program connects to Cassandra on the virtual machine and selects all the rows from the stocks table that was created earlier. You will see the output as shown. The actual output will depend on the rows you have inserted into the stocks table.

6.19 Java Interface observations

We can make the following observations from the Java interface example: First, the Java interface is similar to the JDBC interface. JDBC is a popular interface for connecting to a relational database from Java. Second, you don’t have to worry about distributing your query on the cluster; the driver will take care of it. Third, the driver supports the CQL that was used in the command line interface.

6.20 ODBC Interface

Now, let us discuss the ODBC interface to Cassandra. DataStax provides the ODBC driver for Cassandra. ODBC is a standard interface used by some database utilities. The ODBC driver for 64-bit windows can be downloaded from the given link. You can configure the ODBC connection to Cassandra using the above driver. The ODBC driver makes it possible to connect to Cassandra from other languages like PHP, Python, and C#.

6.21 Quiz

A few questions will be presented in the following screens. Select the correct option and click submit to see the feedback.

6.22 Summary

Let us summarize the topics covered in this lesson. • Cassandra provides a command line interface called cqlsh. • Cqlsh provides many options such as help and the option to specify a file on command line. • Besides CQL commands, cqlsh provides commands such as COPY and CAPTURE. • Cqlsh provides elaborate help for many CQL commands. • The Cassandra Java interface is provided by the DataStax Java driver. • The Java driver can be downloaded from the DataStax website. • A simple Java program can be written to connect to Cassandra and get some data. • The Java program can be compiled and run in a few steps.

6.23 Conclusion

This concludes the lesson ‘Cassandra Interfaces’. In the next lesson, we will learn about advanced Cassandra Architecture and cluster management.

  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.

Request more information

For individuals
For business
Phone Number*
Your Message (Optional)
We are looking into your query.
Our consultants will get in touch with you soon.

A Simplilearn representative will get back to you in one business day.

First Name*
Last Name*
Phone Number*
Job Title*