Examples

DDL

  1. 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 the binstr type.

    a. Let’s create the keyspace

    1. CREATE KEYSPACE animals

    b. Let’s create the table:

    1. CREATE TABLE animals:cat keymap(str,binstr)

    c. Let’s switch to the table:

    1. USE animals:cat

    d. Let’s inspect it:

    1. INSPECT TABLE animals:cat

    e. Let’s drop the table:

    1. DROP TABLE animals:cat

    f. Let’s drop the keyspace

    1. DROP KEYSPACE animals
  2. Create a keymap table called ‘favorites’ in the ‘default’ keyspace. This will store a favorite name, an str and an URL, also an str:

    1. CREATE TABLE favorites keymap(str,str)
  3. 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, so binstr:

    1. CREATE TABLE cache keymap(str,binstr) volatile

Lists

  1. 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 the list type, with the inner type as str 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 the binstr data type instead.

    1. CREATE TABLE notes keymap(str,list<str>)
  2. Now let’s switch to the table

    1. use default:notes
  3. 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):

    1. # create an empty list
    2. LSET sayan
    3. # now append some notes
    4. LMOD sayan PUSH "Just woke up. Jotting down today's first note!"
    5. LMOD sayan PUSH "Heading to the subway; gotta get to work fast!"
    6. LMOD sayan PUSH "Funny that someone broke my chair! Just called someone to get it fixed!"
    7. LMOD sayan PUSH "Brrr...on my work machine. See you later!"
    8. LMOD sayan PUSH "Done with work, now heading home!"
    9. LMOD sayan PUSH "Woot, I'm home. What a hectic day!"
  4. Let’s read all the notes:

    1. LGET sayan

    This would have printed out all the notes if we were using the command-line client, like this:

    1. (1) "Just woke up. Jotting down today's first note!"
    2. (2) "Heading to the subway; gotta get to work fast!"
    3. (3) "Funny that someone broke my chair! Just called someone to get it fixed!"
    4. (4) "Brrr...on my work machine. See you later!"
    5. (5) "Done with work, now heading home!"
    6. (6) "Woot, I'm home. What a hectic day!"
  5. Let’s read a maximum of 5 notes:

    1. LGET sayan LIMIT 5

    This would output:

    1. (1) "Just woke up. Jotting down today's first note!"
    2. (2) "Heading to the subway; gotta get to work fast!"
    3. (3) "Funny that someone broke my chair! Just called someone to get it fixed!"
    4. (4) "Brrr...on my work machine. See you later!"
    5. (5) "Done with work, now heading home!"
  6. Now let’s remove the “broken chair” note because it turns out that I actually had the wrong chair!

    1. LMOD sayan REMOVE 2

    Remember that array indexes start at 0? Duh!

    Let’s see what we have with LGET sayan:

    1. (1) "Just woke up. Jotting down today's first note!"
    2. (2) "Heading to the subway; gotta get to work fast!"
    3. (3) "Brrr...on my work machine. See you later!"
    4. (4) "Done with work, now heading home!"
    5. (5) "Woot, I'm home. What a hectic day!"
  7. 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:

    1. LMOD sayan INSERT 2 "Just discovered a mysterious letter on my desk! How cool!"
  8. Let’s get our entire bunch of notes now:

    1. LGET sayan

    This would output:

    1. (1) "Just woke up. Jotting down today's first note!"
    2. (2) "Heading to the subway; gotta get to work fast!"
    3. (3) "Just discovered a mysterious letter on my desk! How cool!"
    4. (4) "Brrr...on my work machine. See you later!"
    5. (5) "Done with work, now heading home!"
    6. (6) "Woot, I'm home. What a hectic day!"