top of page

Getting started with CouchDB in your Spring Boot project

  • Writer: Chayanika Misra
    Chayanika Misra
  • Aug 3, 2024
  • 3 min read

What is Couch DB?

Couch DB is a Document DB. Documents DBs are a class of NoSQL databases built around JSON-like documents which are dynamic and self-describing, so you don’t need to pre-define the schema in the database. Because of the dynamic nature of the documents stored, they promise higher developer productivity, and faster evolution with application needs.

This DBs are very easy to scale horizontally because documents are independent units which can be easily distributed across multiple servers and unlike their relational db counter parts, don’t require joins and relations to be preserved.

The advantage of having a flexible schema and easy scalability, gives document DBs an edge over relational DBs. And that’s why you hear mongoDB everywhere.

While MongoDB is a widely used Document DB but CouchDB also provides the same functionalities, major difference being Mongo works with BSON and CouchDB works with JSON.

Typical Use Cases of Document DBs:

  • Storing product catalogue in a Retail store or in an e-commerce (which contains products supplier details, product category details etc) . Using a relational DB here, would need three separate tables (product, product_category, product_supplier). If we want to fetch the entire product info, it would need joins on the three tables. It can be easily avoided using a single document to aggregate all product related info.

  • Social Networking Applications use it to store each user’s pre-computed news feed and profile feed efficiently.

  • Used in content management as a de-normalization strategy to collect and store content from a variety of sources, and then deliver it to the customer.

Getting started with Couch DB

  1. Download CouchDB and can access the built-in web interface of CouchDB: http://127.0.0.1:5984/

  2. Add ektorp to your pom.xml (ektorp provides a set of APIs to connect to CouchDB from your Application)

<dependency>
    <groupId>org.ektorp</groupId>
    <artifactId>org.ektorp</artifactId>
    <version>1.4.1</version>
</dependency>

2. Create documents in the database by adding annotation JsonProperty to your model’s getter and setter methods to store its attributes as a document

package com.example.NewsFeedGenerator.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;

public class User {


    private String username;
    private String revision;
    private UserFeedAndProfile userFeedProfile;

    public User(){
        this.username = "";
        this.userFeedProfile = new UserFeedAndProfile(new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());

    }

    @JsonProperty("userProfile")
    public UserFeedAndProfile getUserFeedProfile() {
        return userFeedProfile;
    }

    @JsonProperty("userProfile")
    public void setUserFeedProfile(UserFeedAndProfile userFeedProfile) {
        this.userFeedProfile = userFeedProfile;
    }

    @JsonProperty("_id")
    public String getUsername() {
        return username;
    }

    @JsonProperty("_id")
    public void setUsername(String username) {
        this.username = username;
    }

    @JsonProperty("_rev")
    public String getRevision() {
        return revision;
    }

    @JsonProperty("_rev")
    public void setRevision(String revision) {
        this.revision = revision;
    }
}

The document created out of the User instance will look something like this:

{
 “_id”: “sunita”,
 “_rev”: “1–54b9e2aa175a5f3f9895baad2e982d79”,
 “userProfile”: {
 }
}

3. Create a database using ektorp StdHttpClient and StdCouchDbInstance

HttpClient httpClient = new StdHttpClient.Builder()
                                         .host("localhost")
                                         .port(5984)
                                         .username("admin")
                                         .password("adminroot")
                                         .build();
CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
// - - - - - - - - Creating database - - - - - - - - - - - - - - //
CouchDbConnector db = new StdCouchDbConnector("news-feed-service", dbInstance);

4. Use the above CouchDbConnector instance db to perform all CRUD operations

// - - - - - - - Creating document from User instance - - - - - - //
db.create(user);
// - - - - - - - Reading a user document using id- - - - - - - - //
db.get(User.class, id);
// - - - - - - - Updating a document user - - - - - - - - - - - - - //
db.update(user);
// - - - - - - - Deleting a document user - - - - - - - - - - - - //
db.delete(user);

5. And you are all set to use Couch DB with spring boot.


For reference, here’s an implementation of a news feed service using Spring boot and Couch DB to store the pre-computed feed for users:






Hope you had a good read. I am an amateur at writing tech blogs. Please drop in your feedback in the comments to help me improve the content of next blogs.

For any queries, reach me at chayanikamisra1997@gmail.com

Thank you!!


Recent Posts

See All

2 Comments


ui ni
ui ni
Jul 07

If you're looking for a password generator free of charge that can help you create highly secure passwords, online options are plentiful. These tools are essential for avoiding weak or easily guessable passwords, which are a common target for hackers. By generating truly random combinations of characters, they make your accounts much harder to compromise. It's a fundamental step in good digital hygiene, and using a generator ensures your passwords are up to the task of protecting your sensitive information.

Like

Alx Bob
Alx Bob
Jun 21

Want to Create strong, secure passwords effortlessly? Online generators are designed specifically for this purpose. You can usually customize the length of the password and specify whether to include different character types (like letters, numbers, and special symbols). This allows you to generate passwords that meet the specific requirements of various websites while ensuring they are robust enough to protect your accounts from unauthorized access. It’s a simple yet powerful tool for enhancing your personal online security with minimal effort.

Like
IMG_20191223_220908_294_edited_edited_edited_edited_edited_edited.png

Hi, thanks for stopping by!

I am a software developer who has contributed to the development of tech products across multiple e-commerce startups (Zepto, Myntra, Blinkit) over 5 years of work experience.

Let the posts
come to you.

  • Facebook
  • Instagram
  • Twitter
  • Pinterest
bottom of page