项目作者: kaukas

项目描述 :
A Cassandra driver for Crystal
高级语言: Crystal
项目地址: git://github.com/kaukas/crystal-cassandra.git
创建时间: 2018-07-10T09:20:25Z
项目社区:https://github.com/kaukas/crystal-cassandra

开源协议:MIT License

下载


Crystal DB API for Cassandra

A Crystal wrapper around the DataStax C/C++
Driver
. It conforms to
the crystal-db API.

Status

This is a personal project to create something meaningful while learning
Crystal. There are no guarantees about future development, maintenance, or
support. That said, if you need to use Crystal to query Cassandra this library
could be a good starting point. YMMV.

Installation

Please make sure you have installed the DataStax C/C++
Driver
. You can use
“development” packages if they are available or build from source.

Then add this to your application’s shard.yml:

  1. dependencies:
  2. cassandra:
  3. github: kaukas/crystal-cassandra

Documentation

The latest Crystal Cassandra API documentation can be found
here
.

Usage

From the basic
example
:

  1. require "cassandra/dbapi"
  2. DB.open("cassandra://127.0.0.1/test") do |db|
  3. db.exec(<<-CQL)
  4. create table posts (
  5. id timeuuid primary key,
  6. title text,
  7. body text,
  8. created_at timestamp
  9. )
  10. CQL
  11. db.exec("insert into posts (id, title, body, created_at) values (now(), ?, ?, ?)",
  12. "Hello World",
  13. "Hello, World. I have a story to tell.",
  14. Time.now)
  15. db.query("select title, body, created_at from posts") do |rs|
  16. rs.each do
  17. title = rs.read(String)
  18. body = rs.read(String)
  19. created_at = rs.read(Time)
  20. puts title
  21. puts "(#{created_at})"
  22. puts body
  23. end
  24. end
  25. end

Please refer to crystal-db for
further usage instructions.

Types

crystal-cassandra supports all the
DB::Any

primitive types plus Int8 and Int16 and some additional value types:

  • date maps to Cassandra::DBApi::Date
  • time maps to Cassandra::DBApi::Time
  • uuid maps to Cassandra::DBApi::Uuid
  • timeuuid maps to Cassandra::DBApi::TimeUuid

Some of the collection types are also supported:

  • list maps to Array
  • set maps to Set
  • map maps to Hash

Casting

Cassandra supports nested collection types (lists, sets, maps, etc.). Since
Crystal deprecated recursive
aliases
they can be
represented with recursive structs. crystal-cassandra has
Cassandra::DBApi::Any which performs a similar function to
JSON::Any. You can pass
collection parameters to queries wrapped with Cassandra::DBApi::Any (taken
from the collections
example
):

  1. alias Any = Cassandra::DBApi::Any
  2. # Assuming `authors` is `list<text>`
  3. db.exec("insert into posts (id, authors) values (now(), ?)",
  4. Any.new([Any.new("John Doe"), Any.new("Ben Roe")]))
  5. db.query("select authors from posts") do |rs|
  6. rs.each do
  7. authors = rs.read(Array(Any))
  8. puts "Authors: #{authors.map { |author| author.as_s }.join(", ")}"
  9. end
  10. end

You could do the same for the primitive values as well:

  1. db.exec("insert into posts (id, title) values (now(), ?)",
  2. Any.new("Hello World"))
  3. db.query("select title from posts") do |rs|
  4. rs.each do
  5. title = rs.read(Any)
  6. puts title.as_s
  7. end
  8. end

but shortcuts are defined for them so Any can be skipped (see the basic
example
).

Development

Install the C/C++ Driver. The Makefile commands work on MacOS:

  1. make build-cppdriver

Start a Docker container with an instance of Cassandra:

  1. make start-cassandra

Run the tests:

  1. crystal spec

Stop Cassandra when finished:

  1. make stop-cassandra

Contributing

  1. Fork it (https://github.com/kaukas/crystal-cassandra/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • kaukas Linas Juškevičius - creator, maintainer