The ad hoc c* clj talk
The Driver
- Most clojure drivers for c* are wrapping Java drivers
- I recommend using one that wraps the DataStax Java Driver
- Good options are Cassaforte and Alia
- http://clojurecassandra.info/articles/getting_started.html
- I'll be walking through Cassaforte
- Helpful sometimes to reference the java driver https://github.com/datastax/java-driver
Connecting
What they show
(ns cassaforte.docs.examples
(:require [clojurewerkz.cassaforte.client :as cc]))
;; Will connect to localhost
(cc/connect ["127.0.0.1"])
Connecting
(def cconnection (atom nil))
(def current-keyspace (atom nil))
(defn rebuild-connection []
(let [{:keys [seed-list cassandra-user cassandra-pass]} (conf/get-cassandra-conf)]
(reset! cconnection
(apply cc/connect
(if cassandra-user
[seed-list {:credentials
{:username cassandra-user :password cassandra-pass}}]
[seed-list])))
(reset! current-keyspace nil)))
(defn get-connection
"Gets existing connection or creates a new one."
[& [optional-keyspace & _]]
(when-not @cconnection
(locking cconnection
(when-not @cconnection
(rebuild-connection))))
(when optional-keyspace
(when (not= @current-keyspace optional-keyspace)
(cql/use-keyspace @cconnection :my_data)))
@cconnection)
What to do with state? This works but it's ugly
Connecting
Way better ways?
- Manage lifecyle with something like componet https://github.com/stuartsierra/component
- Compose functions rather than relying on global state
- Dodge locks with mechanisms like core.async
- ~DO NOT~ just swap! an atom, resource leaks/etc
Inserting
- Clojure DSL for CQL
- https://github.com/mpenet/hayt
- Both Cassaforte and Alia use this
(:require '[clojurewerkz.cassaforte.client :as cc]
(:require '[clojurewerkz.cassaforte.cql :as cql])
(def conn (cc/connect ["127.0.0.1"])
(cql/insert conn "test_data" {:data_id (int 42) :data_item "The Ultimate Answer"}))
Selecting
(cql/insert conn "test_data" {:data_id (int 42) :data_item "The Ultimate Answer"}))
(use '[clojurewerkz.cassaforte.query])
(cql/select conn "test_data" (where [[= :data_id (int 42)]]))
More Fun Stuff
- Use clj to launch ccm for repl/integration testing
- https://github.com/SMX-LTD/ccm-clj
- A better demo than I have can be found here
- https://github.com/nickmbailey/clojure-cassandra-demo
- Learn more about Cassandra at Datastax Academy for free
- https://academy.datastax.com/
The Slacker Ad Hoc c* clojure talk
By Philip Doctor
The Slacker Ad Hoc c* clojure talk
This was thrown together in a few minutes to fill in for a last minute speaker cancellation, don't judge this one please.
- 1,364