When you are developing a new applications with Couchbase 2.0, you sometimes need to create view dynamically from your code. For example you may need this when you are installing your application, writing some test, or you can also use that when you are building frameworks, and wants to dynamically create views to query data. This post shows how to do it.
If you are using Maven you can use the following information in your
pom.xml to add the Java Client library:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
See online at https://gist.github.com/4337172
Create and Manage Views From Java
The full Maven project is available on Github.
Connect to Couchbase Cluster
The first thing to do when you want to create a view from Java is obviously to connect to the cluster.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
- Create a list of URIs to different nodes of the cluster - lines 5-6. (In this example I am working on a single node)
- Connect to the bucket, in our case
beer-sample-line 9. You can include the password if the bucket is protected ( this is not the case here so I am sending an empty string)
If you are looking for more information about Couchbase and Java, you can read this article from DZone : Hello World with Couchbase and Java.
Let’s now talk about Couchbase views. You use views/map-reduce functions to index and query data from Couchbase Server based on the content of the JSON document you store inside Couchbase. For more information about views you can look at the “view basics” chapter of the Couchbase Server Manual.
Create Views from Java
Creating a view from Java is really easy : the Java Client Library contains all the classes and methods to do it. As a concrete use case we will use the Application that is described in the Couchbase Java Tutorial.
When you follow this tutorial, you need to manually create some views, as you can see here. In this example, we will create our map function and directly in our Java code and then store it to Couchbase Server. The tutorial asks you to create the following artifacts:
- a view named “by_name”
- in the design document named
- and the map function which looks like the following :
1 2 3 4 5
The following code allows you to do it from Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
- Create a design document using the
com.couchbase.client.protocol.views.DesignDocumentclass - line 4.
- Create a view using
com.couchbase.client.protocol.views.ViewDesignclass with a name and the map function - line 14.
- You can add this view to a design document - line 15
- Finally save the document into the cluster using the
If you need to use a reduce function (built-in or custom) you just need to pass to the ViewDesign constructor as 3rd parameter.
When developing view, from Java or from any other tool/language be sure you understand what are the best practices, and the life cycle of the index. This is why I am inviting you to take a look to the following chapters in the Couchbase documentation:
- View Writing Best Practice : for example in the map function, I do not emit any value. I only emit a key (the beer name).
- Views and Stored Data
- Development and Production Views : in the view above, I have created the view in the development environment (dev_ prefix) allowing me to test and use it on a subset of the data (cluster/index)
Using the view
First of all, the view that you just created is in “development mode”, and by default the Java client SDK will only access the view when it is in “production mode”. This means that when you are calling a view from your application it will search it into the production environment. So before connecting to Couchbase cluster you need to setup the
viewmode to development.
This is done using the
viewmode environment variable from the Java SDK, that could be set using the following methods:
- In your code, add this line before the client connects to the cluster :
- At the command line
- In a properties file
Once it is done you can call the view using the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
This code queries the view you just created. This means Couchbase Server will generate an index based on your map function, will query the server for results. In this case, we specifically want to set a limit of 20 results and also get the most current results by setting Stale.FALSE.
- Set the
development- line 4
- Get the view using the
CouchbaseClient.getView()method -line 6-. As you can see I just use the name beer for the design document (and not dev_beer, Couchbase will know where to search since I am in development mode)
- Create a query and set a limit (20) and ask the SDK to return the document itself
setIncludeDocs(true)-line 8- The document will be returned from Couchbase server in the most efficient way
- Ask the system to update the index before returning the result using query.setStale( Stale.FALSE
);-line 9-. Once again be careful when you use setStale method. Just to be sure here is the documentation about it : Index Updates and the stale Parameter
- Execute the query - line 10
- And use the result - lines 11-13
In this article, you have learned:
- How to create Couchbase views from Java
- Call this view from Java
- Configure development/production mode views from Couchbase Java Client Library
This example is limited to the creation of a view, you can take a look to the other methods related to design documents and views if you want to manage your design documents :