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 ”;

CouchDB – Home

CouchDB Tutorial PDF Version Quick Guide Resources Job Search Discussion 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. Audience This tutorial helps the professionals aspiring to make a career in Big Data and NoSQL databases, especially the documents store. Prerequisites Before you start proceeding with this tutorial, we are assuming that you have a brief knowledge on Big Data, Hadoop, and also have the basic understanding of databases. Print Page Previous Next Advertisements ”;

CouchDB – Updating a Document

CouchDB – Updating a Document ”; Previous Next Updating Documents using cURL You can update a document in CouchDB by sending an HTTP request to the server using PUT method through cURL utility. Following is the syntax to update a document. curl -X PUT http://127.0.0.1:5984/database_name/document_id/ -d ”{ “field” : “value”, “_rev” : “revision id” }” Example Suppose there is a document with id 001 in the database named my_database. You can delete this as shown below. First of all, get the revision id of the document that is to be updated. You can find the _rev of the document in the document itself, therefore get the document as shown below. $ curl -X GET http://127.0.0.1:5984/my_database/001 { “_id” : “001”, “_rev” : “2-04d8eac1680d237ca25b68b36b8899d3 ” , “age” : “23” } Use revision id _rev from the document to update the document. Here we are updating the age from 23 to 24. $ curl -X PUT http://127.0.0.1:5984/my_database/001/ -d ” { ” age ” : ” 24 ” , ” _rev ” : ” 1-1c2fae390fa5475d9b809301bbf3f25e ” } ” { ” ok ” : true , ” id ” : ” 001 ” , ” rev ” : ” 2-04d8eac1680d237ca25b68b36b8899d3 ” } Verification To verify the document, get the document again using GET request as shown below. $ curl -X GET http://127.0.0.1:5984/my_database/001 { ” _id ” : ” 001 “, ” _rev ” : ” 2-04d8eac1680d237ca25b68b36b8899d3 ” , ” age ” : ” 23 ” } Note Following are some important points to be noted while updating a document. The URL we send in the request containing the database name and the document id. Updating an existing document is same as updating the entire document. You cannot add a field to an existing document. You can only write an entirely new version of the document into the database with the same document ID. We have to supply the revision number as a part of the JSON request. In return JSON contains the success message, the ID of the document being updated, and the new revision information. If you want to update the new version of the document, you have to quote this latest revision number. Updating Documents using Futon To delete a document open the http://127.0.0.1:5984/_utils/ url to get an Overview/index page of CouchDB as shown below. Select the database in which the document to be updated exists and click it. Here we are updating a document in the database named tutorials_point. You will get the list of documents in the database as shown below. Select a document that you want to update and click on it. You will get the contents of the documents as shown below. Here, to update the location from Delhi to Hyderabad, click on the text box, edit the field, and click the green button to save the changes as shown below. Print Page Previous Next Advertisements ”;

CouchDB – Curl & Futon

