Over a million developers have joined DZone.

Couch DB Java Integration : Couchdb4j

DZone's Guide to

Couch DB Java Integration : Couchdb4j

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

  •  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,


Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}