Note: This post uses the the Couchbase Analytics Data Definition Language as of the version 5.5 preview release.  For updates and information on breaking changes in newer versions, please refer to Changes to the Couchbase Analytics Service.

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.

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:

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:

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:

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:

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:

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:

Now we can start developing our application. Create an app.js file in your project with the following:

Not too much is happening in the above code, but we’ll break it down anyways.

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.

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

Posted by Nic Raboy, Developer Advocate, Couchbase

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

Leave a reply