Node.js Driver for DataStax Enterprise

Datetime:2016-08-23 00:42:23          Topic: Node.js           Share

Following the release of DataStax Enterprise (DSE) 5.0 , we announced the availability of a new set of drivers specifically designed for the platform.

The DataStax Enterprise Node.js Driver is built on top of the “core” Node.js driver for Apache Cassandra and supports additional features added by that platform, likeDSE Graph, Geospatial types and Unified Authentication .

The most noticeable change in the DSE driver API is the Client prototype which extends the core driver counterpart and exposes a few additional capabilities:

  • It can execute graph statements
  • It can represent geospatial data
  • It uses a special load balancing policy by default, DseLoadBalancingPolicy
  • It uses Execution profiles to better handle mixed workloads

Upgrading from the Core Driver

Upgrading from the cassandra-driver can be as simple as changing the import statement to point to the dse package:

const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['host1'] });

Becomes:

const dse = require('dse-driver');
const client = new dse.Client({ contactPoints: ['host1'] });

The DSE driver module also exports the same submodules as the core driver.

Unified Authentication

For clients connecting to a cluster secured with DSE Unified Authentication , two authentication providers are included:

You can set the authentication provider when creating the Client instance:

const dse = require('dse-driver');
const client = new dse.Client({
  contactPoints: ['h1', 'h2'], 
  keyspace: 'ks1',
  authProvider: new dse.auth.DseGssapiAuthProvider()
});

See the API docs for DseGssapiAuthProvider and DsePlainTextAuthProvider for more information.

DSE Graph Support

The DSE driver supports two different query languages: CQL for interacting with Cassandra tables, and Gremlin , the graph traversal language that allows you to interact withDSE Graph.

Client prototype includes the executeGraph() method to execute graph queries:

client.executeGraph('g.V()', function (err, result) {
  assert.ifError(err);
  const vertex = result.first();
  console.log(vertex.label);
});

Check out DSE 5.0 Graph documentation more information on DSE Graph / Gremlin and the driver documentation on graph for further information about executing graph queries and setting graph options.

Geospatial Types

DataStax Enterprise 5.0 comes with a set of additional CQL types to represent geospatial data: PointType , LineStringType and PolygonType .

cqlsh> CREATE TABLE points_of_interest(name text PRIMARY KEY, coords 'PointType');
cqlsh> INSERT INTO points_of_interest (name, coords) VALUES ('Eiffel Tower', 'POINT(48.8582 2.2945)');

The DSE driver includes encoders and representations of these types in the geometry module that can be used directly as parameters in queries. All Javascript geospatial types implement toString() , that returns the string representation in Well-known text format , and toJSON() , that returns the JSON representation in GeoJSON format .

const dse = require('dse-driver');
const Point = dse.geometry.Point;
const insertQuery = 'INSERT INTO points_of_interest (name, coords) VALUES (?, ?)';
client.execute(insertQuery, ['Eiffel Tower', new Point(48.8582, 2.2945)], callback);
const selectQuery = 'SELECT coords FROM points_of_interest WHERE name = ?';
client.execute(selectQuery, ['Eiffel Tower'], function (err, result) {
  assert.ifError(err);
  const row = result.first();
  const point = row['coords'];
  console.log(point instanceof Point); // true
  console.log('x: %d, y: %d', point.x, point.y); // x: 48.8582, y: 2.2945
});

See the driver documentation on geospatial types for more details.

Working with Mixed Workloads

The driver featuresExecution Profiles that provide a mechanism to group together a set of configuration options and reuse them across different query executions.

Execution Profiles are specially useful when dealing with different workloads like DSE Graph and CQL workloads, allowing you to use a single Client instance for all workloads, for example:

const client = new Client({ 
  contactPoints: ['host1'], 
  profiles: [
    new ExecutionProfile('time-series', {
      consistency: consistency.localOne
      readTimeout: 30000,
      serialConsistency: consistency.localSerial
    }),
    new ExecutionProfile('graph', {
      loadBalancing: new DseLoadBalancingPolicy('graph-us-west')
      consistency: consistency.localQuorum
      readTimeout: 10000,
      graphOptions: { name: 'myGraph' }
    })
  ]
});

// Use an execution profile for a CQL query
client.execute('SELECT * FROM system.local', null, { executionProfile: 'time-series' }, callback);

// Use an execution profile for a gremlin query
client.executeGraph('g.V().count()', null, { executionProfile: 'graph' }, callback);

Looking Forward

We will continue to develop and improve the Node.js driver for Apache Cassandra as it represents the core building block on which DataStax Enterprise Node.js Driver is built, and also continue to add DSE-specific functionalities in this new driver.

To provide feedback use the following:

The DSE driver is available on npm: dse-driver .

Note that the DataStax Enterprise Node.js Driver is published under specific license terms that allow its usage only in conjunction with DataStax Enterprise software .





About List