cql-client v0.3.3
cql-client
Node.js driver for cassandra on Cassandra Binary Protocol v2.
Features
- CQL binary protocol v2
- Automatic discovery of cluster peers
- Fail-over cluster peers
- Retry queries when disconnected from servers
- Paging large result set with paging state
- Events
Quick start
var client = require('cql-client').createClient({
hosts: ['127.0.0.1']
});
client.execute('SELECT * FROM system.peers', function(err, rs) {
var rows = rs.rows;
rows.forEach(function(row) {
console.log(row.peer);
console.log(row.host_id);
..
});
});Usage
Install library
npm install cql-client --saveCreate a client
var cql = require('cql-client');
var client = cql.createClient({ hosts: ['127.0.0.1'], keyspace: 'ks' });Execute queries
client.execute('SELECT * FROM table', function(err, rs) {
rs.rows.forEach(function(row) {
console.log(row);
});
});Execute prepared queries
client.execute(
'INSERT INTO table (id, v1, v2) VALUES (?, ?, ?)',
[1000, 'foo', 'bar'],
function(err) {
...
}
);Batch
var batch = client.batch();
batch.add('UPDATE table SET v1 = ? WHERE id = ?', ['101', 1001]);
batch.add('UPDATE table SET v1 = ? WHERE id = ?', ['101', 1001]);
batch.add('DELETE FROM table WHERE id = ?', ['102']);
batch.commit(function(err) {
..
});Read data with cursor
var cursor = client.execute('SELECT * FROM table', function(err, rs) {
var cursor = rs.cursor();
cursor.on('row', function(row) {
console.log(row);
});
cursor.on('error', function(err) {
..
});
cursor.on('end', function() {
console.log('done');
});
});API
CQL
cql.createClient(options)
Create a client with options. The created client will connect the cluster automatically.
optionshostsList of cassandra hosts you are connecting to. Use array to connect multiple hosts.keyspaceName of the default keyspace.autoDetectSet true to detect peers automatically. (Optional)connsPerHostThe size of connection pool per host. (Default: 2)connectTimeoutMilliseconds to determine connections are timed out. (Default: 5000)reconnectIntervalMilliseconds of interval for reconnecting. (Default: 5000)preparedCacheSizeSize of cache for prepared statement. (Default: 10000)
Client
client.connect(cb)
Connect to the cluster. Note that you do not need to call the method since client will connect automatically.
cbcallback when connected
client.close()
Close the connection to the cluster.
client.getAvailableConnection()
Get available connection from connections.
client.execute(query, values, options, callback)
Execute CQL. It will use prepared query when values are specified.
queryQuery statement to be executed.valuesArray of bound values.optionsconsistencyLevelConsistency level of the query. cql.CL.ONE,THREE,QUORUM,ALL,LOCAL_QUORUM,EACH_QUORUMpageSizePaging size of the result.pagingStatePaging state of the query.serialConsistencySerial consistency of the query.callbackCallback with error and result set.
client.batch()
Create batch instance. See Batch.
ResultSet
Result data of the query.
resultSet.metadata
Metadata of result rows and columns.
resultSet.rows
Array of rows.
resultSet.cursor()
Create cursor instance of the result.
Cursor
cursor.on(event, listener)
Register event listener.
- Events
rowwhen cursor have an available rowendwhen cursor endserrorwhen error occurs
cursor.abort()
Abort cursor. It will fire end event.
Batch
batch.add(query, values)
Add query to the batch.
queryQuery statement.valuesArray of values to be bound.
batch.option(options)
Set option parameters to the batch.
optionsSame as options of client.execute()
batch.commit(callback)
Commit queries. It will clean up queries in the batch after committing.
TODO
- Authentication
License
See LICENSE
Copyright (C) Suguru Namura
patched cql-protocol. See LICENSE and cql-protocol