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.

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.

As of right now Couchbase Analytics is available 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.

Assuming that the sample Bucket was activated, execute the following command from the Analytics tab of the Couchbase Administrative Dashboard:

What we’re doing in the above command is creating an Analytics Bucket based off the travel-sample Bucket.

Before we can start working with the data, we need to create some shadow datasets for our Analytics Bucket. These can be created by executing the following:

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 smooth 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/App.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.

Conclusion

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