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:
1 2 |
create dataset airlines on `travel-sample` where `type` = "airline"; create dataset airports on `travel-sample` where `type` = "airport"; |
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:
1 |
connect link Local; |
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:
1 2 |
select * from airlines limit 10; |
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:
1 |
gradle init --type java-application |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
plugins { id 'java' id 'application' } mainClassName = 'App' dependencies { compile 'com.google.guava:guava:23.0' compile group: 'com.couchbase.client', name: 'java-client', version:'2.5.6' testCompile 'junit:junit:4.12' } repositories { maven { url "http://files.couchbase.com/maven2" } mavenCentral() jcenter() } |
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:
1 |
gradle run |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.couchbase.client.java.*; import com.couchbase.client.java.analytics.AnalyticsQuery; import com.couchbase.client.java.analytics.AnalyticsQueryResult; import com.couchbase.client.java.analytics.AnalyticsQueryRow; public class App { public static void main(String[] args) { CouchbaseCluster cluster = CouchbaseCluster.create("couchbase://localhost"); cluster.authenticate("demo", "123456"); Bucket bucket = cluster.openBucket("travel-sample"); AnalyticsQueryResult result = bucket.query(AnalyticsQuery.simple("SELECT * FROM airports")); for(AnalyticsQueryRow row : result) { System.out.println(row.toString()); } } } |
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.