Cassandra Cheat Sheet

1. Discover

Launch CQL client

cassandra-2.1.13/bin/cqlsh [hostnmae] -u [user]

List Keyspaces

DESCRIBE KEYSPACES;
or
select * from system.schema_keyspaces ;

Change Keyspace

USE nom_keyspace;

List tables of a Keyspace

describe tables;

2. Create

Create Keyspace

CREATE KEYSPACE Keyspace Name] 
WITH REPLICATION = {‘class:SimpleStrategy, 
				replication_factor : 3 };

Create table

CREATE TABLE 	[Table Name] (
[Field 2] [Type],
[Field 1] [Type])
WITH COMPACT STORAGE;

Info : You may add PRIMARY KEY after the type to set a primary key

Multi field primary key

CREATE TABLE 	[Table Name] (
[Field 2] [Type],
[Field 1] [Type])
PRIMARY KEY (Field1, Field2));

Info : The field order is important:

  • First column is the partition key
  • Second column is the cluster

You may have partition key or clusters with many fields

PRIMARY KEY (([Field 1],[Field 2]), ([Field 3],[Field 4]))

3. Insert and Update

Insert a new record

INSERT INTO [Table] ([Fields list]) VALUES ([Values List);

Info : Use simple quotes for the values

Update a record

UPDATE [Table]] set [Field] = [Value]
comments powered by Disqus