Apache Cassandra 1.1 Documentation

Using the Database - CQL Introduction

Cassandra has several interfaces for querying the database from the command line:

Getting Started Using CQLsh

CQLsh 2 is the default query language and CQLsh 3 is the new, recommended version. After starting the Cassandra Server, follow these steps to get started using CQLsh 3 quickly:

  1. Start CQLsh 3 on Linux or Mac:

    From the bin directory of the Cassandra installation, run the cqlsh script.

    cd <install_location>/bin
    
    ./cqlsh --cql3
    
  2. Create a keyspace to work with:

    cqlsh> CREATE KEYSPACE demodb
             WITH strategy_class = 'org.apache.cassandra.locator.SimpleStrategy'
             AND strategy_options:replication_factor='1';
    
    cqlsh> USE demodb;
    
  3. Create a column family (the counterpart to a table in relational database world):

    cqlsh> CREATE TABLE users (
             user_name varchar,
             password varchar,
             state varchar,
             PRIMARY KEY (user_name)
           );
    
  4. Enter and read data from Cassandra:

    cqlsh> INSERT INTO users
             (user_name, password)
             VALUES ('jsmith', 'ch@ngem3a');
    
    cqlsh> SELECT * FROM users WHERE user_name='jsmith';
    

    The output is:

     user_name | password  | state
    -----------+-----------+-------
        jsmith | ch@ngem3a |  null
    
  5. Exit CQLsh:

    cqlsh> exit