=====Database Operations=====
For a NoSQL, Cassandra seems more as SQL Database (Oracle, DB2, etc) than NoSQL databases (Mongo for example)
A lot of the commands are the same as SQL datatabase, as you will see below:
====Connection====
First and foremost, we have to connect to our cassandra :) Cassandra uses a lot of ports and knowing which ports to use is essential:
Cassandra
* 7199 JMX monitoring port and in earlier Cassandra (0.8) version it was 8080
* 9042 CQL Native Transport Port
* 9160 Thrift client API
* 7000 Inter-node cluster
* 7001 TLS Internode communication (used if TLS enabled)
DataStax OpsCenter
* 61620 opscenterd daemon
* 61621 Agent
* 8888 Website
So for our connection we will use port: 9042, which is the native CQL client port, as follows:
$ cqlsh 172.27.1.246 9042 -u jandonov -p XXXXXXXXXXXXXXXXXXXXX
^ ^ ^ ^ ^
| | | | |
Client IP Address Port Username Password
[cqlsh 5.0.1 | Cassandra 3.10 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
jandonov@cqlsh> SELECT * FROM system_schema.keyspaces;
keyspace_name | durable_writes | replication
====Schemas/Databases====
Schemas/Databases, also known as keyspaces, are created as follows:
cqlsh> CREATE KEYSPACE testkeyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor': '3'} AND durable_writes = true;
cqlsh> DESCRIBE testkeyspace;
CREATE KEYSPACE testkeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'} AND durable_writes = true;
cqlsh>
cqlsh> SELECT * FROM system_schema.keyspaces;
keyspace_name | durable_writes | replication
--------------------+----------------+-------------------------------------------------------------------------------------
system_auth | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '1'}
system_schema | True | {'class': 'org.apache.cassandra.locator.LocalStrategy'}
system_distributed | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '3'}
system | True | {'class': 'org.apache.cassandra.locator.LocalStrategy'}
testkeyspace | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '3'}
system_traces | True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '2'}
(6 rows)
cqlsh>
====Table====
Tables, also known as column families, are created within a schema/database as follows:
cqlsh> use testkeyspace;
cqlsh:testkeyspace>
cqlsh:testkeyspace> CREATE TABLE testTable (
... id int,
... name text,
... primary key(id)
... ) WITH read_repair_chance = 0.0
... AND dclocal_read_repair_chance = 0.1
... AND gc_grace_seconds = 864000
... 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 default_time_to_live = 0
... AND speculative_retry = '99PERCENTILE'
... AND min_index_interval = 128
... AND max_index_interval = 2048
... AND crc_check_chance = 1.0;
cqlsh:testkeyspace>
cqlsh:testkeyspace>
====Insert====
Inserts are also done pretty easily:
cqlsh:testkeyspace> select * from testTable;
id | name
----+------
(0 rows)
cqlsh:testkeyspace> insert into testTable values(1,'Julien');
SyntaxException: line 1:22 no viable alternative at input 'values' (insert into [testTable] values...)
cqlsh:testkeyspace> insert into testTable(id,name) values(1,'Julien');
cqlsh:testkeyspace> select * from testTable;
id | name
----+--------
1 | Julien
(1 rows)
cqlsh:testkeyspace>
====Create users====
Users in Cassandra can have either super(all privileges) role or objects privileges for objects. In order to list all the users, use the following command:
name | super
------------+-------
casbak | False
games | False
games_fun | False
games_real | False
jandonov | True
kharole | False
millichev | False
pdudenkov | False
To create new user, we can do as follows:
jandonov@cqlsh> create user andriin123 with password 'PASSWORD1';
jandonov@cqlsh> grant dev to adriin;
jandonov@cqlsh> list users;
name | super
-------------+-------
andriin123 | False