• Stars
    star
    244
  • Rank 165,885 (Top 4 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 13 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CQL client for Cassandra written in Node.js.

node-cassandra-client

node-cassandra-client is a Node.js CQL 2 driver for Apache Cassandra 0.8 and later.

CQL is a query language for Apache Cassandra. You use it in much the same way you would use SQL for a relational database. The Cassandra documentation can help you learn the syntax.

node-cassandra-client and Apache Cassandra 1.2.x

Since 0.14.1, the client supports Apache Cassandra 1.2.x in CQL 2 compatibility mode.

By default Cassandra 1.2.x uses CQL 3 so you need to turn the CQL 2 compatibility mode on by passing 'cql_version': '2.0.0' attribute to Connection / PooledConnection constructor in the options object.

Explicitly setting CQL version on a connection is only supported from Apache Cassandra 1.1 and above so you should only set it if you are using version 1.1 or above.

For example:

var conn = new Connection({'host': host, 'port': port, 'keyspace': 'Keyspace1', 'cql_version': '2.0.0'});
var pool = new PooledConnection({'hosts': hosts, 'keyspace': 'Keyspace1', 'cql_version': '2.0.0'});

Creating column families using cqlsh

If you use cqlsh which ships with Cassandra 1.2.x or a newer version of cqlsh which defaults to CQL 3 you need to pass -2 argument to it, otherwise the client won't be able to read column family definitions (#67).

For example:

cqlsh -2 localhost 9160 < my_file.cql

Installation

$ npm install cassandra-client

Build status

Build Status

Running Tests and Lint

npm run-script test
npm run-script lint

License

node-cassandra-client is distributed under the Apache license.

lib/bigint.js is borrowed from the Node.js source (which comes from the V8 source).

Using It

Access the System keyspace

var System = require('cassandra-client').System;
var sys = new System('127.0.0.1:9160');

sys.describeKeyspace('Keyspace1', function(err, ksDef) {
  if (err) {
    // this code path is executed if the key space does not exist.
  } else {
    // assume ksDef contains a full description of the keyspace (uses the thrift structure).
  }
});

Create a keyspace

sys.addKeyspace(ksDef, function(err) {
  if (err) {
    // there was a problem creating the keyspace.
  } else {
    // keyspace was successfully created.
  }
});

Updating

This example assumes you have strings for keys, column names and values:

var Connection = require('cassandra-client').Connection;
var con = new Connection({host:'cassandra-host', port:9160, keyspace:'Keyspace1', user:'user', pass:'password'});
con.connect(function(err) {
  if (err) {
    // Failed to establish connection.
    throw err;
  }

  con.execute('UPDATE Standard1 SET ?=? WHERE key=?', ['cola', 'valuea', 'key0'], function(err) {
      if (err) {
          // handle error
      } else {
          // handle success.
      }
  });
});

The Connection constructor accepts the following properties:

host:        cassandra host
port:        cassandra port
keyspace:    cassandra keyspace
user:        [optional] cassandra user
pass:        [optional] cassandra password
use_bigints: [optional] boolean. toggles whether or not BigInteger or Number instances are in results.
timeout:     [optional] number. Connection timeout. Defaults to 4000ms.
log_time:    [optional] boolean. Log execution time for all the queries.

Getting data

NOTE: You'll only get ordered and meaningful results if you are using an order-preserving partitioner. Assume the updates have happened previously.

  con.execute('SELECT ? FROM Standard1 WHERE key >= ? and key <= ?', ['cola', 'key0', 'key1'], function (err, rows) {
    if (err) {
      // handle error
    } else {
      console.log(rows.rowCount());
      console.log(rows[0]);
                        assert.strictEqual(rows[0].colCount(), 1);
                        assert.ok(rows[0].colHash['cola']);
                        assert.ok(rows[0].cols[0].name === 'cola');
                        assert.ok(rows[0].cols[0].value === 'valuea');
    }
  });

Pooled Connections

// Creating a new connection pool.
var PooledConnection = require('cassandra-client').PooledConnection;
var hosts = ['host1:9160', 'host2:9170', 'host3', 'host4'];
var connection_pool = new PooledConnection({'hosts': hosts, 'keyspace': 'Keyspace1'});

PooledConnection() accepts an objects with these slots:

     hosts : String list in host:port format. Port is optional (defaults to 9160).
  keyspace : Name of keyspace to use.
      user : User for authentication (optional).
      pass : Password for authentication (optional).
   maxSize : Maximum number of connection to pool (optional).
idleMillis : Idle connection timeout in milliseconds (optional).
use_bigints: boolean indicating whether or not to use BigInteger or Number in numerical results.
timeout:   : [optional] number. Connection timeout. Defaults to 4000ms.
log_time   : [optional] boolean. Log execution time for all the queries.
             Timing is logged to 'node-cassandra-client.driver.timing' route. Defaults to false.

Queries are performed using the execute() method in the same manner as Connection, (see above). For example:

// Writing
connection_pool.execute('UPDATE Standard1 SET ?=? WHERE KEY=?', ['A', '1', 'K'],
  function(err) {
    if (err) console.log("failure");
    else console.log("success");
  }
);

// Reading
connection_pool.execute('SELECT ? FROM Standard1 WHERE KEY=?', ['A', 'K'],
  function(err, row) {
    if (err) console.log("lookup failed");
    else console.log("got result " + row.cols[0].value);
  }
);

When you are finished with a PooledConnection instance, call shutdown(callback). Shutting down the pool prevents further work from being enqueued, and closes all open connections after pending requests are complete.

// Shutting down a pool
connection_pool.shutdown(function() { console.log("connection pool shutdown"); });

Logging

Instances of Connection() and PooledConnection() are EventEmitter's and emit log events:

var Connection = require('cassandra-client').Connection;
var con = new Connection({host:'cassandra-host', port:9160, keyspace:'Keyspace1', user:'user', pass:'password'});
con.on('log', function(level, message, obj) {
  console.log('log event: %s -- %j', level, message);
});

The level being passed to the listener can be one of debug, info, warn, error, timing and cql. The message is a string and obj is an object that provides more detailed information.

Regenerating Thrift Definition Files

To regenerate Thrift definition files you need to have thrift compiler installed. You can find information on how to do that on the Thrift website.

git clone git://github.com/apache/cassandra.git
cd cassandra/interface
thrift --gen js:node cassandra.thrift

Things you should know about

Numbers

The Javascript Number type doesn't match up well with the java longs and integers stored in Cassandra. Therefore all numbers returned in queries are BigIntegers. This means that you need to be careful when you do updates. If you're worried about losing precision, specify your numbers as strings and use the BigInteger library.

Decoding

node-cassandra-client supports Cassandra BytesType, IntegerType, LongTime and TimeUUIDType out of the box. When dealing with numbers, the values you retreive out of rows will all be BigIntegers (be wary of losing precision if your numbers are bigger than 2^53--you know, like a timestamp).

BigInteger supports many operations like add, subtract, multiply, etc., and a few others that may come in handy: shift, square, abs, etc. Check the source if you'd like to know more.

We technically have a UUID type, but have not had the need to flesh it out yet. If you find the need to expose more parts of the UUID (timestamp, node, clock sequence, etc.), or would like to implement some operations, patches are welcome.

Todo

  • Full BigInteger documentation.

More Repositories

1

node-elementtree

Port of Python's Element Tree module to Node.js
JavaScript
129
star
2

node-swiz

Serialization and Validation Framework for Objects in REST APIs
JavaScript
96
star
3

rackspace-monitoring-agent-plugins-contrib

Contributed Rackspace Monitoring Agent plugins
Python
71
star
4

glimpse.js

A visualization library built on top of d3.
JavaScript
64
star
5

gutsy

internal dashboard bootstrap
JavaScript
34
star
6

docker-ubuntu-with-updates

https://index.docker.io/u/racker/precise-with-updates/
Python
28
star
7

restkin

RESTkin is a REST API for Twitter's Zipkin
Python
24
star
8

rackspace-monitoring

Python client library for Rackspace Cloud Monitoring based on Apache Libcloud.
Python
24
star
9

node-failure-detector

The Phi accrual failure detector.
JavaScript
21
star
10

gorax

A Rackspace Cloud client library written in Go
Go
13
star
11

libtraceroute

BSD traceroute made into a small library
C
12
star
12

perigee

REST client optimized for use with APIs using JSON for request and response bodies, such as OpenStack.
Go
12
star
13

node-rproxy

A reverse proxy for RESTful services
JavaScript
10
star
14

node-keystone-client

Node.js client for the OpenStack Keystone API.
JavaScript
10
star
15

go-proxy-protocol

A Go library to parse the HA Proxy/stud proxy protocol line
Go
10
star
16

node-proxy-protocol

parser for the node-proxy-protocol based on node-strtok
JavaScript
10
star
17

service-registry

API-driven cloud service built for keeping track of your services and storing configuration values in a centralized place.
JavaScript
9
star
18

node-zookeeper-client

A higher-level ZooKeeper client based on node-zookeeper with support for locking and master election.
JavaScript
9
star
19

devopsjson

DevOps JSON defines a well-known URI for DevOps information.
JavaScript
9
star
20

rackspace-monitoring-rb

Ruby bindings to the Rackspace Cloud Monitoring project
Ruby
8
star
21

rackspace-monitoring-gui

Tiny webapp for Rackspace Cloud Monitoring
JavaScript
8
star
22

python-raxcli

Rackspace Open Cloud command line interface
Python
8
star
23

secure-template

Scripts to handle encrypting secrets shared across a team
Python
7
star
24

eom

Emerging OpenStack WSGI Middleware
Python
7
star
25

cloud-init-docker-build

Build cloud-init with docker scripts
Makefile
7
star
26

rackspace-monitoring-poller

Rackspace Monitoring Poller
Go
6
star
27

python-twisted-web

Twisted web
Python
6
star
28

standupbot

An irc bot and simple web page for doing standups remotely
JavaScript
6
star
29

scrivener

Twisted Scribe Client/Server Framework.
Python
6
star
30

torment

A Study in Fixture Based Testing Frameworking
Python
5
star
31

cookbook-cleanup

This cookbook creates a `cleanup` resource to handle the cleanup of arbitrary paths based on number of files or last modified dates.
Ruby
5
star
32

rackspace_cloudmonitoring_migration_tool

Python
5
star
33

python-twisted-core

Twisted core debian packaging
Python
4
star
34

lnup

Scheme
4
star
35

node-rackspace-shared-utils

Shared Node.js utility modules and functions.
JavaScript
4
star
36

glimpse.js-website

The website for glimpse.js
4
star
37

azr-CustomScriptExtensions

This is a public repository which contains a library of Custom Script Extensions created to support Azure.
Shell
3
star
38

node-service-registry-client

Node.js client for Rackspace Service Registry.
JavaScript
3
star
39

node-command-parser

Node.js command line parser.
JavaScript
3
star
40

salus-docs

Provides overarching documentation for the suite of Salus modules
3
star
41

cloud-init-debian-pkg

Python
3
star
42

python-twisted-words

Twisted words
Python
3
star
43

txzookeeper

Python
3
star
44

salus-telemetry-envoy

Provides the agent manager/relay for Project Salus
Go
3
star
45

rackspace-database

Python
3
star
46

node-moruga

HTTP proxy for API unit-testing and debugging
JavaScript
3
star
47

python-service-registry-client

Python client for Rackspace Service Registry.
Python
3
star
48

protobuf-for-node

git fork of protobuf-for-node
C++
3
star
49

node-zk

node.js native port of the zookeeper client and network protocol
JavaScript
3
star
50

python-twisted-keystone-agent

Twisted Python client for Keystone API.
Python
3
star
51

omnibus

Magical chef-server packaging...magic
C
2
star
52

salus-telemetry-bundle

Java
2
star
53

slogger

irc message indexer and user notifier
Python
2
star
54

python-stats

Python
2
star
55

salus-tools

Salus repository for standalone tools that need to be run against multiple different projects
Go
2
star
56

handy

Python
2
star
57

digital-atg

ATG related configurations and source files
Java
2
star
58

java-service-registry-client

Java client for Rackspace Service Registry.
Java
2
star
59

archaius-rsr

Archaius (configuration library) bindings for Rackspace Service Registry
Java
2
star
60

ele-agent-meta-packages

Shell
2
star
61

kumoru-sdk-go

The official golang Kumoru SDK
Go
2
star
62

cloudmonitoring-demo

A demo site for Cloud Monitoring
JavaScript
2
star
63

ko-validation

Knockout Validation Plugin for the Rackspace Control Panel.
JavaScript
2
star
64

salus-event-engine

Consumes metrics, processes matching event tasks, and produce event notifications
Java
1
star
65

salus-common

Java
1
star
66

tf-hcl

Ruby
1
star
67

python-dcs

Dynamic Chef Solo provides an easier to produce the JSON attributes used by Chef Solo.
Python
1
star
68

salus-telemetry-resource-management

Microservice responsible for managing resouce data
Java
1
star
69

salus-telemetry-presence-monitor

Java
1
star
70

ruby-1.9.3-lucid

ruby 1.9.3 for lucid
C
1
star
71

sip4-qt3

C
1
star
72

mriya

Salaesforce data transformation command line tools that built on top of salesforce bulk api. Look into both 'master' and 'mriya_dmt' branches.
Python
1
star
73

rax-jenkins

Fork of autoscale-chef that focuses on Jenkins
Ruby
1
star
74

salus-event-engine-common

Java
1
star
75

libmemcached

libmemcached upstream + debian
Shell
1
star
76

protobuf

C++
1
star
77

node-v1

JavaScript
1
star
78

ruby-chef-gelf

ruby chef-gelf packaged by gem2deb
Ruby
1
star
79

luvlint

Lua
1
star
80

salus-app-base

1
star
81

nodejs-deb-generator

Shell
1
star
82

salus-telemetry-auth-service

Java
1
star
83

managed-cloud-cloudmonitoring

Python
1
star
84

python-twisted-lore

Twisted lore
Python
1
star
85

python-twisted-conch

Twisted conch
Python
1
star
86

keyczar

keyczar + debian packaging
Python
1
star
87

service-registry-docs

Documentation in Markdown format for Rackspace Service Registry.
1
star
88

python-twisted-news

Twisted news
Python
1
star
89

logformatjson

Simple json formatter for standard python logging
Python
1
star
90

node-rackspace-shared-middleware

Shared Node.js Express middleware.
JavaScript
1
star
91

ruby-gelf

ruby gelf packaged by gem2deb
Ruby
1
star
92

pylibmc

pylibmc debian package
C
1
star
93

salus-telemetry-etcd-adapter

Java
1
star
94

python-twisted-runner

Twisted runner
Python
1
star
95

salus-event-engine-management

Java
1
star
96

python-twisted-names

Twisted names
Python
1
star
97

node-buildbot

nodejs library for interacting with buildbot
JavaScript
1
star
98

salus-telemetry-monitor-management

Java
1
star
99

protobuf-c

Debian package for protobuf-c
Shell
1
star
100

cookbook-required_attributes

Provides methods for forcing required_attributes
Ruby
1
star