Skip to content

Instantly share code, notes, and snippets.

@gdusbabek
Created April 12, 2012 17:31
Show Gist options
  • Save gdusbabek/2369391 to your computer and use it in GitHub Desktop.
Save gdusbabek/2369391 to your computer and use it in GitHub Desktop.
Examples of how to use node-cassandra-client
var async = require('async');
function doSimpleConnect(callback) {
var Connection = require('cassandra-client').Connection;
// these are the connection parameters you need to connecto to Cassandra.
var connectionOptions = {
host: '127.0.0.1',
port: 19170,
keyspace: 'EXAMPLE_KS',
use_bigints: false
};
var con = new Connection(connectionOptions);
con.connect(function(err) {
// if err != null, something bad happened.
// else, assume all is good. your connection is ready to use.
if (!err) {
con.close(callback);
} else {
callback(err);
}
});
}
var PooledConnection = require('cassandra-client').PooledConnection;
function doPoolConnect(callback) {
var conOptions = { hosts: ['127.0.0.1:19170'],
keyspace: 'EXAMPLE_KS',
use_bigints: false };
var con = new PooledConnection(conOptions);
con.shutdown(function() {
// no error object by design.
callback();
});
}
function doComplexInsert(callback) {
var conOptions = { hosts: ['127.0.0.1:19170'],
keyspace: 'EXAMPLE_KS',
use_bigints: false };
var con = new PooledConnection(conOptions);
var cql = 'UPDATE very_complex_cf SET ?=?, ?=?, ?=? where KEY=?';
var params = ['string_col', 'string_value',
'uuid_col', '6f8483b0-65e0-11e0-0000-fe8ebeead9fe',
'int_col', 42,
'complex_insert_row'];
con.execute(cql, params, function(err) {
// demonstrates use of a callback. A simplification would have been:
// con.execute(cql, params, callback);
if (err) {
console.log(err);
}
con.shutdown(callback);
});
}
function doComplexBatchInsert(callback) {
var conOptions = { hosts: ['127.0.0.1:19170'],
keyspace: 'EXAMPLE_KS',
use_bigints: false };
var con = new PooledConnection(conOptions);
con.execute('BEGIN BATCH USING CONSISTENCY ONE \
UPDATE very_complex_cf SET ?=?, ?=?, ?=? where KEY=?; \
UPDATE very_complex_cf SET ?=?, ?=?, ?=? where KEY=?; \
UPDATE very_complex_cf SET ?=?, ?=?, ?=? where KEY=?; \
APPLY BATCH;',
[
'string_col', 'value for string col in row_1',
'uuid_col', '6f8483b0-65e0-11e0-0000-fe8ebeead9ff',
'int_col', 25,
'complex_batch_row_1',
'string_col', 'value for string col in row_1',
'uuid_col', '6f8483b0-65e0-11e0-0000-fe8ebeeada00',
'int_col', 26,
'complex_batch_row_4',
'string_col', 'value for string col in row_1',
'uuid_col', '6f8483b0-65e0-11e0-0000-fe8ebeeada02',
'int_col', 27,
'complex_batch_row_3'
],
function(err) {
if (err) {
console.log(err);
}
con.shutdown(callback);
}
);
}
function doSelectAll(callback) {
var conOptions = { hosts: ['127.0.0.1:19170'],
keyspace: 'EXAMPLE_KS',
use_bigints: false };
var con = new PooledConnection(conOptions);
con.execute('SELECT * from very_complex_cf', [], function(err, rows) {
if (!err) {
console.log(rows.length); // should be 4 at this point.
rows.forEach(function(row) {
console.log(row.key);
// access column names and values by index.
for (var i = 0; i < row.colCount(); i++) {
console.log(row.cols[i].name);
console.log(row.cols[i].value);
}
// access column values by hash.
console.log(row.colHash['string_col']); // it's a string
console.log(row.colHash['uuid_col']); // it's an instance of require('cassandra-client').UUID
console.log(row.colHash['int_col']); // it's a number.
});
}
con.shutdown(callback);
});
}
function doSelectiveSelect(callback) {
var conOptions = { hosts: ['127.0.0.1:19170'],
keyspace: 'EXAMPLE_KS',
use_bigints: false };
var con = new PooledConnection(conOptions);
con.execute(
'SELECT uuid_col FROM very_complex_cf WHERE KEY=?',
['complex_batch_row_1'],
function(err, rows) {
if (!err) {
// rows.length should == 1.
var row = rows[0];
console.log(row.key);
console.log(row.colCount()); // should be 1 also.
// getting the values out is an exercise for the reader.
}
con.shutdown(callback);
});
}
function doComplexColumnNames(callback) {
var conOptions = { hosts: ['127.0.0.1:19170'],
keyspace: 'EXAMPLE_KS',
use_bigints: false };
var con = new PooledConnection(conOptions);
var updateArgs = [ '6f8483b0-65e0-11e0-0000-fe8ebeead9ff', new Buffer([1,2,3,4,5]), 'uuid_row_1' ];
async.series([
function insert(callback) {
con.execute('UPDATE complex_cf set ?=? where KEY=?', updateArgs, callback);
},
function select(callback) {
con.execute('SELECT * from complex_cf WHERE KEY=?', ['uuid_row_1'], function(err, rows) {
if (!err) {
// rows.length should == 1.
// first access the col by index (when you know it).
// col name should be an instance of UUID.
console.log(rows[0].cols[0].name);
// col value should be <Buffer 01 02 03 04 05>
console.log(rows[0].cols[0].value);
// if you don't know the col index, you need to use the col hash.
// of course, this assumes you know the column name.
// in this case, it should point to the same buffer already referenced.
console.log(rows[0].colHash['6f8483b0-65e0-11e0-0000-fe8ebeead9ff']);
}
callback(err);
});
}
], function(err) {
if (err) {
console.log(err);
}
con.shutdown(callback);
});
}
// run them all.
async.series([
doSimpleConnect,
doPoolConnect,
doComplexInsert,
doComplexBatchInsert,
doSelectAll,
doSelectiveSelect,
doComplexColumnNames
], function(err) {
if (err) {
console.log(err);
} else {
console.log('all good');
}
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment