-
Notifications
You must be signed in to change notification settings - Fork 0
Cassandra
Installation from Debian packages
https://cassandra.apache.org/download/
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.
- Columns - name-value pair, can be indexed
- Row - container for columns, referenced by primary key
- Table - container for rows
- Keyspace - container for tables, can span one or more nodes
- Cluster - container for keyspace
$ cqlsh
https://www.tutorialspoint.com/cassandra/
https://ford.udemy.com/course/learn-cassandra-from-scratch
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)
cqlsh> create keyspace tutorialspoint with replication = {'class':'SimpleStrategy', 'replication_factor' : 3};
cqlsh> describe keyspaces;
tutorialspoint system_auth system_distributed
system_schema system system_traces
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)
A WHERE clause can be used only on the columns that are part of the primary key or have a secondary index on them.
If it's forced to query with "ALLOW FILTERING", if no partition key, it will need to visit all nodes; if not clustering key or indexed, all columns will need to be re-sorted.
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)
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)
cqlsh:tutorialspoint> delete * from emp where emp_name = 'ram';
cqlsh:tutorialspoint> truncate table emp;
drop keyspace tutorialspoint;
Partition key - decides how data is distributed across nodes Clustering key - decides how data is stored on a single node, rows are sorted by values of clustering keys
cqlsh:tutorialspoint> create table books_by_author (
... author_name TEXT,
... publish_year INT,
... book_id UUID,
... book_name TEXT,
... rating FLOAT,
... primary key((author_name), publish_year, book_id)
... ) with clustering order by (publish_year DESC, book_id ASC);
cqlsh:tutorialspoint> describe table books_by_author;
CREATE TABLE tutorialspoint.books_by_author (
author_name text,
publish_year int,
book_id uuid,
book_name text,
rating float,
PRIMARY KEY (author_name, publish_year, book_id)
) WITH CLUSTERING ORDER BY (publish_year DESC, book_id ASC)
AND bloom_filter_fp_chance = 0.01
AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND crc_check_chance = 1.0
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99PERCENTILE';