Last active
December 18, 2015 09:59
-
-
Save ajjain/5765624 to your computer and use it in GitHub Desktop.
This Gist demonstrate on how to integrate NodeJs with Cassandra. I researched both cassandra-node and Helenus to approach the solution, found Helenus very handful could be integrated with ease but lacks composite column support. Cassandra-node was my default choice since I had to handle composites.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
/* | |
- Make sure you have downloaded node-cassandra-client and related dependencies and made it available | |
- at the suitable place. | |
*/ | |
var cassandraClient = require('./node-cassandra-client.js'); | |
var hosts = ['host1:port1','host2:port2','host3:port3']; | |
var connection_pool = new cassandraClient.PooledConnection({'hosts': hosts, 'keyspace': 'keyspacename'}); | |
http.createServer( | |
function(request, response){ | |
console.log('Received request at '+request.url); | |
response.writeHead(200,{'content-type': 'text/plain'}); | |
response.end('Received a request !!'); | |
} | |
).listen(9999); | |
console.info("Server started running on 9999 port"); | |
/* | |
- This is actually what you are looking for right? | |
*/ | |
connection_pool.execute('SELECT * FROM message limit 10', [], | |
function(err, rows) { | |
if (err){ | |
console.log("lookup failed"); | |
} | |
else{ | |
for(var i=0; i<rows.length; i++){ | |
for(var j=0; j<rows[i].cols.length; j++){ | |
console.log("got result " + rows[i].cols[j].name + ":" + rows[i].cols[j].value); | |
} | |
console.log("-------------------------------------------"); | |
} | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment