Code Monkey home page Code Monkey logo

scqla's Introduction

Scqla

A fully asynchronous Cassandra CQL driver for Scala using Akka IO.

##Highlights:

  1. Directly construct objects from results.

  2. Completely stateless, no sessions required. Fire any query from anywhere, anytime.

  3. Load balance between cassandra nodes.

  4. Get an Either as a result of all queries. No exceptions, just Scala goodness.

###Connect to cassandra cluster.

Scqla.connect

###Import from Scqla object

import Scqla._

###Create a new keyspace

query("CREATE KEYSPACE demodb WITH REPLICATION = {'class' : 'SimpleStrategy','replication_factor': 1}").map(_.fold(
  error => println(error),
  valid => {
    //do something
  }
))

###Set global keyspace

Note: This only works if you only connect to one node in Cassandra cluster. If that is not the case use full table qualifiers as shown in the examples here.

query("use demodb").map(_.fold(
  error => println(error),
  valid => { //do something
  }
))

###Create a new table

    query("""CREATE TABLE demodb.emp (
    		empID int,
    		deptID int,
    		alive boolean,
    		id uuid,
    		first_name varchar,
    		last_name varchar,
    		salary double,
    		age bigint,
        PRIMARY KEY (empID, deptID))""").map(_.fold(
      error => println(error),
      valid => {
        //do something
      }
    ))

###Execute prepared queries

    prepare("INSERT INTO demodb.emp (empID, deptID, alive, id, first_name, last_name, salary, age) VALUES (?, ?, ?, ?, ?, ?, ?, ?)").map(_.fold(
      error => println(error),
      valid => valid.execute(104, 15, true, new java.util.UUID(0, 0), "Hot", "Shot", 10000000.0, 98763L)))

###You can directly construct objects from the result.

###Case class list

case class Emp(empId: Int, deptId: Int, alive: Boolean, id: java.util.UUID, first: String, last: String, salary: Double, age: Long)

queryAs[Emp]("select empID, deptID, alive, id, first_name, last_name, salary, age from demodb.emp").map(_.fold(
  error => println(error),
  empList => {
    empList.foreach(e => s"First name of employee id ${e.empId} is ${e.first}")
  }
))

###Primitives list

queryAs[Int]("select empid from demodb.emp").map(_.fold(
  error => println(error),
  idList => {
    idList.foreach(println)
  }
))

###Strings

queryAs[String]("select first_name from demodb.emp").map(_.fold(
  error => println(error),
  nameList => {
    nameList.foreach(println)
  }
))

###Execute prepared queries and get results

prepare("select empID, deptID, alive, id, first_name, last_name, salary, age from demodb.emp where empid = ? and deptid = ?").map(_.fold(
  error => println(error),
  valid => valid.executeGet[Emp](104, 15).map(_.fold(
    error => println(error),
    empList => {
      empList.foreach(e => s"First name of employee id ${e.empId} is ${e.first}")
    }
  )
)))

###Drop keyspace

query("drop KEYSPACE demodb").map(_.fold(
  error => println(error),
  valid => {
    //do something
  }
))

scqla's People

Contributors

eklavya avatar

Watchers

Matthew Lagace avatar James Cloos 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.