CQL (Cassandra Query Language)
Keyspace
Di Cassandra, skema disebut dengan keyspace. Keyspace berfungsi seperti sebuah folder atau skema yang digunakan untuk mengelompokkan tabel dalam suatu unit. Dengan menggunakan keyspace, tabel-tabel yang memiliki fungsi dan keterkaitan yang sama dapat dikelompokkan bersama. Sehingga, dapat memudahkan pengaturan dan pengelolaan tabel dalam suatu cluster.
SQL
Copy
-- create keyspace
create keyspace test_keyspace with replication = {
    'class' : 'SimpleStrategy',
    'replication_factor':'1'
    } and durable_writes = true;
-- drop keyspace
drop keyspace test_keyspace;
Table
SQL
Copy
-- create table
create table employee_by_id
(
    id       int primary key,
    name     text,
    position text
);
-- drop table
drop table employee_by_id;
-- create table dengan komposit value
-- car_make sebagai partition key yang digunakan untuk filter
-- id digunakan sebagai clustering column yang digunakan untuk order
create table employee_by_car_make
(
    car_make  text,
    id        int,
    car_model text,
    primary key ( car_make, id )
);
-- clustering column juga bisa lebih dari 1, disini ada age dan id
create table employee_by_car_make_sorted
(
    car_make text,
    age      int,
    id       int,
    name     text,
    primary key ( car_make, age, id)
);
-- partition key juga bisa lebih dari 1, disini ada car_make dan car_model
create table employee_by_car_make_and_car_model
(
    car_make  text,
    car_model text,
    id        int,
    name      text,
    primary key ( (car_make, car_model), id )
);
-- insert to table
insert into employee_by_id(id, name, position)
VALUES (1, 'John', 'Manager');
insert into employee_by_id(id, name, position)
VALUES (2, 'Bob', 'CEO');
