cassandra

Bogdan Ailiesei edited this page Jul 23, 2013 · 2 revisions

Cassandra wrapper adaptor over helenus

config

Cassandra can be activated in config.js under boot modules. You can have as many links as you want with cassandra adaptors bound. For this example, we'll only use one link named storage

//config.js
[...]
/**
 * Boot modules
 */
,boot : {
    /**
     * Cassandra example configuration with multiple nodes
     */
     storage : [
         'cassandra'
         , [ {host : '1.2.3.4', port : '9160'}, {host : '1.2.3.5', port : '9160'} ] //nodes
         , 'default-keyspace' //default keyspace
         , { user: '', pass: '', slow : 5 } //various options for the connection
     ]
}
[...]

fetch connection

this.kernel . storage ( keyspace : string, callback : function (err, connection) )

Accessing the link will return (via the callback) a cassandra connection.

excute query

conn . query ( query : string, params : array, callback : function (err, rows) )

Once we have the connection, we can use it to execute a query.

example

//somewhere inside any included file 
this.kernel.storage('keyspace', function(err, conn) {
    if (err) {
        //couldn't fetch a connection for some err.message reason
    } else {
        //success, we'll query now
        conn.query(
            'SELECT something FROM sometable WHERE somecondition = ?',
            [ param ],
            function (err, rows) {
                if (err) {
                    //query failed for some err.message reason
                } else {
                    for (var i = 0, max = rows.length; i < max; i++) {
                        console.log(rows[i].something);
                    }
                }
            }
        );
    }
});