DataStax Node.js Driver for Apache Cassandra
A modern, feature-rich and highly tunable Node.js client library for Apache Cassandra (1.2+) and DataStax Enterprise (3.1+) using exclusively Cassandra's binary protocol and Cassandra Query Language v3.
$ npm install cassandra-driver
const cassandra = ;const client = contactPoints: 'h1' 'h2' keyspace: 'ks1' ;const query = 'SELECT name, email FROM users WHERE key = ?';client;
Alternatively, you can use the callback-based execution for all asynchronous methods of the API.
The driver will prepare the query once on each host and execute the statement with the bound parameters.
// Use query markers (?) and parametersconst query = 'UPDATE users SET birth = ? WHERE key=?';const params = 1942 10 1 'jimi-hendrix' ;// Set the prepare flag in the query optionsclient;
#stream() methods, the driver parses each row as soon as it is received,
yielding rows without buffering them.
// Reducing a large resultclient;
#stream() method works in the same way but instead of callback it returns a Readable Streams2 object
objectMode that emits instances of
It can be piped downstream and provides automatic pause/resume logic (it buffers when not read).
For example: Consider the following UDT and table
CREATE TYPE address (street text,city text,state text,zip int,phones set<text>);CREATE TABLE users (name text PRIMARY KEY,email text,address frozen<address>);
const query = 'SELECT name, address FROM users WHERE key = ?';client;
Read more information about using UDTs with the Node.js Driver.
All driver methods use a default
fetchSize of 5000 rows, retrieving only first page of results up to a
maximum of 5000 rows to shield an application against accidentally large result sets. To retrieve the following
records you can use the
autoPage flag in the query options of
//Imagine a column family with millions of rowsconst query = 'SELECT * FROM largetable';client;
You can execute multiple statements in a batch to update/insert several rows atomically even in different column families.
const queries =query: 'UPDATE user_profiles SET email=? WHERE key=?'params: emailAddress 'hendrix'query: 'INSERT INTO user_track (key, text, date) VALUES (?, ?, ?)'params: 'hendrix' 'Changed email';client;
EventEmitter and emit
level being passed to the listener can be
Help us focus our efforts! Provide your input on the Platform and Runtime Survey (we kept it short).
The development effort to provide an up to date, high performance, fully featured Node.js Driver for Apache Cassandra will continue on this project, while node-cassandra-cql will be discontinued.
Copyright 2016 DataStax
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.