Git Product home page Git Product logo

cql-client's Introduction

cql-client

Build Status Coverage Status

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 --save

Create 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.

  • options
  • hosts List of cassandra hosts you are connecting to. Use array to connect multiple hosts.
  • keyspace Name of the default keyspace.
  • autoDetect Set true to detect peers automatically. (Optional)
  • connsPerHost The size of connection pool per host. (Default: 2)
  • connectTimeout Milliseconds to determine connections are timed out. (Default: 5000)
  • reconnectInterval Milliseconds of interval for reconnecting. (Default: 5000)
  • preparedCacheSize Size 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.

  • cb callback 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.

  • query Query statement to be executed.
  • values Array of bound values.
  • options
  • consistencyLevel Consistency level of the query. cql.CL.ONE,THREE,QUORUM,ALL,LOCAL_QUORUM,EACH_QUORUM
  • pageSize Paging size of the result.
  • pagingState Paging state of the query.
  • serialConsistency Serial consistency of the query.
  • callback Callback 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
  • row when cursor have an available row
  • end when cursor ends
  • error when error occurs

cursor.abort()

Abort cursor. It will fire end event.

Batch

batch.add(query, [values])

Add query to the batch.

  • query Query statement.
  • values Array of values to be bound.

batch.option(options)

Set option parameters to the batch.

  • options Same 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

cql-client's People

Contributors

suguru avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.