Git Product home page Git Product logo

cassandra-phantom's Introduction

Cassandra + Phantom Example

This project will give you the idea on how to design your cassandra tables in scala using the phantom dsl.

Data Modeling Concepts

Cassandra tables should be designed using a query-driven approach. Now let's check our query requirements before create the tables.

Requirements

  • Our Query1 should find a song by it's id
  • Our Query2 should find all songs from a specific artist.

Modeling

So far we know cassandra uses the query-driven approach and we also know our requirements, let's create our tables.

CREATE KEYSPACE test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
CREATE TABLE test.songs (
    song_id timeuuid PRIMARY KEY,
    album text,
    artist text,
    title text
);

CREATE TABLE test.songs_by_artist (
    artist text,
    song_id timeuuid,
    album text,
    title text,
    PRIMARY KEY (artist, song_id)
) WITH CLUSTERING ORDER BY (song_id ASC);

Data

Let's put some data and see how it works with our tables.

insert into songs(song_id, title, album, artist) values(now(), 'Prison Song', 'Toxicity', 'System of a Down');
insert into songs(song_id, title, album, artist) values(now(), 'Chop Suey', 'Toxicity', 'System of a Down');
insert into songs(song_id, title, album, artist) values(now(), 'Aerials', 'Toxicity', 'System of a Down');

select * from songs;

song_id                               | album    | artist           | title
--------------------------------------+----------+------------------+-------------
 b49e4790-3abd-11e5-9032-1572798d8cfa | Toxicity | System of a Down | Prison Song
 b49e6ea1-3abd-11e5-9032-1572798d8cfa | Toxicity | System of a Down |   Chop Suey
 b49e6ea0-3abd-11e5-9032-1572798d8cfa | Toxicity | System of a Down |     Aerials

How about our songs_by_artist? In CQL we need to do it again.

insert into songs_by_artist(song_id, title, album, artist) values(now(), 'Prison Song', 'Toxicity', 'System of a Down');
insert into songs_by_artist(song_id, title, album, artist) values(now(), 'Chop Suey', 'Toxicity', 'System of a Down');
insert into songs_by_artist(song_id, title, album, artist) values(now(), 'Aerials', 'Toxicity', 'System of a Down');

select * from songs_by_artist;

artist           | song_id                               | album    | title
------------------+--------------------------------------+----------+-------------
 System of a Down | b49e4790-3abd-11e5-9032-1572798d8cfa | Toxicity | Prison Song
 System of a Down | b49e6ea0-3abd-11e5-9032-1572798d8cfa | Toxicity |     Aerials
 System of a Down | b49e6ea1-3abd-11e5-9032-1572798d8cfa | Toxicity |   Chop Suey

Thus we can lookup for songs from a specific artist.

select * from songs_by_artist where artist = 'System of a Down';

artist           | song_id                               | album    | title
------------------+--------------------------------------+----------+-------------
 System of a Down | b49e4790-3abd-11e5-9032-1572798d8cfa | Toxicity | Prison Song
 System of a Down | b49e6ea0-3abd-11e5-9032-1572798d8cfa | Toxicity |     Aerials
 System of a Down | b49e6ea1-3abd-11e5-9032-1572798d8cfa | Toxicity |   Chop Suey

Phantom

Looking at the documentation, it shows simple examples on how to get your cassandra table into scala.

This project explore the scala generics to make our life easier and do not duplicate code when creating the same table with different partitions key.

Features

Apart of data modeling, you will also find:

  • Connect to a secure(user/password) Cassandra Cluster
  • Set Consistency Level for any kind of statement
  • Handle inserts/deletes for duplicate tables

Resources

cassandra-phantom's People

Contributors

iamthiago avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.