Coder Social home page Coder Social logo

sebinsua / node-cassandra-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from racker/node-cassandra-client

0.0 3.0 0.0 418 KB

CQL client for Cassandra written in Node.js.

Home Page: http://code.google.com/a/apache-extras.org/p/cassandra-node/

License: Apache License 2.0

Shell 1.84% JavaScript 98.16%

node-cassandra-client's Introduction

node-cassandra-client

node-cassandra-client is a Node.js CQL 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.

Installation

$ npm install cassandra-client

Build status

Build Status

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.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.

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.

node-cassandra-client's People

Contributors

gdusbabek avatar kami avatar eevans avatar ctavan avatar allanca avatar robert-chiniquy avatar pquerna avatar peters avatar

Watchers

Seb Insua 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.