Monday, 19 March, 2018 UTC


Summary

The Couchbase Analytics Service, sometimes referred to as CBAS, is a great thing for Couchbase and your NoSQL data needs because it allows you to create and run potentially complex queries against massive amounts of data efficiently using a familiar SQL dialect.
We’re going to see how to use this analytics service, new as of Couchbase 5.5, with the Node.js SDK for Couchbase.
As of right now, Couchbase 5.5 is available as Developer Build, however, code and concepts are likely to remain similar, if not the same, when a stable release is pushed.
For this example, we’re going to be working with the travel-sample example Bucket that can be optionally installed with Couchbase Server. It isn’t considered a huge dataset, but it will work with this example.
Configuring a Couchbase Analytics Dataset
Within the Analytics tab of the Couchbase Administrative Dashboard, execute the following query:
CREATE BUCKET travel WITH { "name":"travel-sample" };
The above query will create a travel Analytics Bucket based of the Couchbase travel-sample Bucket.
With the bucket created, we can create a shadow dataset to work with. Execute the following queries:
CREATE SHADOW DATASET airlines ON travel WHERE `type` = "airline";
CREATE SHADOW DATASET airports ON travel WHERE `type` = "airport";
The above queries will create two shadow datasets based on data that exists in the travel-sample bucket. While the shadow datasets are created, they need to be initialized:
CONNECT BUCKET travel;
Once initialized, the Analytics service will begin making its copy of the documents and actively monitor for changes. We can test the data by executing a query like the following:
SELECT * FROM airports;
The above query will find all documents from our shadow dataset. In other words, we’ll get all documents that have a type property of airline.
This is all great, but as of now, we’ve only used the Couchbase Administrative Dashboard. We want to be able to run queries against our Analytics dataset from Node.js.
Querying CBAS from a Node.js Application
Assuming you have Node.js installed, create a new project directory and execute the following command:
npm init -y
The above command will create a new package.json file at your CLI path. In order to communicate with Couchbase, we’re going to need the SDK. It can be installed by executing the following from the CLI:
npm install couchbase --save
Now we can start developing our application. Create an app.js file in your project with the following:
const Couchbase = require("couchbase");

var cluster = new Couchbase.Cluster("couchbase://localhost");
cluster.authenticate("travel", "123456");
cluster.enableCbas(["localhost:8095"]);

var statement = "SELECT * FROM airports";
var query = Couchbase.CbasQuery.fromString(statement);

cluster.query(query, (error, result) => {
    if(error) {
        throw error;
    }
    console.log(result);
});
Not too much is happening in the above code, but we’ll break it down anyways.
var cluster = new Couchbase.Cluster("couchbase://localhost");
cluster.authenticate("travel", "123456");
cluster.enableCbas(["localhost:8095"]);
Before we can start executing queries, we need to connect to the cluster and enable interactions with the Analytics service. We also need to authenticate to the cluster with a user that has permission to use Analytics. In this example, Analytics is active on the node that we’re connecting to.
Notice that we’re not connecting to a Bucket. Had we wanted to use N1QL, we’d open a Bucket, but we’re not using N1QL even if it might look similar.
var statement = "SELECT * FROM airports";
var query = Couchbase.CbasQuery.fromString(statement);

cluster.query(query, (error, result) => {
    if(error) {
        throw error;
    }
    console.log(result);
});
Using a simple Analytics query, we can parse it and execute it. The results of the query will be returned and printed to the logs.
Not too bad right?
Since Analytics uses SQL++, you can make your queries much more complicated using other operators such as WHERE, subqueries, and operations on collections.
Conclusion
You just saw how to create a simple Node.js application that uses the Couchbase Analytics Service (CBAS) rather than N1QL. Analytics and SQL++ is not a replacement to N1QL, but instead a compliment for much larger or complicated datasets, where as N1QL shines on smaller datasets.
To learn more about Analytics or the Couchbase Node.js SDK, visit the Couchbase Developer Portal.  Learn about other new features available in the Couchbase Server 5.5 Developer Build.
The post Using Couchbase Analytics with Node.js and JavaScript appeared first on The Couchbase Blog.