Cassandra has several interfaces for querying the database from the command line:
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:
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
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;
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)
);
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
Exit CQLsh:
cqlsh> exit