Question

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.

Était-ce utile?

La solution

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

Autres conseils

node-cassandra-cql module has been retired. Datastax has their cassandra-driver module.

var cassandra = require('cassandra-driver');
var client = new cassandra.Client({contactPoints: ['host1', 'host2'], keyspace: 'ks1'});
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);
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top