In the world of Big Data, where we have gogolbytes of information available about all aspects of our applications and the users that are interacting with them, being able to analyze all of this data and provide insightful intelligence is paramount. At Couchbase, we are working to build data management platforms that let you take advantage of this data. This will then eventually be accessible from a variety of industry-standard analysis platforms like Tableau, QlikView and Microstrategy as well as next generation NoSQL analytics platforms such as Cloud9 and SlamData, however it is also important that developers have APIs to access the systems that store and analyze this data. With this level of direct access, these developers have the flexibility to build everything from simple dashboards to complete custom reporting for their users.

As of version 2.2.4 of our Node.js SDK, we now have experimental support for performing Couchbase Analytics queries against a cluster. The API is very similar to the existing APIs available for performing N1QL queries in the Node.js SDK.

// Connect to the local cluster and enable experimental Couchbase Analytics support. var cluster = new couchbase.Cluster('couchbase://localhost'); cluster.enableCbas(['localhost:8095']); // Perform a simple query, selecting all users with a specific email. var qs = 'SELECT * FROM users WHERE email="tom@example.com"'; var q = couchbase.CbasQuery.fromString(qs); cluster.query(q, function(err, res) { if (err) { throw err; } console.log(res); }); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // Connect to the local cluster and enable experimental Couchbase Analytics support. var cluster = new couchbase . Cluster ( 'couchbase://localhost' ) ; cluster . enableCbas ( [ 'localhost:8095' ] ) ; // Perform a simple query, selecting all users with a specific email. var qs = 'SELECT * FROM users WHERE email="tom@example.com"' ; var q = couchbase . CbasQuery . fromString ( qs ) ; cluster . query ( q , function ( err , res ) { if ( err ) { throw err ; } console . log ( res ) ; } ) ;

As you might notice, the difference between our N1QL APIs and the Couchbase Analytics APIs is that with Couchbase Analytics you must perform your queries at the cluster-level and use CbasQuery to instantiate your query object.

Using these new APIs, we have also built a new dashboard demonstration application which pulls together a few well-known libraries in the Node.js world to show how you might turn these new APIs into a useful interface that can display business-specific metrics that are useful to you and your colleagues.

How the Dashboard Works

The dashboard we have put together consists of two separate components. First, a server-side component which is running the Node.js SDK and provides a restful interface. Second, a client-side component which is a simple HTML5 page. The server-side component will serve the client-side component from the root of its web-service to make things easier.

The client-side of the application is a simple HTML and Javascript page which contains a small form allowing you to specify some parameters, a large text section which displays all the queries which have been executed against your Couchbase Analytics cluster, as well as 3 graphs which are used to present the data from your queries. When the page loads, or when you click the update button, we dispatch requests to the server-side application requesting various pieces of information about our travel-sample data. When a reply is received from the server, we then pass this information to Chart.js which generates our graphs.

The code for this is pretty straightforward.

$.get('/pricestats?' + formData) .done(function(data) { addNarration(data.narration); var info = data.data; config1.data.labels = []; config1.data.datasets[0].data = []; for (var i = 0; i < info.length; ++i) { config1.data.labels.push(info[i].grpdate); config1.data.datasets[0].data.push(info[i].metric); } myChart1.update(); }).fail(function(err) { addNarrationText(err.toString()); }); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 $ . get ( '/pricestats?' + formData ) . done ( function ( data ) { addNarration ( data . narration ) ; var info = data . data ; config1 . data . labels = [ ] ; config1 . data . datasets [ 0 ] . data = [ ] ; for ( var i = 0 ; i < info . length ; ++ i ) { config1 . data . labels . push ( info [ i ] . grpdate ) ; config1 . data . datasets [ 0 ] . data . push ( info [ i ] . metric ) ; } myChart1 . update ( ) ; } ) . fail ( function ( err ) { addNarrationText ( err . toString ( ) ) ; } ) ;

It simply uses Express to start a web-server which responds to any root requests with the client-side page for this application. It additionally registers a couple of REST endpoints which the application uses to update its graphs. Each of these REST endpoints front-ends a SQL++ query against the Couchbase Analytics cluster and then returns the results of that query back to the client-side of the application to be graphed.

