Skip to content

Cassandra

Luke Chen edited this page Mar 6, 2021 · 23 revisions

Installation

Installation from Debian packages

https://cassandra.apache.org/download/

Start/Stop Cassandra

https://cassandra.apache.org/doc/latest/getting_started/installing.html#installing-the-debian-packages

sudo service cassandra start
sudo service cassandra stop

You can start Cassandra with sudo service cassandra start and stop it with sudo service cassandra stop. However, normally the service will start automatically. For this reason be sure to stop it if you need to make any configuration changes.

Verify that Cassandra is running by invoking nodetool status from the command line.

$ nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address    Load       Tokens       Owns (effective)  Host ID                               Rack
UN  127.0.0.1  70.75 KiB  256          100.0%            30848df6-476d-4728-b2cb-ec769fd5e2d8  rack1

The Cassandra service gets started automatically after installation. Monitor the progress of the startup with:

$ tail -f /var/log/cassandra/system.log

The default location of configuration files is /etc/cassandra.

The default location of log and data directories is /var/log/cassandra/ and /var/lib/cassandra.

Start-up options (heap size, etc) can be configured in /etc/default/cassandra.

Connect to the database

$ cqlsh

Tutorial

https://www.tutorialspoint.com/cassandra/

Case study

cqlsh connect to a cluster

lchen154@ubuntu:~$ cqlsh
Connected to Test Cluster at 127.0.0.1:9042.
cqlsh> select cluster_name, listen_address from system.local;

 cluster_name | listen_address
--------------+----------------
 Test Cluster |      127.0.0.1

(1 rows)

Create a keyspace

cqlsh> create keyspace tutorialspoint with replication = {'class':'SimpleStrategy', 'replication_factor' : 3};
cqlsh> describe keyspaces;

tutorialspoint  system_auth  system_distributed
system_schema   system       system_traces     

Create a table in a keyspace

cqlsh> use tutorialspoint;
cqlsh:tutorialspoint> create table emp(
                  ... emp_id int PRIMARY KEY,
                  ... emp_name text,
                  ... emp_city text,
                  ... emp_sal varint,
                  ... emp_phone varint
                  ... );
cqlsh:tutorialspoint> select * from emp;

 emp_id | emp_city | emp_name | emp_phone | emp_sal
--------+----------+----------+-----------+---------

(0 rows)
cqlsh:tutorialspoint> create index name on emp (emp_name);
cqlsh:tutorialspoint> INSERT INTO emp (emp_id, emp_name, emp_city,
                  ...    emp_phone, emp_sal) VALUES(1,'ram', 'Hyderabad', 9848022338, 50000);
cqlsh:tutorialspoint> INSERT INTO emp (emp_id, emp_name, emp_city,
                  ...    emp_phone, emp_sal) VALUES(2,'robin', 'Hyderabad', 9848022339, 40000);
cqlsh:tutorialspoint> INSERT INTO emp (emp_id, emp_name, emp_city,
                  ...    emp_phone, emp_sal) VALUES(3,'rahman', 'Chennai', 9848022330, 45000);
cqlsh:tutorialspoint> select * from emp;

 emp_id | emp_city  | emp_name | emp_phone  | emp_sal
--------+-----------+----------+------------+---------
      1 | Hyderabad |      ram | 9848022338 |   50000
      2 | Hyderabad |    robin | 9848022339 |   40000
      3 |   Chennai |   rahman | 9848022330 |   45000

(3 rows)

Query with WHERE

A WHERE clause can be used only on the columns that are part of the primary key or have a secondary index on them

cqlsh:tutorialspoint> select * from emp where emp_id = 2;

 emp_id | emp_city  | emp_name | emp_phone  | emp_sal
--------+-----------+----------+------------+---------
      2 | Hyderabad |    robin | 9848022339 |   40000

(1 rows)
cqlsh:tutorialspoint> select * from emp where emp_name = 'ram';

 emp_id | emp_city  | emp_name | emp_phone  | emp_sal
--------+-----------+----------+------------+---------
      1 | Hyderabad |      ram | 9848022338 |   50000

(1 rows)
cqlsh:tutorialspoint> select * from emp where emp_city = 'Hyderabad';
InvalidRequest: Error from server: code=2200 [Invalid query] message="Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING"
cqlsh:tutorialspoint> select * from emp where emp_city = 'Hyderabad' ALLOW FILTERING;

 emp_id | emp_city  | emp_name | emp_phone  | emp_sal
--------+-----------+----------+------------+---------
      1 | Hyderabad |      ram | 9848022338 |   50000
      2 | Hyderabad |    robin | 9848022339 |   40000

(2 rows)

Create another secondary index

cqlsh:tutorialspoint> create index city on emp (emp_city);

cqlsh:tutorialspoint> select * from emp where emp_city = 'Hyderabad';

 emp_id | emp_city  | emp_name | emp_phone  | emp_sal
--------+-----------+----------+------------+---------
      1 | Hyderabad |      ram | 9848022338 |   50000
      2 | Hyderabad |    robin | 9848022339 |   40000

(2 rows)
cqlsh:tutorialspoint> select * from emp where emp_name = 'ram';

 emp_id | emp_city  | emp_name | emp_phone  | emp_sal
--------+-----------+----------+------------+---------
      1 | Hyderabad |      ram | 9848022338 |   50000

(1 rows)

Delete rows

cqlsh:tutorialspoint> delete * from emp where emp_name = 'ram';

Delete the table:

cqlsh:tutorialspoint> truncate table emp;

Delete a keyspace

drop keyspace tutorialspoint;

Clone this wiki locally