Posted on by & filed under Content - Highlights and Reviews, Mobile Development, Programming & Development, Web Development.

Apache Cassandra is an open source NoSQL database that is designed to meet the challenges of massive scalability and fault tolerance. In previous articles (try this article) we discussed the basics of Cassandra and its unique data modeling techniques (try this article). In this article, we will build on the knowledge gained to learn how to interact with a Cassandra node using the Command Line Interface (CLI) tool that ships by default with the Cassandra installation.

Getting started

To run the tool, open the command prompt (or Terminal if you are on Linux) and type the following:

If the output is something like:

Then the tool has successfully opened. If not, then you need to troubleshoot the issue. Common problems may include a corrupt installation or the path to the ‘bin’ directory of Cassandra not in your PATH environment variable. Try re-installing the software and attempt to run the CLI tool again.

Connecting to a node

A typical Cassandra cluster consists of multiple identical nodes running in parallel. Establishing a connection to any one of these nodes will allow you to communicate with the Cassandra cluster as a whole. In the simplest scenario, the Cassandra “cluster” will consist of a single node running on the local machine. To connect to this local node, type the following within the CLI prompt:

Note the semicolon at the end of the command. This is required by most of the commands in Cassandra, so make sure you end each command with it. The “localhost/9160” part after the keyword “connect” is the ip/port pair of the node to which connection is to be made. Substitute this part with the appropriate values (if need be) to match your own setup.

If connection is successful, Cassandra will display something along the lines of:

Setting up the “schema”

We discussed the Cassandra data model in a previous article where we explained how Cassandra is essentially a key-value store but is complemented by a powerful column-based system organized into column families. We will assume that the reader is familiar with these basic concepts in the coming text.

The first step in order to start using Cassandra is to create a keyspace, which is equivalent to creating a database or schema in the relational database world. In order to do so, type:

“create” is the command to create anything in Cassandra. In this case, we are creating a “keyspace” with the name “TEST”. Notice the semicolon at the end, just like with the connect command.

Next, we need to authenticate ourselves in order to start using the newly created keyspace:

We do not need to specify any username/password combination since we did not set any credentials for the keyspace access. The system will report:

Storing and retrieving data

Before we can start adding data to Cassandra, we need to create a column family under which we can add columns to rows. To create a column family for storing data for users, type:

The command above creates a column family “USERS” and also specifies two attributes: “comparator” and “default_validation_class” both of which are set to “UTF8Type”. A detailed explanation is beyond the scope of this post, but suffice to say that these attributes specify that the column name and value should be treated as UTF-8 type.

Now we can begin storing and retrieving data. To add data, we need to:

The command “set” is used to store data into Cassandra. “USERS” refers to the newly created column family. The index within square brackets is the key of the row for which we are adding data. “name” is the column name and the value after the equals sign (=) is the column value.

To view (retrieve) this newly added data, we can:

This will output:

confirming that our data has been stored correctly.


In this article, we covered the basics of using the CLI tool to interact with Cassandra. The CLI can execute a wide variety of commands collectively referred to as the Cassandra Query Language (CQL). More in depth coverage of CQL can be found at the official Cassandra wiki at

Safari Books Online has the content you need

Below are some Cassandra books to help you develop applications, or you can check out all of the Cassandra books and training videos available from Safari Books Online. You can browse the content in preview mode or you can gain access to more information with a free trial or subscription to Safari Books Online.

The rising popularity of Apache Cassandra rests on its ability to handle very large data sets that include hundreds of terabytes — and that’s why this distributed database has been chosen by organizations such as Facebook, Twitter, Digg, and Rackspace. With Cassandra: The Definitive Guide, you’ll get all the details and practical examples you need to understand Cassandra’s non-relational database design and put it to work in a production environment.
Apache Cassandra is a fault-tolerant, distributed data store which offers linear scalability allowing it to be a storage platform for large high volume websites. Cassandra High Performance Cookbook provides detailed recipes that describe how to use the features of Cassandra and improve its performance. Recipes cover topics ranging from setting up Cassandra for the first time to complex multiple data center installations. The recipe format presents the information in a concise actionable form. The book describes in detail how features of Cassandra can be tuned and what the possible effects of tuning can be. Recipes include how to access data stored in Cassandra and use third party tools to help you out. The book also describes how to monitor and do capacity planning to ensure it is performing at a high level. Towards the end, it takes you through the use of libraries and third party applications with Cassandra and Cassandra integration with Hadoop
NoSQL databases are an efficient and powerful tool for storing and manipulating vast quantities of data. Most NoSQL databases scale well as data grows. In addition, they are often malleable and flexible enough to accommodate semi-structured and sparse data sets. Professional NoSQL is a comprehensive hands-on guide to the fundamental concepts and practical solutions for getting you ready to use NoSQL databases. Expert author Shashank Tiwari begins with a helpful introduction on the subject of NoSQL, explains its characteristics and typical uses, and looks at where it fits in the application stack. Unique insights help you choose which NoSQL solutions are best for solving your specific data storage needs.

About the authors

Salman Ul Haq is a techpreneur, co-founder and CEO of TunaCode, Inc., a startup that delivers GPU-accelerated computing solutions to time-critical application domains. He holds a degree is Computer Systems Engineering. His current focus is on delivering the right solution for cloud security. He can be reached at
Shaneeb Kamran is a Computer Engineer from one of the leading universities of Pakistan. His programming journey started at the age of 12 and ever since he has dabbled himself in every new and shiny software technology he could get his hands on. He is currently involved in a startup that is working on cloud computing products.

Tags: Apache Cassandra, Cassandra Query Language, CLI, Command Line Interface, CQL, PlanetCassandra,

Comments are closed.