public
Last active

Examples of how to use node-cassandra-client

  • Download Gist
node-cassandra-client-examples.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
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);
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.