CouchDB – Curl & Futon ”; Previous Next cURL Utility cURL utility is a way to communicate with CouchDB. It is a tool to transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, TFTP, DICT, TELNET, LDAP or FILE). The command is designed to work without user interaction. cURL offers a busload of useful tricks like proxy support, user authentication, ftp upload, HTTP post, SSL (https:) connections, cookies, file transfer resume and more. The cURL utility is available in operating systems such as UNIX, Linux, Mac OS X and Windows. It is a command line utility using which user can access HTTP protocol straight away from the command line. This chapter teaches you how to use cURL utility. Using cURL Utility You can access any website using cURL utility by simply typing cURL followed by the website address as shown below − curl www.tutorialspoint.com/ By default, the cURL utility returns the source code of the requested page. It displays this code on the terminal window. cURL Utility Options cURL utility provides various options to work with, and you can see them in cURL utility help. The following code shows some portion of cURL help. $ curl –help Usage: curl [options…] <url> Options: (H) means HTTP/HTTPS only, (F) means FTP only –anyauth Pick “any” authentication method (H) -a/–append Append to target file when uploading (F/SFTP) –basic Use HTTP Basic Authentication (H) –cacert <file> CA certificate to verify peer against (SSL) -d/–data <data> HTTP POST data (H) –data-ascii <data> HTTP POST ASCII data (H) –data-binary <data> HTTP POST binary data (H) –data-urlencode <name=data/name@filename> HTTP POST data urlencoded (H) –delegation STRING GSS-API delegation permission –digest Use HTTP Digest Authentication (H) –disable-eprt Inhibit using EPRT or LPRT (F) –disable-epsv Inhibit using EPSV (F) -F/–form <name=content> Specify HTTP multipart POST data (H) –form-string <name=string> Specify HTTP multipart POST data (H) –ftp-account <data> Account data to send when requested by server (F) –ftp-alternative-to-user <cmd> String to replace “USER [name]” (F) –ftp-create-dirs Create the remote dirs if not present (F) –ftp-method [multi cwd/no cwd/single cwd] Control CWD usage (F) –ftp-pasv Use PASV/EPSV instead of PORT (F) -G/–get Send the -d data with a HTTP GET (H) -H/–header <line> Custom header to pass to server (H) -I/–head Show document info only -h/–help This help text –hostpubmd5 <md5> Hex encoded MD5 string of the host public key. (SSH) -0/–http1.0 Use HTTP 1.0 (H) –ignore-content-length Ignore the HTTP Content-Length header -i/–include Include protocol headers in the output (H/F) -M/–manual Display the full manual -o/–output <file> Write output to <file> instead of stdout –pass <pass> Pass phrase for the private key (SSL/SSH) –post301 Do not switch to GET after following a 301 redirect (H) –post302 Do not switch to GET after following a 302 redirect (H) -O/–remote-name Write output to a file named as the remote file –remote-name-all Use the remote file name for all URLs -R/–remote-time Set the remote file”s time on the local output -X/–request <command> Specify request command to use –retry <num> Retry request <num> times if transient problems occur –retry-delay <seconds> When retrying, wait this many seconds between each –retry-max-time <seconds> Retry only within this period -T/–upload-file <file> Transfer <file> to remote site –url <URL> Set URL to work with -B/–use-ascii Use ASCII/text transfer While communicating with CouchDB, certain options of cURL utility were extensively used. Following are the brief descriptions of some important options of cURL utility including those used by CouchDB. -X flag (HTTP) Specifies a custom request method used when communicating with the HTTP server. The specified request is used instead of the method otherwise used (which defaults to GET). Read the HTTP 1.1 specification for details and explanations. (FTP) Specifies a custom FTP command to use instead of LIST when doing file lists with ftp. -H (HTTP) Extra header is used when getting a web page. Note that if you add a custom header that has the same name as one of the internal ones cURL would use, your externally set header will be used instead of the internal one. This allows you to make even trickier work than cURL would normally do. You should not replace internally set headers without perfectly knowing what you’re doing. Replacing an internal header with the one without content on the right side of the colon, will prevent that header from appearing. cURL assures that each header you add/replace get sent with the proper end of line marker. Neither you should add that as a part of the header content nor add newlines or carriage returns to disorder things. See also the -A/–user-agent and -e/–referer options. This option can be used multiple times to add/replace/remove multiple headers. -d flag Using this flag of cURL, you can send data along with the HTTP POST request to the server, as if it was filled by the user in the form and submitted. Example Suppose there is a website and you want to login into it or send some data to the website using –d flag of cURL utility as shown below. curl -X PUT http://mywebsite.com/login.html -d userid=001 -d password=tutorialspoint It sends a post chunk that looks like “userid=001&password=tutorialspoint”. Likewise you can also send documents (JSON ) using -d flag. -o flag Using this flag, cURL writes the output of the request to a file. Example The following example shows the use of -o flag of cURL utility. $ curl -o example.html www.tutorialspoint.com/index.htm % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 81193 0 81193 0 0 48168 0 –:–:– 0:00:01 –:–:– 58077 This gets the source code of the homepage of tutorialspoint.com, creates a file named example.com and saves the output in the file named example.html. Following is the snapshot of the example.html. -O This flag is similar to –o, the only difference is with this flag, a new file with the same name as the requested url was created, and the source code of the requested