DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Couch DB Java Integration : Couchdb4j

Couch DB Java Integration : Couchdb4j

Sandeep Patel user avatar by
Sandeep Patel
·
Mar. 01, 13 · Interview
Like (0)
Save
Tweet
Share
17.63K Views

Join the DZone community and get the full member experience.

Join For Free

 
  •  COUCHDB4J API is library for accessing Documents from couch database.
  • This Library is downloaded from the link:-
         http://code.google.com/p/couchdb4j/downloads/list
  • 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, 
  commons-lang.jar,commons-logging-1.1.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")+")_________");
        
       if(studentRow.containsKey(STUDENT_KEY_NAME)){
         
        System.out.println("NAME : "+studentRow.get(STUDENT_KEY_NAME));
         
       }
       if(studentRow.containsKey(STUDENT_KEY_MARKS)){
         
        System.out.println("MARK : "+studentRow.get(STUDENT_KEY_MARKS));
         
       }
        
       if(studentRow.containsKey(STUDENT_KEY_ROLL)){
         
        System.out.println("ROLL : "+studentRow.get(STUDENT_KEY_ROLL));
         
       }
       if(studentRow.containsKey(STUDENT_KEY_CONTACT)){
         
        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 

 

Java (programming language) Integration

Published at DZone with permission of Sandeep Patel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Secrets Management
  • Using AI and Machine Learning To Create Software
  • Top Authentication Trends to Watch Out for in 2023
  • Deploying Java Serverless Functions as AWS Lambda

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: