Examples
DDL
-
Create a keymap table called 'cats' in a keyspace 'animals'. The cat's name will be the key which as an
strvalue while the value would be the cat's image in a binary format, so it'll be of thebinstrtype.a. Let's create the keyspace
CREATE KEYSPACE animalsb. Let's create the table:
CREATE TABLE animals:cat keymap(str,binstr)c. Let's switch to the table:
USE animals:catd. Let's inspect it:
INSPECT TABLE animals:cate. Let's drop the table:
DROP TABLE animals:catf. 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
strand 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
strwhile 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
strtype for the key. For the data type, we'll use thelisttype, with the inner type asstrsince 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 thebinstrdata 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 sayanThis 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 5This 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 2Remember 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 sayanThis 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!"