Giter Site home page Giter Site logo

clj-hector's Introduction

clj-hector

A simple Clojure client for Cassandra that wraps Hector

Current build status: Build status

Installation

Add the following to your project.clj

:dev-dependencies [[org.clojars.paul/clj-hector "0.1.3"]]

Usage

Schema Manipulation

(def cluster (cluster "Pauls Cluster" "localhost"))
(add-keyspace cluster
              {:name "Keyspace Name"
               :replication 3
               :column-families [{:name "a"}
                                 {:name "b"
                                  :comparator :long}]})
(add-column-family cluster "Keyspace Name" {:name "c"})
(drop-keyspace cluster "Keyspace Name")

Basic retrieval of rows

(def c (cluster "Pauls Cluster" "localhost"))
(def ks (keyspace c "Twitter"))
(get-rows ks "Users" ["paul"] :n-serializer :string)

user> (-> (cluster "Pauls Cluster" "localhost")
          (keyspace "Twitter")
          (get-rows "Users" ["paul"] :n-serializer :string))
({"paul" {"age" #<byte[] [B@324a897c>, "login" #<byte[] [B@3b8845af>}})

It's also possible to query for column slices

user> (-> (cluster "Pauls Cluster" "localhost")
          (keyspace "Twitter")
          (get-columns "Users" "paul" ["age" "login"] :n-serializer :string))

Serializing non-String types

user> (put ks "Users" "Paul" {"age" 30})
#<MutationResultImpl MutationResult took (2us) for query (n/a) on host: localhost(127.0.0.1):9160>
user> (get-rows ks "Users" ["Paul"] :n-serializer :string :v-serializer :integer)
({"Paul" {"age" 30}})

The following serializers are supported

  • :string
  • :integer
  • :long
  • :bytes

Super Columns

Firstly, the column family will need to support super columns.

user> (add-column-family cluster "Keyspace Name" {:name "UserRelationships"
                                                  :type :super})

Storing super columns works using a nested map structure:

user> (put ks "UserRelationships" "paul" {"SuperCol" {"k" "v"} "SuperCol2" {"k2" "v2"}})
#<MutationResultImpl MutationResult took (6us) for query (n/a) on host: localhost(127.0.0.1):9160>

Retrieving super columns with get-super-rows:

user> (get-super-rows ks "UserRelationships" ["paul"] ["SuperCol" "SuperCol2"] :s-serializer :string :n-serializer :string :v-serializer :string)
({"paul" ({"SuperCol", {"a" "1", "k" "v"}} {"SuperCol2", {"k2" "v2"}})})

In the above example, note the addition of the s-serializer option: this controls how super column names should be deserialized.

You can also query for a sequence of columns:

user> (get-super-columns ks "UserRelationships" "paul" "SuperCol" ["a" "k"] :s-serializer :string :n-serializer :string :v-serializer :string)
{"a" "1", "k" "v"}

Deleting Rows

It's possible to delete all columns identified by keys with the delete-rows function. This works with both super-column families and regular column families.

To delete the example above:

user> (delete-rows ks "UserRelationships" ["paul"])

user> (get-super-columns ks "UserRelationships" "paul" "SuperCol" ["a" "k"] :s-serializer :string :n-serializer :string :v-serializer :string)
{}

user> (get-super-rows ks "UserRelationships" ["paul"] ["SuperCol" "SuperCol2"] :s-serializer :string :n-serializer :string :v-serializer :string)
({"paul" ()})

TODO: In the above query, a row is returned despite having no results. This should probably just return an empty sequence.

Query metadata

Hector exposes data about how long queries took to execute (and on which host). This is provided as metadata on the query result maps:

user> (meta (get-rows ks "Users" ["Paul"] {:n-serializer :string :v-serializer :integer}))
{:exec_us 2, :host #<CassandraHost localhost(127.0.0.1):9160>}

Experimental Schema Querying

clj-hector allows you to provide default schema settings for the specified column families (see ./test/clj_hector/test/schema.clj for examples).

For example, when operating with the MyColumnFamily column family, you can provide default name and value serializers as follows:

(def MyColumnFamily [:name "MyColumnFamily"
                     :n-serializer :string
                     :v-serializer :string])

Then, when querying, wrap the functions with the with-schema macro:

(with-schemas [MyColumnFamily]
  (put ks "MyColumnFamily" "row-key" {"k" "v"})
  (get-rows ks "MyColumnFamily" ["row-key"])))

Note that it's still very early days- all suggestions and forks are welcome!

TODO

  • Better support different Hector query types- multimethod dispatch based on arity of pk and c args?
  • Paging support for queries (somehow wiring into chunked sequences?)
  • Better support of CassandraHostConfigurator
  • Refactoring

License

Copyright (c) Paul Ingles

Distributed under the Eclipse Public License, the same as Clojure.

clj-hector's People

Contributors

pingles avatar nickmbailey avatar rplevy-draker avatar alanpeabody avatar licenser avatar thobbs avatar

Stargazers

Oz avatar

Watchers

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