var qs = 'SELECT ' + ' parse_date(SUBSTR(e.date, 0, 10), "Y-M-D") AS grpdate, ' + 'SUM(e.price) AS metric ' + 'FROM users u ' + ' UNNEST u.flights e ' + 'WHERE parse_date(SUBSTR(e.date, 0, 10), "Y-M-D") >= parse_date("2016-10-01", "Y-M-D") ' + ' AND parse_date(SUBSTR(e.date, 0, 10), "Y-M-D") < parse_date("2016-11-01", "Y-M-D") ' + 'GROUP BY grpdate ' + 'ORDER BY grpdate;'; var q = couchbase.CbasQuery.fromString(qs); cluster.query(q, function(err, qres) { if (err) { res.status(400).send(err); return; } res.send({ data: qres, narration: [ ['sql', qs] ] }); }); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 var qs = 'SELECT ' + ' parse_date(SUBSTR(e.date, 0, 10), "Y-M-D") AS grpdate, ' + 'SUM(e.price) AS metric ' + 'FROM users u ' + ' UNNEST u.flights e ' + 'WHERE parse_date(SUBSTR(e.date, 0, 10), "Y-M-D") >= parse_date("2016-10-01", "Y-M-D") ' + ' AND parse_date(SUBSTR(e.date, 0, 10), "Y-M-D") < parse_date("2016-11-01", "Y-M-D") ' + 'GROUP BY grpdate ' + 'ORDER BY grpdate;' ; var q = couchbase . CbasQuery . fromString ( qs ) ; cluster . query ( q , function ( err , qres ) { if ( err ) { res . status ( 400 ) . send ( err ) ; return ; } res . send ( { data : qres , narration : [ [ 'sql' , qs ] ] } ) ; } ) ;

In addition to the RESTful service provided by the server-side application, we have additionally included automatic creation of the necessary Couchbase Analytics shadow buckets for the travel sample and inserting 1000 entries worth of randomly generated flight purchase data to give us something interesting to look at.

var qs = 'CREATE BUCKET tsBucket WITH {"name":"travel-sample","nodes":"127.0.0.1"};' + 'CREATE SHADOW DATASET airlines ON tsBucket WHERE `type` = "airline";' + 'CREATE SHADOW DATASET airports ON tsBucket WHERE `type` = "airport";' + 'CREATE SHADOW DATASET hotels ON tsBucket WHERE `type` = "hotel";' + 'CREATE SHADOW DATASET landmarks ON tsBucket WHERE `type` = "landmark";' + 'CREATE SHADOW DATASET routes ON tsBucket WHERE `type` = "route";' + 'CREATE SHADOW DATASET users ON tsBucket WHERE `type` = "user";' + 'CONNECT BUCKET tsBucket WITH {"password":""};'; var q = couchbase.CbasQuery.fromString(qs); cluster.query(q, function(err, res) { // We ignore errors here since they are usually just // 'dataset already exists'. callback(null); }); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 var qs = 'CREATE BUCKET tsBucket WITH {"name":"travel-sample","nodes":"127.0.0.1"};' + 'CREATE SHADOW DATASET airlines ON tsBucket WHERE `type` = "airline";' + 'CREATE SHADOW DATASET airports ON tsBucket WHERE `type` = "airport";' + 'CREATE SHADOW DATASET hotels ON tsBucket WHERE `type` = "hotel";' + 'CREATE SHADOW DATASET landmarks ON tsBucket WHERE `type` = "landmark";' + 'CREATE SHADOW DATASET routes ON tsBucket WHERE `type` = "route";' + 'CREATE SHADOW DATASET users ON tsBucket WHERE `type` = "user";' + 'CONNECT BUCKET tsBucket WITH {"password":""};' ; var q = couchbase . CbasQuery . fromString ( qs ) ; cluster . query ( q , function ( err , res ) { // We ignore errors here since they are usually just // 'dataset already exists'. callback ( null ) ; } ) ;

Getting the Example Dashboard

Getting started with our new demonstration is as simple as visiting is GitHub repo which is located at: https://github.com/couchbaselabs/node-cbasdashdemo and following the Getting Started guide available in the included README.

Getting the Node.js Release

As with all of our Node.js releases, we have published this version to NPM and it can be installed with a simple invocation of npm:

npm install Couchbase@2.2.4 1 npm install Couchbase @ 2.2.4

We also ship prebuilt binaries as part of our npm release process, allow you to get started with the Node.js SDK without any further hassles (note that Node.js v7.0.0 does not currently have prebuilt binaries of the Couchbase module available and requires a working compiler to install).