Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple example of how to use the Cassandra database (cql) and node.js

I was trying to query and insert things into a cassandra DB. I know how to do that in the command line for cql (cqlsh), but was unsure on how to basically interact with the cassandra database through node.js.

If someone could give me very simple code examples of how to do this, it would be very useful!

Without thrift, only node.js is allowed.

Thanks in advance.

like image 494
Charlie Parker Avatar asked Mar 05 '14 19:03

Charlie Parker


1 Answers

You can use any of the available drivers. All of them provide a callback based approach, common in Node.js.

If you are looking for a native driver in Node.js for Cassandra without a thrift dependency, you should consider DataStax Node.js driver for Cassandra.

Example:

var cassandra = require('cassandra-driver');
var client = new cassandra.Client({contactPoints: ['host1', 'host2']});
var query = 'SELECT email, last_name FROM user_profiles WHERE key=?';
client.execute(query, ['guy'], function(err, result) {
  console.log('got user profile with email ' + result.rows[0].email);
});

Disclaimer: I'm the author of node-cassandra-cql and contributor in the DataStax Node.js driver for Cassandra.

UPDATE: Recommended to DataStax driver instead of node-cassandra-cql

like image 130
jorgebg Avatar answered Sep 22 '22 10:09

jorgebg