CouchDB Tutorial

Apache CouchDB

Apache CouchDB Tutorial

What is CouchDB?

Apache CouchDB is an open-source database software which is developed by an
Apache Software Foundation and first released in 2005 under the Apache license of version 2.0.
CouchDB was initially written in C++, but in 2008 this project moves to the Erlang, which is a functional programming language.

CouchDB is a NoSQL database which focuses on ease of use. It uses a JSON (JavaScript-based object Notation) Document for storing the data and uses a JavaScript as its Query Language using MapReduce.

CouchDB uses the HTTP protocol API, which helps to communicate with the database easily, such as reading and updating database documents.

Difference between SQL and CouchDB:

The following are some important differences between SQL and CouchDB:

SQL(RDBMS)CouchDB
It is a Relational Database model.It is a Non-Relational Database model.
It stores data in the form of tables.It stores data in the form of JSON Document files, i.e., key-value pair.
SQL Query Engine.Map/Reduce Function.
It support joins as there is a foreign and primary key.CouchDB not support joins. It consists of a concept 'view' to join the document.

Features of Apache CouchDB

  • All the data in CouchDB database stored in documents and each document is uniquely named in a database. Each Document consists of any number of ‘keys’ and their corresponding ‘values’ known as fields and also includes the metadata. In the document,  each field must be given a unique name, i.e., no two fields have the same name. Document fields contain different types of data, such as a text string, Number, Boolean values (true or false). There is no size limit to text or a number in documents.
  • ACID Properties: CouchDB file follows the ACID (i.e., Atomicity, Consistency, Isolation, Durability) properties. The Updated documents will follow the atomicity that is either the updates saved completely or not saved at all. The data that enters in CouchDB is consistent, i.e., Once the data in CouchDB saved in the database, then that document will not be overwritten and modified.
  • Apache CouchDB replicates to the devices (like android devices) that can go offline and backup your data for you when the device is back online.
  • Document format of CouchDB based on a JSON Structure.

Installing CouchDB in Windows:

Apache-CouchDB-on-windows
  1. For downloading the setup file of CouchDB, go to the official website: http://couchdb.apache.org/.
CouchDB Installation
  1. Now, click on the Download option and then it leads to the page where various download links are provided.
CouchDB Installation 1
  1. Now, Click on the windows option. After 2-3 minutes, CouchDB will be download in your system in the form of Setup file.
CouchDB Installation 2

Now, run that setup file apache-Couchdb-2.3.1.

  1. After clicking on the run button, proceed with the installation. Default Settings are recommended, click on next button.
CouchDB Installation 3
  1. Click On next button.
CouchDB Installation 4
  1. In the below figure choose the directory of the CouchDB folder. And then click on Next Button.
CouchDB Installation 5
  1. At last click on install button. And after that, a confirmation dialog box will appear and in that click on ‘yes’ button. Your installation will complete in some seconds.
CouchDB Installation 6
  1. After the complete installation opens the browser and type the following URL: http://127.0.0.1:5984/ and open the link. If everything goes fine, the resulting output will appear:
{"couchdb":"Welcome","version":"2.3.1","git_sha":"c298091a4","uuid":"2c468501cc403507843c0e8e1bf68f21",
"features":["pluggable-storage-engines","scheduler"],
"vendor":{"name":"The Apache Software Foundation"}}
  1. Now, by typing this URL: http://127.0.0.1:5984/_utils/, you can interact with the CouchDB web interface, which shows the index page of Futon as shown in the figure:
CouchDB Installation 7
  1. Verify the CouchDB installation by going to the verify tab that shown in below figure. Now Click on the Verify installation.
CouchDB Installation 8
  1. If your CouchDB is installed Successfully, a window will appear as shown below:
CouchDB Installation 9

There are two ways to Communicate with the CouchDB:

  1. CouchDB Curl
  2. CouchDB Futon

1.CouchDB Curl:

CouchDB Curl utility is a way to communicate or to interact with the CouchDB and its database.

It is a Command line tool available on operating systems such as mac os x, windows, and Linux. Curl provides easy access to the HTTP protocol directly from the Command-line.

  • While interacting with the CouchDB by using the curl, following options of curl utility are used:
  1. -X
  2. - H
  3. -d
  4. -O

Installing Curl:

  1. If Curl is not available in your system, so first Download the Zip file by this site: https://curl.haxx.se/download.html.
  2. Open the given link then go to the end of the web page and download the file by clicking on the marked blue file.
Installing Curl 2
  1. Then open the folder where the Zip file downloaded. Then Extract the files by right-clicking on it.
  2. Verify Installation: After that open the Command Prompt and type the following Commands:
C:\Users\user>cd Downloads
C:\Users\user\Downloads>cd curl-7.64.1                      
C:\Users\user\Downloads\curl-7.64.1>cd src
C:\Users\user\Downloads\curl-7.64.1\src>curl --version
  1. If you see the Curl version same as shown in below figure, then Curl is Successfully installed in your system.
Installing Curl 5

By using Curl, get the Database information:

C:\Users\user\Downloads\curl-7.64.1\src>curl http://127.0.0.1:5984/

This Command will show the following output :

Installing Curl 6

List Of all the Database :

            C:\Users\user\Downloads\curl-7.64.1\src>curl -X GET    http://127.0.0.1:5984/_all_dbs

Above Command shows the list of all the databases.

Installing Curl 7
  1. CouchDB Futon:

CouchDB Futon is a second way to interact with a CouchDB database. It provides a graphical interface for CouchDB and offers full access to all CouchDB features and make it easy to work.

By using Futon, we can do the following features:

  • Create Database
  • Destroy Database
  • Create Document
  • Update Documents
  • Edit Documents
  • Delete Documents

Starting Futon:

Make Sure that CouchDB is running in your System and then open the browser and type the following URL: http://127.0.0.1:5984/_utils/.

If you open the URL then it displays the Futon home page which is as shown below:

Starting Futon CouchDB

Creating the database:

You can create the database using the Curl Utility by sending the HTTP request to the server by using the PUT method :

curl -X PUT http://127.0.0.1:5984/school
create the database using the Curl Utility

Now, to check that 'school' Database is successfully Created in CouchDB by using the below command:

curl -X GET http://127.0.0.1:5984/_all_dbs
Database is successfully Created in CouchDB