Getting started with CouchDB in your Spring Boot project

Chayanika Misra
3 min readJun 13, 2021

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:

News Feed Generator service using Spring boot and couch DB

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!!

--

--

Chayanika Misra

Software Developer | Love for code & travel | Writing my experiences about learning new things | connect with me on https://www.linkedin.com/in/chayanika-misra/