Using Couchbase with Codenvy

Use Codenvy Factory to instantly start working with a sample Couchbase project:

This tutorial will explain the details of integrating Couchbase with Codenvy. Couchbase is a document model oriented, NoSQL database and KuroBase provides Couchbase hosting on a cloud platform. It allows your application to connect to your Couchbase data source. The explanation has been done with a very basic Java project that makes use of the Couchbase Java drivers to connect to your Couchbase database available within KuroBase. Couchbase is hosted on RightScale as can be seen at but we have used KuroBase which can simplify the setup.

Register With KuroBase

Before proceeding to the creation of the Java project, please register at with your email id:


On registering, you will be able to login to your KuroBase account that allows you to create Couchbase buckets that you can then connect to from within your application:


On creation of the Couchbase bucket, you can then view the connection details within your account:


You can get more information about the Couchbase usage at The documentation has different sections that will describe the usage of Couchbase server and the process of establishing connection to Couchbase from your application using different client drivers or REST API.
You can also get more information about the Couchbase Java Driver at

Create a Project

Now, login to your Codenvy workspace and create a WAR project named as ‘CouchBaseSample’. This will have the following structure:

Create pom.xml File

To connect to your Couchbase database using the Couchbase Java driver from your application, please update the pom.xml to include the Couchbase Java driver into your project as Maven dependencies:

      <name>Maven Repository Switchboard</name>

This will include the driver jar in your project as follows:

Create Java Class

Now create a Java class named that will establish a Couchbase database connection using the Kurobase connection details, create a Couchbase document with a sample data and then retrieve the data to display the data on your console logs and then close the connection. All this is done using the Couchbase Java driver API methods that make use of the Couchbase queries. All this is done inside the method onInitialize() in the sample source Java class.

   public void onInitialize() throws Exception{

        ArrayList<URI> nodes = new ArrayList<URI>();

        // Add one or more nodes of your cluster (exchange the IP with yours)
        nodes.add(URI.create("<bucket-url>/pools")); //Please use the URL and port number available under the bucket in your KuroBase account
        // Try to connect to the client
        CouchbaseClient client = null;
        try {
          client = new CouchbaseClient(nodes, "<bucket-name>", "<bucket-password>"); //Please use the bucket name and bucket password available under the bucket in your KuroBase account
        } catch (Exception e) {
          System.err.println("Error connecting to Couchbase: " + e.getMessage());
        // Set your first document with a key of "hello" and a value of "couchbase!"
        client.set("hello", "couchbase!").get();
        // Return the result and cast it to string
        String result = (String) client.get("hello");
        // Shutdown the client

Create index.jsp File

Your Codenvy project will have an index.jsp which will invoke the Java class -

    com.codenvy.Couchbase couchbase = new com.codenvy.Couchbase();

Now you build and run your Codenvy project and the response will be displayed in the console logs:


This way, you can use the Couchbase database within your application for various purposes.