by Sabine Maennel
at pydata Zürich 25.1.2017
from Cassandra by example, rackspace.com
RDBMS | Cassandra | |
---|---|---|
Query-Language | SQL | CQL |
Container | Database | Keyspace |
Table | Table | Table |
Fields | Column | Column |
Primary Key | Primary Key | Primary Key |
Operations | SELECT, CRUD | SELECT, CRUD |
Cassandra is a mulitlevel-map rather then a structure
the primary key has two parts:
-> look at how Cassandra reads and writes
>cassandra
to start cassandra>cqlsh
in a different terminal to start CQL
cqlsh> CREATE KEYSPACE IF NOT EXISTS twissandra
WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};
cqlsh> DESCRIBE twissandra;
CREATE KEYSPACE twissandra WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
Now we have to use that keyspace
cqlsh> USE twissandra;
We are ready to create the first table:
cqlsh> CREATE TABLE users (
username text PRIMARY KEY,
password text);
cqlsh> DESCRIBE users;
CREATE TABLE twissandra.users (
username text PRIMARY KEY,
password text
) WITH 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';
cqlsh>
-- "username" follows "followed"
CREATE TABLE following (
username text,
followed text,
PRIMARY KEY(username, followed)
);
-- "username" is followed by "following"
CREATE TABLE followers (
username text,
following text,
PRIMARY KEY(username, following)
);
CREATE TABLE tweets (
tweetid uuid PRIMARY KEY,
username text,
body text
);
CREATE TABLE userline (
tweetid timeuuid,
username text,
body text,
PRIMARY KEY(username, tweetid)
);
CREATE TABLE timeline (
username text,
tweetid timeuuid,
posted_by text,
body text,
PRIMARY KEY(username, tweetid)
);