Apache CouchDB is one of a new breed of database management systems . CouchDB is often categorized as a “NoSQL” database different from traditional SQL-based databases.
CouchDB is built on the Erlang OTP platform, a functional, concurrent programming language and runtime environment. Erlang has built-in support for concurrency, distribution and fault tolerance.
CouchDB uses the Couch Replication Protocol that lets your data flow seamlessly between server clusters to applications, enabling a compelling, offline-first user-experience while maintaining high performance and strong reliability
Need for CouchDB
Unlike SQL databases, which are designed to store and report on highly structured, interrelated data. CouchDB is designed to store and report on large amounts of semi-structured, document oriented data dynamically to accommodate evolving needs
CouchDB makes use of the ubiquitous HTTP protocol and JSON data format and is compatible with any software that supports them
This greatly simplifies the development of document oriented applications, such as collaborative web applications
Apache CouchDB Features
- Schema-free . No pre-defined data structures
- Supports single node and clustered database model with higher capacity and high-availability
- Reliable synchronization of databases
- Supports redundant data storage
- Seamless distributed data handling
- Graceful error recovery
- The CouchDB document update model is lockless
CouchDB Data Handlinghttp:
A CouchDB database is a flat collection of documents. Unlike records in table , CouchDB deals with documents . Each document is identified by a unique ID
CouchDB provides a RESTFUL HTTP API for reading and updating (add, edit, delete) database documents.
The CouchDB document update model is lockless and optimistic which means multiple clients accessing the same document will incur update conflicts. To resolve the update conflict, the latest document version can be opened, the edits reapplied and the update tried again. The database never contains partially saved or edited documents.
CouchDB Installation
Installation is as simple as below
- Get the latest installable from the below URL
http://couchdb.apache.org/#download
- Run the installer and continue next to complete the installation
- Open up Fauxton
Apache CouchDB Fauxton Interface
Fauxton is a native web-based interface built into CouchDB. It provides a basic interface to the majority of the functionality, including the ability to create, update, delete and view documents and design documents. It provides access to the configuration parameters, and an interface for initiating replication
Clicking on Fauxton , opens up the below webpage URL
http://localhost:5984/_utils/index.html
Creating a database
- Select create database in the top left side of the Interface
- Enter the database name and click create
Creating document in a database
- Click on the New Doc option in the Design Documents drop down
- Create Document Page opens up with a unique id in JSON format . Add the data to the document in JSON format and click create
Document – ba1338eaf1d3deabed0c3b3b1c011883
{
“_id”: “ba1338eaf1d3deabed0c3b3b1c011883”,
“_rev”: “1-e17bf3108ea3b6234b30f26be287164d”,
“name”: “prabhu”
}
“_id” >> Defines the Auto generated Unique Identifier of the document . Can be edited with user-defined values
“_rev” >> Defines the Auto generated document revision which is auto incremented for each successful document change (insert or update or delete)
“name”: “prab” >> Defines the data (where name is similar to column and prab is value for the column in terms of traditional database)
Interacting with CouchDB from TIBCO BW
As CouchDB, provides a RESTFUL HTTP API for reading and updating (add, edit, delete,search) database documents.
From Tibco BW designer , HTTP requests can be initiated to communicate with Apache CouchDB on the below host and port
Data conversion from XML to JSON and JSON to XML can be achieved using TIBCO JSON Plugin or Java Code
HOST – 127.0.0.1
PORT – 5984
METHOD – GET | PUT | DELETE
BW Process Flows
1. GetCouchDBData.process
- GetDataActivity – Sends HTTP request to CouchDB with hostname, port, method as GET and request URI parameters
RequestURI value means as below
test – Database name
sampleTable – Document or record name
- Output
Status – 200
ASCIIContent – {“ok”:true,”id”:”sampleTable”,”rev”:”1-5ac88b59fcb1d2da4ab920009849d55b”}
2. DeleteCouchDBData.process
- GetDataActivity – Sends HTTP request to CouchDB with hostname, port, method as GET and request URI parameters
Output
ASCIIContent – {“ok”:true,”id”:”sampleTable”,”rev”:”1-5ac88b59fcb1d2da4ab920009849d55b”}
- FetchRevisionID Mapper Activity – As rev acts as unique identifier for each document . Always last used rev value should be passed for document update or delete. To do so , fetch the revisionID from the retrieved document based on below XPATH substring-before(substring-after($GetData/RequestActivityOutput/asciiContent,concat(“_rev”,'”‘,”:”,'”‘)),'”‘)
- DeleteDataActivity – Sends HTTP request to CouchDB with hostname, port, method as DELETE and requestURI parameters
RequestURI value means as below
test – Database name
sampleTable – Document or record name
?rev= – Query String
$FetchRevisionID/RevID – value passed to query string (output of FetchRevisionID mapper activity)
- Output
Status – 200
ASCIIContent – {“ok”:true,”id”:”sampleTable”,”rev”:”3-5ac89849fcb1d2da4ab92d55bfc0sd39″}
3. UpdateCouchDBData.process
- GetDataActivity – Sends HTTP request to CouchDB with hostname, port, method as GET and request URI parameters
Output
ASCIIContent – {“ok”:true,”id”:”sampleTable”,”rev”:”1-5ac88b59fcb1d2da4ab920009849d55b”}
- FetchRevisionID Mapper Activity – Fetches the revisionID from the retrieved document based on below XPATH
substring-before(substring-after($GetData/RequestActivityOutput/asciiContent,concat(“_rev”,'”‘,”:”,'”‘)),'”‘)
- EditData Mapper Activity – As CouchDB understands JSON format , update the retrieved document in JSON format with new data as below in XPATH
concat(‘{‘,'”‘,’_id’,'”‘,’:’,'”‘,’sampleTable’,'”‘,’,’,'”‘,’_rev’,'”‘,’:’,'”‘,$FetchRevisionID/RevID,'”‘,’,’,'”‘,’name’,'”‘,’:’,'”‘,’prabhu’,'”‘,’,’,'”‘,’age’,'”‘,’:’,'”‘,’26’,'”‘,’}’)
- UpdateDataActivity – Sends HTTP request to CouchDB with hostname, port, method as PUT, requestURI and PostData parameters
RequestURI value means as below
test – Database name
sampleTable – Document or record name
PostData – Actual data to be updated that is created in EditData mapper activity
- Output
Status – 200
ASCIIContent – {“ok”:true,”id”:”sampleTable”,”rev”:”3-5ac89849fcb1d2da4ab92d55bfc0sd3
References
Access below link for more details on Apache CouchDB