A passionate Java Web Developer and Blogger.Continuously testing various technologies and publishing it as post to my blog. Sandeep is a DZone MVB and is not an employee of DZone and has posted 15 posts at DZone. You can read more from them at their website. View Full User Profile

Couch DB Java Integration : Couchdb4j

  • submit to reddit

  •  COUCHDB4J API is library for accessing Documents from couch database.
  • This Library is downloaded from the link:-
  • In this Demo , "Using FUTON Web Interface a student database is created and 4 student documents are created.The java program is written for accessing these documents and displaying the details in console output".
  • The Student Database in Futon looks like,

  •  This library has some dependencies to these libraries,
  commons-httpclient-3.1.jar, commons-beanutils.jar, 
  commons-codec-1.3.jar, commons-collections.jar, 
  json-lib-2.0-jdk15.jar, ezmorph-1.0.3.jar 

  • The Project Structure,showing all the dependencies jar libraries in the CLASSPATH,
  •  The Java code is CouchDBTest.java is,
    package com.sandeep.couchdb.util;
    import java.util.List;
    import com.fourspaces.couchdb.Database;
    import com.fourspaces.couchdb.Document;
    import com.fourspaces.couchdb.Session;
    import com.fourspaces.couchdb.ViewResults;
    public class CouchDBTest {
     /*These are the keys of student document in couch db*/
     public static final String STUDENT_KEY_NAME ="name";
     public static final String STUDENT_KEY_MARKS ="marks";
     public static final String STUDENT_KEY_ROLL="roll";
     public static final String STUDENT_KEY_CONTACT ="contact";
     public static void main(String[] args){
      /*Creating a session with couch db running in 5984 port*/
      Session studentDbSession = new Session("localhost",5984);
      /*Selecting the student database from list of couch database*/
      Database studentCouchDb = studentDbSession.getDatabase("student");
      /*Fetching all Student Document to ViewResult object*/
      ViewResults couchViewResults = studentCouchDb.getAllDocuments();
      /*Retieving all document as result to a List*/
      List<Document> studentDocuments = couchViewResults.getResults();
      for(Document couchDocument: studentDocuments){
       String id = couchDocument.getJSONObject().getString("id");
       Document studentRow = studentCouchDb.getDocument(id);
       System.out.println("__________START OF DOCUMENT("+studentRow.get("_id")+")_________");
        System.out.println("NAME : "+studentRow.get(STUDENT_KEY_NAME));
        System.out.println("MARK : "+studentRow.get(STUDENT_KEY_MARKS));
        System.out.println("ROLL : "+studentRow.get(STUDENT_KEY_ROLL));
        System.out.println("CONTACT : "+studentRow.get(STUDENT_KEY_CONTACT));
  • ViewResults(com.fourspaces.couchdb.ViewResults) is the class which stores all the document.The debug point screenshot,  
    • Couch db stores the data in Document format (com.fourspaces.couchdb.Document) package.
  • The output:-
     __________START OF DOCUMENT(326e49164d9b414a6c1ce2c8a802d175)_________
    NAME : sandeep
    MARK : 75
    ROLL : 4
    CONTACT : +91-1234567890
    __________START OF DOCUMENT(326e49164d9b414a6c1ce2c8a802d28e)_________
    NAME : sumanta
    MARK : 80
    ROLL : 3
    __________START OF DOCUMENT(326e49164d9b414a6c1ce2c8a802dda1)_________
    NAME : sangeeta
    MARK : 95
    ROLL : 7
    __________START OF DOCUMENT(326e49164d9b414a6c1ce2c8a802f200)_________
    NAME : Surabhi
    MARK : 73
    ROLL : 7 
  • The output screenshot in java console,

Demo Code:-

Download link for demo code 


Published at DZone with permission of Sandeep Patel, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Sunil Gulabani replied on Tue, 2013/06/25 - 5:38am

 How to load result based on conditions. I tried using :

AdHocView adhocView = new AdHocView("function(doc) {  emit(null, doc);}");
ViewResults resultAdHoc = db.adhoc(adhocView);


ViewResults resultAdHoc = db.adhoc("function(doc) {  emit(null, doc);}");

But output is NULL only:

25 Jun, 2013 4:07:02 PM com.fourspaces.couchdb.Database adhoc
WARNING: Error executing view - null null
resultAdHoc: null

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.