JVM Languages

Apache CouchDB and Java

, February 22, 2010

Accessing the CouchDB database for data storage and retrieval using Java

Rajarshi Bhose is a Senior Research Associate and Manager at the Cloud Computing Center of Excellence at SETLabs, Infosys Technologies. Kashyap Santoki works in the Cloud Computing Center of Excellence at SETLabs where he specializes in .NET, Java, and performance engineering. They can be contacted at KashyapChimanlal_s@infosys.com and Rajarshi_Bhose@infosys.com.

Cloud databases are an emerging type of non-relational databases which do not follow relational algebra. Generally speaking, they are key-value oriented systems used for storing internet scale data and provide easy programmatic access. Databases suc as Amazon SimpleDB, Facebook's Cassandra, and Apache CouchDB among others fall in the category of non-relational databases that have been widely used in large consumer-based applications.

Apache CouchDB is a document-oriented, non-relational, schema-less database used for storing Internet-scale data. Apache CouchDB provides a rich set of REST-based JSON API's to access the database to perform administrative as well as CRUD operations. Apache CouchDB follows all ACID properties and also allows distributed updates and replications. Apache CouchDB is implemented on the Erlang OTP Platform.

In Apache CouchDB, the key is the "document ID" and the value is the associated document and where each document can have multiple fields. CouchDB also allows retrieving documents based on field values; this is done with the help of "views".

In this article, we demonstrate how to use CouchDB for CRUD and administrative operations using Java. We have used Java 1.6 version and CouchDB driver from Google code. The development environment is Eclipse 3.4.2 on Microsoft Windows XP-SP3 and Apache CouchDB on Fedora 9 System.

Before Your Start

This article provides a step-by-step guide for using Apache CouchDB using Java. Here we will use Java code to:

Create a database in CouchDB,

Store employee data (Employee number, Name, Designation, etc).

Perform some CRUD operations on the data that is stored.

Show the use of "Views" and how it can retrieve data based on field values.

Documents are the basic unit of data in CouchDB. They can contain any number of fields as well as attachments. Documents also contain metadata information which is maintained by the CouchDB. Fields in a CouchDB document are uniquely named and contain values of different types like text, number, boolean, lists, etc.

A CouchDB document is a simple JSON object. The following is an example of a CouchDB document:

Any fields with a name starting with a _ prefix are reserved for use by CouchDB itself. Common examples for such fields are _id (document id) and _rev (document revision), as shown above.

A CouchDB database is a flat collection of all these documents. Each document is identified by a unique ID (_id).

Creating New Projects and Establishing Sessions with CouchDB

To start, you need to create a new project in Eclipse, and a new class in that project. You can then add all the previously mentioned jar files to the project. At this point, you can start writing Java code in the class you have created.

The first step in using or accessing CouchDB is to create a Session. If you don't create a Session, you won't be able to perform any operations on the CouchDB. In short, Session is mandatory. You need to provide two input parameters to create a Session -- Host name and Port. The following code shows how to create session object or how to establish Session:

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!