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 http://www.couchbase.com/company/partners/rightscale 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 http://console.kurobase.com/users/new with your email id:

couchbase-register

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:

couchbase-bucket

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

couchbase-account

You can get more information about the Couchbase usage at http://www.couchbase.com/documentation-archives. 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 http://docs.couchbase.com/couchbase-sdk-java-1.2/.

Create a Project

Now, login to your Codenvy workspace and create a WAR project named as ‘CouchBaseSample’. This will have the following structure:
couchbase-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:

  <dependencies>
    <dependency>
      <groupId>com.couchbase.client</groupId>
      <artifactId>couchbase-client</artifactId>
      <version>1.3.2</version>
    </dependency>
  </dependencies>
  <repositories>
     <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <url>http://repo1.maven.org/maven2</url>
     </repository>
  </repositories>

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

Create Java Class

Now create a Java class named Couchbase.java 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());
          System.exit(1);
        }
    
        // 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");
        System.out.println(result);
    
        // Shutdown the client
        client.shutdown();
  
   }

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();
    couchbase.onInitialize();
%>

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

couchbase-output

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