Note: This post has been updated to reflect the production version of the Couchbase Analytics Data Definition Language.

Not too long ago I had written about using Couchbase’s new Analytics service, sometimes referred to as CBAS, within a Node.js application. In this example we had set up an Analytics dataset based on a sample Bucket and issued a query from our code.

While Node.js is one of my favorite development technologies, it isn’t the only one that supports Couchbase. Java is still a great language, so we’re going to see how to work with Couchbase Analytics in our application.

Couchbase Analytics is available starting in the Couchbase Server 5.5 release.

Preparing a Dataset for Couchbase Analytics Queries

Before we can start querying our data with SQL++ and Analytics, we have to configure our dataset. The simplest thing to do for this example would be to use the travel-sample Bucket that is available with Couchbase Server.  Once this bucket has been activated, we can prepare shadow datasets against this bucket with simple commands from the Analytics query console:

The two shadow datasets are very simple in comparison to what they could be. You could write a complex query to narrow down the data that each dataset will contain, so the above examples are broad. We’re essentially saying, give us a dataset for all the airports and a dataset for all the airlines.

Even though we created the datasets, they need to be initialized still. Initializing the Bucket can be done with the following command:

After the Analytics Bucket has been initialized, the Analytics service will start copying documents and monitoring them for changes.

You can validate that everything went smoothly by executing the following:

Again, the above query is quite simple in comparison what we could have created. The great thing about the Analytics service is that your queries can be significantly more complex without worrying at all about creating efficient indexes. This is all taken care of for you.

Developing an Application with Java for SQL++ Querying

With a few Analytics datasets ready to go, we can start writing our Java code. I personally use Gradle when it comes to Java. If you’re using it too, you can create a new project by executing the following from the command line:

The above command will create a new Java application. Next we’ll want to install the Couchbase Java SDK. Open the project’s build.gradle file and make it look like the following:

Notice that we’ve included the client library as well as a Maven repository. We can test that our project runs without errors by executing the following Gradle command:

Assuming everything is running smooth up until now, let’s get some code going. Open the project’s src/main/java/ file and include the following. We’ll break it down after.

In the above code we are first establishing a connection to our cluster. Assuming that an account was created, we are authenticating with our cluster using the RBAC account that we have in our Couchbase Administrative Dashboard.

While we are not using a standard Couchbase Bucket for this example, we have to open it to initialize the application resources for what follows. Using the open Bucket, we can execute an AnalyticsQuery that has SQL++ in it. The results of said query are printed to the console logs.

While this was a simple example, adding complexity won’t change anything we’ve discussed.


You just saw how to use the Couchbase Analytics service with the Java SDK. To reiterate, Analytics is a powerful way to query massive amounts of data using potentially complex queries without having to worry about indexing, all while having extremely fast results.

To learn more about developing with Java, check out the Couchbase Developer Portal.  Read more about the other new features available in the Couchbase Server 5.5 release.


Posted by Peter Reale

Peter Reale is a Senior Solutions Engineer at Couchbase and has been in the data business since 1984. He is based in Los Angeles.

Leave a reply