Giter VIP home page Giter VIP logo

cqex's Introduction

cqex

Modern Cassandra driver for Elixir, using cqerl underneath.

Under development, so not much documentation for now

Installation

Add cqex and cqerl to your application's mix.exs:

defp deps do
  [
    # ...
    {:cqex, "~> 0.2.0"},
    {:cqerl, github: "matehat/cqerl"},
    # ...
  ]
end

And update your applications list in the project:

def application do
  [applications: [:cqex]]
end

Usage examples

If you're using a single cluster, in your project's config/config.exs:


use Mix.Config

config :cqerl, 
  cassandra_nodes: [{"10.0.0.1", 9042}, {"10.0.0.2", 9042}],
  keyspace: "keyspace"

Then, create a transient client connection

client = CQEx.Client.new!

Fetch the complete list of users, and creating a list out of it using Enum

all_users = client |> CQEx.Query.call!("SELECT * FROM users;") |> Enum.to_list
# => [ list of users... ]

all_users[0]
# => %{ ... first_user ... }

Chain queries and using Stream and Enum to get the result set in small pages.

alias CQEx.Query, as: Q

base = %Q{
  statement: "INSERT INTO animals (name, legs, friendly) values (?, ?, ?);",
  values: %{ legs: 4, friendly: true }
}

^base = Q.new 
|> Q.statement("INSERT INTO animals (name, legs, friendly) values (?, ?, ?);")
|> Q.put(:legs, 4)
|> Q.put(:friendly, true)

animals_by_pair = client
|> Q.call!("CREATE TABLE IF NOT EXISTS animals (name text PRIMARY KEY, legs tinyint, friendly boolean);")
|> Q.call!( base |> Q.merge(%{ name: "cat", friendly: false }) )
|> Q.call!( base |> Q.put(:name, "dog") )
|> Q.call!( base |> Q.merge(%{ name: "bonobo", legs: 2 }) )
|> Q.call!("SELECT * FROM animals;")
|> Stream.chunk(2)

animals_by_pair
|> Enum.at(0)

# => [ %{name: "cat", legs: 4, friendly: false}, %{name: "dog", legs: 4, friendly: true} ]

animals_by_pair
|> Enum.to_list

# => [ 
#      [ %{name: "cat", legs: 4, friendly: false}, %{name: "dog", legs: 4, friendly: true} ], 
#      [ %{name: "bonobo", legs: 2, friendly: true} ] 
#    ]

Use comprehensions on the results of a CQL query

for %{ legs: leg_count, name: name, friendly: true } <- Q.call!(client, "SELECT * FROM animals"), 
  leg_count == 4,
  do: "#{name} has four legs"
  
# => [ "dog has four legs" ]

If you want different clusters, in your project's config/config.exs:

use Mix.Config

config :cqerl, 
  cassandra_clusters: [
    cluster1: { [{"10.0.0.1", 9042}, {"10.0.0.2", 9042}], [keyspace: "keyspace1"] },
    cluster2: { [{"10.0.0.1", 9042}, {"10.0.0.2", 9042}], [keyspace: "keyspace2"] }
  ]

Then, in your code

client = CQEx.Client.new! :cluster1
# etc

cqex's People

Contributors

matehat avatar kelostrada avatar laibulle avatar rranelli avatar janajri avatar mus0u avatar

Watchers

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.