Examples
DDL
Create a keymap table called ‘cats’ in a keyspace ‘animals’. The cat’s name will be the key which as an
str
value while the value would be the cat’s image in a binary format, so it’ll be of thebinstr
type.a. Let’s create the keyspace
CREATE KEYSPACE animals
b. Let’s create the table:
CREATE TABLE animals:cat keymap(str,binstr)
c. Let’s switch to the table:
USE animals:cat
d. Let’s inspect it:
INSPECT TABLE animals:cat
e. Let’s drop the table:
DROP TABLE animals:cat
f. Let’s drop the keyspace
DROP KEYSPACE animals
Create a keymap table called ‘favorites’ in the ‘default’ keyspace. This will store a favorite name, an
str
and an URL, also anstr
:CREATE TABLE favorites keymap(str,str)
Create a keymap table called ‘cache’ in the ‘default’ keyspace that is volatile. Our cache key is an
str
while the value would be some binary data, sobinstr
:CREATE TABLE cache keymap(str,binstr) volatile
Lists
Create a table called ‘notes’ in the default keyspace. We’ll have usernames linked to a collection of notes made by them. Since the names are unicode values, we’ll use the
str
type for the key. For the data type, we’ll use thelist
type, with the inner type asstr
since the notes will also have unicode characters (like emojis, for example). If we needed to store binary data within the lists, we’d simply use thebinstr
data type instead.CREATE TABLE notes keymap(str,list<str>)
Now let’s switch to the table
use default:notes
Let’s add some notes made by our theoretical user
joe
. This is how it goes (#
signs were simply added for explanations, simply ignore them):# create an empty list
LSET sayan
# now append some notes
LMOD sayan PUSH "Just woke up. Jotting down today's first note!"
LMOD sayan PUSH "Heading to the subway; gotta get to work fast!"
LMOD sayan PUSH "Funny that someone broke my chair! Just called someone to get it fixed!"
LMOD sayan PUSH "Brrr...on my work machine. See you later!"
LMOD sayan PUSH "Done with work, now heading home!"
LMOD sayan PUSH "Woot, I'm home. What a hectic day!"
Let’s read all the notes:
LGET sayan
This would have printed out all the notes if we were using the command-line client, like this:
(1) "Just woke up. Jotting down today's first note!"
(2) "Heading to the subway; gotta get to work fast!"
(3) "Funny that someone broke my chair! Just called someone to get it fixed!"
(4) "Brrr...on my work machine. See you later!"
(5) "Done with work, now heading home!"
(6) "Woot, I'm home. What a hectic day!"
Let’s read a maximum of 5 notes:
LGET sayan LIMIT 5
This would output:
(1) "Just woke up. Jotting down today's first note!"
(2) "Heading to the subway; gotta get to work fast!"
(3) "Funny that someone broke my chair! Just called someone to get it fixed!"
(4) "Brrr...on my work machine. See you later!"
(5) "Done with work, now heading home!"
Now let’s remove the “broken chair” note because it turns out that I actually had the wrong chair!
LMOD sayan REMOVE 2
Remember that array indexes start at 0? Duh!
Let’s see what we have with
LGET sayan
:(1) "Just woke up. Jotting down today's first note!"
(2) "Heading to the subway; gotta get to work fast!"
(3) "Brrr...on my work machine. See you later!"
(4) "Done with work, now heading home!"
(5) "Woot, I'm home. What a hectic day!"
Wait, I forgot about something! Someone left a mysterious letter on my desk. I discovered it as soon as I entered my workplace. Let’s insert that:
LMOD sayan INSERT 2 "Just discovered a mysterious letter on my desk! How cool!"
Let’s get our entire bunch of notes now:
LGET sayan
This would output:
(1) "Just woke up. Jotting down today's first note!"
(2) "Heading to the subway; gotta get to work fast!"
(3) "Just discovered a mysterious letter on my desk! How cool!"
(4) "Brrr...on my work machine. See you later!"
(5) "Done with work, now heading home!"
(6) "Woot, I'm home. What a hectic day!"