CouchDB – Introduction ”; Previous Next Database management system provides mechanism for storage and retrieval of data. There are three main types of database management systems namely RDBMS (Relational Database management Systems), OLAP (Online Analytical Processing Systems) and NoSQL. RDBMS RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. A Relational database management system (RDBMS) is a database management system (DBMS) that is based on the relational model as introduced by E. F. Codd. The data in RDBMS is stored in database objects called tables. The table is a collection of related data entries and it consists of columns and rows. It stores only structured data. OLAP Online Analytical Processing Server (OLAP) is based on the multidimensional data model. It allows managers and analysts to get an insight of the information through fast, consistent, and interactive access to information. NoSQL Databases A NoSQL database (sometimes called as Not Only SQL) is a database that provides a mechanism to store and retrieve data other than the tabular relations used in relational databases. These databases are schema-free, support easy replication, have simple API, eventually consistent, and can handle huge amounts of data (big data). The primary objective of a NoSQL database is to have the following − Simplicity of design, Horizontal scaling, and Finer control over availability. NoSQL databases use different data structures compared to relational databases. It makes some operations faster in NoSQL. The suitability of a given NoSQL database depends on the problem it must solve. These databases store both structured data and unstructured data like audio files, video files, documents, etc. These NoSQL databases are classified into three types and they are explained below. Key-value Store − These databases are designed for storing data in key-value pairs and these databases will not have any schema. In these databases, each data value consists of an indexed key and a value for that key. Examples − BerkeleyDB, Cassandra, DynamoDB, Riak. Column Store − In these databases, data is stored in cells grouped in columns of data, and these columns are further grouped into Column families. These column families can contain any number of columns. Examples − BigTable, HBase, and HyperTable. Document Store − These are the databases developed on the basic idea of key-value stores where “documents” contain more complex data. Here, each document is assigned a unique key, which is used to retrieve the document. These are designed for storing, retrieving, and managing document-oriented information, also known as semi-structured data. Examples − CouchDB and MongoDB. What is CouchDB? CouchDB is an open source database developed by Apache software foundation. The focus is on the ease of use, embracing the web. It is a NoSQL document store database. It uses JSON, to store data (documents), java script as its query language to transform the documents, http protocol for api to access the documents, query the indices with the web browser. It is a multi master application released in 2005 and it became an apache project in 2008. Why CouchDB? CouchDB have an HTTP-based REST API, which helps to communicate with the database easily. And the simple structure of HTTP resources and methods (GET, PUT, DELETE) are easy to understand and use. As we store data in the flexible document-based structure, there is no need to worry about the structure of the data. Users are provided with powerful data mapping, which allows querying, combining, and filtering the information. CouchDB provides easy-to-use replication, using which you can copy, share, and synchronize the data between databases and machines. Data Model Database is the outermost data structure/container in CouchDB. Each database is a collection of independent documents. Each document maintains its own data and self-contained schema. Document metadata contains revision information, which makes it possible to merge the differences occurred while the databases were disconnected. CouchDB implements multi version concurrency control, to avoid the need to lock the database field during writes. Features of CouchDB:Reduce the Content Document Storage CouchDB is a document storage NoSQL database. It provides the facility of storing documents with unique names, and it also provides an API called RESTful HTTP API for reading and updating (add, edit, delete) database documents. In CouchDB, documents are the primary unit of data and they also include metadata. Document fields are uniquely named and contain values of varying types (text, number, Boolean, lists, etc.), and there is no set limit to text size or element count. Document updates (add, edit, delete) follow Atomicity, i.e., they will be saved completely or not saved at all. The database will not have any partially saved or edited documents. Json Document Structure { “field” : “value”, “field” : “value”, “field” : “value”, } ACID Properties CouchDB contains ACID properties as one of its features. Consistency − When the data in CouchDB was once committed, then this data will not be modified or overwritten. Thus, CouchDB ensures that the database file will always be in a consistent state. A multi-Version Concurrency Control (MVCC) model is used by CouchDB reads, because of which the client will see a consistent snapshot of the database from the beginning to the end of the read operation. Whenever a documents is updated, CouchDB flushes the data into the disk, and the updated database header is written in two consecutive and identical chunks to make up the first 4k of the file, and then synchronously flushed to disk. Partial updates during the flush will be discarded. If the failure occurred while committing the header, a surviving copy of the previous identical headers will remain, ensuring coherency of all previously committed data. Except the header area, consistency checks or fix-ups after a crash or a power failure are never necessary. Compaction Whenever the space in the database file got wasted above certain extent, all the active data will be copied (cloned) to a new file. When the copying process is entirely
Category: couchdb
CouchDB – HTTP API
CouchDB – HTTP API ”; Previous Next Using HTTP request headers, you can communicate with CouchDB. Through these requests we can retrieve data from the database, store data in to the database in the form of documents, and we can view as well as format the documents stored in a database. HTTP Request Formats While communicating with the database we will use different request formats like get, head, post, put, delete, and copy. For all operations in CouchDB, the input data and the output data structures will be in the form of JavaScript Object Notation (JSON) object. Following are the different request formats of HTTP Protocol used to communicate with CouchDB. GET − This format is used to get a specific item. To get different items, you have to send specific url patterns. In CouchDB using this GET request, we can get static items, database documents and configuration, and statistical information in the form of JSON documents (in most cases). HEAD − The HEAD method is used to get the HTTP header of a GET request without the body of the response. POST − Post request is used to upload data. In CouchDB using POST request, you can set values, upload documents, set document values, and can also start certain administration commands. PUT − Using PUT request, you can create new objects, databases, documents, views and design documents. DELETE − Using DELETE request, you can delete documents, views, and design documents. COPY − Using COPY method, you can copy documents and objects. HTTP Request Headers HTTP headers should be supplied to get the right format and encoding. While sending the request to the CouchDB server, you can send Http request headers along with the request. Following are the different Http request headers. Content-type − This Header is used to specify the content type of the data that we supply to the server along with the request. Mostly the type of the content we send along with the request will be MIME type or JSON (application/json). Using Content-type on a request is highly recommended. Accept − This header is used to specify the server, the list of data types that client can understand, so that the server will send its response using those data types. Generally here, you can send the list of MIME data types the client accepts, separated by colons. Though, using Accept in queries of CouchDB is not required, it is highly recommended to ensure that the data returned can be processed by the client. Response Headers These are the headers of the response sent by the server. These headers give information about the content send by the server as response. Content-type − This header specifies the MIME type of the data returned by the server. For most request, the returned MIME type is text/plain. Cache-control − This header suggests the client about treating the information sent by the server. CouchDB mostly returns the must-revalidate, which indicates that the information should be revalidated if possible. Content-length − This header returns the length of the content sent by the server, in bytes. Etag − This header is used to show the revision for a document, or a view. Status Codes Following is the tabular form of the status code sent by the http header and the description of it. Sr.No. Status Code & Description 1 200 − OK This status will be issued when a request completed successfully. 2 201 − Created This status will be issued when a document is created. 3 202 − Accepted This status will be issued when a request is accepted. 4 404 − Not Found This status will be issued when the server is unable to find the requested content. 5 405 − Resource Not Allowed This status is issued when the HTTP request type used is invalid. 6 409 − Conflict This status is issued whenever there is any update conflict. 7 415 − Bad Content Type This status indicated that the requested content type is not supported by the server. 8 500 − Internal Server Error This status is issued whenever the data sent in the request is invalid. HTTP URL Paths There are certain url paths using which, you can interact with the database directly. Following is the tabular format of such url paths. Sr.No. URL & Operation 1 PUT /db This url is used to create a new database. 2 GET /db This url is used to get the information about the existing database. 3 PUT /db/document This url is used to create a document/update an existing document. 4 GET /db/document This url is used to get the document. 5 DELETE /db/document This url is used to delete the specified document from the specified database. 6 GET /db/_design/design-doc This url is used to get the definition of a design document. 7 GET /db/_design/designdoc/_view/view-name This url is used to access the view, view-name from the design document from the specified database. Print Page Previous Next Advertisements ”;
CouchDB – Deleting a Database ”; Previous Next Deleting a Database using cURL Utility You can delete a database in CouchDB by sending a request to the server using DELETE method through cURL utility. Following is the syntax to create a database − $ curl -X DELETE http://127.0.0.1:5984/database name Using −X we can specify a custom request method of HTTP we are using, while communicating with the HTTP server. In this case, we are using the DELETE method. Send the url to the server by specifying the database to be deleted in it. Example Assume there is a database named my_database2 in CouchDB. Using the above given syntax if you want to delete it, you can do it as follows − $ curl -X DELETE http://127.0.0.1:5984/my_database2 { “ok” : true } As a response, the server will return you a JSON document with content “ok” − true indicating the operation was successful. Verification Verify whether the database is deleted by listing out all the databases as shown below. Here you can observe the name of the deleted database, “my_database” is not there in the list. $ curl -X GET http://127.0.0.1:5984/_all_dbs [ “_replicator ” , ” _users ” ] Deleting a Database using Futon To delete a database, open the http://127.0.0.1:5984/_utils/ url where you will get an Overview/index page of CouchDB as shown below. Here you can see three user created databases. Let us delete the database named tutorials_point2. To delete a database, select one from the list of databases, and click on it, which will lead to the overview page of the selected database where you can see the various operations on databases. The following screenshot shows the same − Among them you can find Delete Database option. By clicking on it you will get a popup window, asking whether you are sure! Click on delete, to delete the selected database. Print Page Previous Next Advertisements ”;
CouchDB – Discussion
Discuss CouchDB ”; Previous Next This tutorial provides a brief knowledge about CouchDB, the procedures to set it up, and the ways to interact with CouchDB server using cURL and Futon. It also tells how to create, update and delete databases and documents. Please enable JavaScript to view the comments powered by Disqus. Print Page Previous Next Advertisements ”;