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

The ODBC

PHP interface

Python interface

Ruby interface

C# and .Net interface

In this lesson, you will learn about the command line, Java, and ODBC interfaces for Cassandra. Let us start with Cassandra command line interface in the next section.

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

Let us talk about Cqlsh Options in the next section.

Cqlsh Options

Cqlsh supports many options. The table shows some of these.

Options

Description

-h or --help

Displays helpful information on various cqlsh commands

--version

Shows the version number of the cqlsh program

--username= USERNAME

To authenticate using username

--password= PASSWORD

To authenticate using password

--keyspace=KEYSPACE

To specify a particular keyspace on login

--file=FILENAME

To execute the commands from a file and exit

–e statement

To execute a given statement and exit

Let us talk about Cqlsh Commands in the next section.

Cqlsh Commands

You can also use various commands to interact with cqlsh.

The cqlsh prompt is shown on the 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.

In the next section, let us focus on Cqlsh Shell Commands.

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

Command

Description

CAPTURE

Appends the command output to a given file. It can be turned off with CAPTURE OFF

COPY

Imports and exports data

DESCRIBE

To provide information on the cluster, keyspaces, and tables

HELP

Provides detailed help on many CQL and cqlsh commands supported by cqlsh

Cqlsh Help

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.

You learned about the Command Line Interface, cqlsh, until now. Next, we will discuss the Java interface.

Java Interfaces

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.

In the next section, let us focus on using the Java Interface.

Using the Java Interface

The Java driver and associated software can be downloaded from DataStax here.

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.

In the next section, let us focus on Querying Cassandra using Java.

Querying Cassandra using Java

Following are the steps to run a query on Cassandra using the Java interface.

Get a handler for the cluster.

Get a session handler by connecting to the cluster.

Send a query to Cassandra using the session handler.

Retrieve the results and iterate through the result set, one row at a time.

Close the session.

Let us see the java example in the next section.

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 128.0.0.1 using the Cluster builder, and then initiate a session by connecting to the cluster and getting a session handler.

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.

Let us look into the full program in the next section.

Full Program

The complete program to connect to Cassandra using Java is shown below.

This program, StockSelect.java, 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 127.0.0.1 as the node to the connect function.

In the next section, let us focus on compiling the Java program.

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.

Create the Java program shown earlier as StockSelect.java. The file name should match the class name.

Use the commands shown below 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.

Let us learn how to run the Java program in the next section.

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

The actual output will depend on the rows you have inserted into the stocks table.

In the next section, let us talk about Java Interface Observations.

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.

Now, let us discuss the ODBC interface to Cassandra.

ODBC Interface

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