Skip to content

smanolache/lua-cassandra

 
 

Repository files navigation

lua-cassandra Module Version Build Status Coverage Status

This project is a fork of jbochi/lua-resty-cassandra. It adds support for binary protocol v3, a few bug fixes and more to come. See the improvements section for more details.

Lua Cassandra client using CQL binary protocol v2/v3 for Cassandra 2.0 and later.

It is 100% non-blocking if used in Nginx/Openresty but can also be used with luasocket.

Installation

Luarocks

Installation through luarocks is recommended:

$ luarocks install lua-cassandra

Manual

Simply copy the src/ folder in your application.

Usage

local cassandra = require "cassandra"
-- local cassandra = require "cassandra.v2" -- binary protocol v2 for Cassandra 2.0.x

local session = cassandra:new()
session:set_timeout(1000) -- 1000ms timeout

local connected, err = session:connect("127.0.0.1", 9042)
assert(connected)
session:set_keyspace("demo")

-- simple query
local table_created, err = session:execute [[
  CREATE TABLE users(
    id uuid PRIMARY KEY,
    name varchar,
    age int
  )
]]

-- query with arguments
local ok, err = session:execute("INSERT INTO users(name, age, user_id) VALUES(?, ?, ?)"
  , {"John O'Reilly", 42, cassandra.uuid("1144bada-852c-11e3-89fb-e0b9a54a6d11")})


-- select statement
local users, err = session:execute("SELECT name, age, user_id FROM users")
assert(1 == #users)

local user = users[1]
print(user.name) -- "John O'Reilly"
print(user.user_id) -- "1144bada-852c-11e3-89fb-e0b9a54a6d11"
print(user.age) -- 42

You can check more examples on the documentation or in the tests.

Documentation and examples

Refer to the online manual and reference.

Improvements

This fork provides the following improvements over the root project:

  • Support for binary protocol v3
    • User Defined Types and Tuples support
    • Serial Consistency support for batch requests
  • Support for authentication
  • Keyspace switch fix
  • IPv6 encoding fix

Roadmap

  • Support for binary protocol v3 named values when binding a query
  • Support for binary protocol v3 default timestamp option
  • Support for binary protocol v4

Makefile Operations

When developing, use the Makefile for doing the following operations:

Name Description
dev Install busted, luacov and luacheck
test Run the unit tests
lint Lint all Lua files in the repo
coverage Run unit tests + coverage report
clean Clean coverage report

Note: Before running make lint or make test you will need to run make dev.

Note bis: Tests are running for both binary protocol v2 and v3, so you must ensure to be running Cassandra 2.O or later.

Packages

No packages published

Languages

  • Lua 96.2%
  • Shell 3.2%
  • Makefile 0.6%