Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
1/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CS-695 NoSQL DatabaseCouchDB (part 1 of 2)
Dr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck Cartledge
22 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 201522 Oct. 2015
2/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
Table of contents I
1 Miscellanea
2 Mid-term
3 DB comparisons
4 Assgn. #5
5 Origins and history
6 Data model
7 CRUDy stuff
8 Conclusion
9 References
3/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
Corrections and additions since last lecture.
Assignment #05 is available
Mid-term
4/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
How different DBs compare to a RDBMS
We have some terms to compare now[4]
RDBMS K/V Columnar Doc.
DB. instance cluster cluster instancedatabase — namespace —table bucket table collectionrow key-value row documentrowid key — idcol. — col. fam. —schema — — databasejoin — — DBRef
5/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
Words of explanation.
The full text is available at:http://www.cs.odu.edu/~ccartled/Teaching/2015-Fall/NoSQL/Assignments/05/
In general terms:
1 Parse data
2 Create document database(possibly two, or three)
3 Update documents based onnumber of movies filmed per year,and number of trivia facts permovie
4 Query database
5 Create list of movies per year(two different types of lists)
6 Plot graphic
7 Create a list of number of triviafacts by movie
8 Plot graphic
6/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
Where it came from and why.
Cluster of unreliable commodity hardware DataBase(CouchDB)1
Started in April 2005
Became an ApacheIncubator project in 2008
Overview
Document StorageACID Semantics (at thedocument level)Map/Reduce Views andIndexesDistributed Architecturewith ReplicationREST APIEventual ConsistencyBuilt for Offline
7/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
What is in the backend?
JSON is our friend.
Document databases storedocuments in the value part of akey-value store
Document databases havekey-value stores where the valuepart is examinable
Each document has unique fields:
1 id: a unique ID for thedocument
2 rev: a unique revisionnumber for the document
id and rev are required toupdate a document
The JSON attributes are wherethe data lives.
Image from [2].
8/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
Pre-CRUDy stuff
The mongoDB already exists
The database is static andavailable at all times
The collection is a group ofidentifiers
Anyone can create anddelete a collection
Database administrators canbe created andauthenticated
Lots of simularities to MongoDB.
9/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CouchDB Futon
Futon is a web UI for CouchDB
Futon alows total access to theCouchDB.
Create and delete databases
Create and deletedocuments
Create and edit JavascriptMap and Reduce functions
Image from [1].
Futon is available on cs695-nosql. Hooking a browser to it couldbe a challenge.
10/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CouchDB Futon
Making sure that CouchDB is alive and well (1 of 2)
Checking CouchDB’s health
Is it alive:curl http://127.0.0.1:5984/
Which databases are defined:curl -X GET http://127.0.0.1:5984/ all dbs
Information about a database:curl -X GET http://127.0.0.1:5984/ users
11/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CouchDB Futon
Making sure that CouchDB is alive and well (2 of 2)
12/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Drivers and other things2
CouchDB is available via curlcommands, or drivers for
Java, JavaScript, C#,python, ruby,
And others.
Image from [3].
Too many choices. All use HTTP ReST communications backend.2https://cwiki.apache.org/confluence/display/COUCHDB/CouchDB+clients
13/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Create a database (1 of 2)
The database is created when the PUT command is executed:
curl -X PUT http://127.0.0.1:5984/<database name >
curl -X PUT http://127.0.0.1:5984/test db
14/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Create a database (2 of 2)
15/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Create data3 (1 of 2)
curl -X POST URLtoDataBase -H ’Content-Type:application/json’ -d ’JSON’
curl -X POST http://couchhost:5984/test db -H’Content-Type: application/json’ -d ’{”FirstName”:”Isabella”,”FamilyName”:”Huegdon”, ”Age”:2, ”Likes”:[”noise”,”animals”, ”chocolate”, ”kicking Milly”, ”throwingtantrums”]}’
3Data taken from:http://nefariousdesigns.co.uk/couchdbs-restful-api.html
16/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Create data (2 of 2)
17/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Find data (1 of 2)
Seehttp://couchdb-13.readthedocs.org/en/latest/api/database/
for complete list of database methods.
curl -X GET URLtoCouchHost/databse/ all docs
curl -X GET http://127.0.0.1:5984/test db/ all docs
curl -X GET URLtoCouchHost/databse/id
curl -X GET http://127.0.0.1:5984/test db/19124749379a656b309520811100020a
18/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Find data (2 of 2)
19/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Update data (1 of 2)
Partial document updates are not supported. An update willreplace the previous document in its entirety.
Find the document based on id and rev
Update the JSON data locally
Add the id and rev fields to the JSON data
POST the update JSON to the database
CouchDB is a key-value database. The id and rev fields are thekey, and the JSON is the value.
20/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Update data (2 of 2)
21/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Remove a document (1 of 3)
Its not as obvious as you might think[6].
curl -X DELETEURLtoCouchHost/id?rev=REVISION-STRING
curl -X DELETE http://127.0.0.1:5984/test db/19124749379a656b309520811100020a?rev=2-c5c854b1d41d5ed00a2039400b4950d8
Alternative way is to UPDATE the document with a newJSON containing the “ deleted” attribute set to “true”
Alternative way is to POST document information theCouchDB’s purge API
22/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Remove a document (2 of 3)
23/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
CRUDy nuts and bolts
Remove a document (3 of 3)
“Note: Deleted documents remain in the databaseforever, even after compaction, to allow eventualconsistency when replicating. If you delete using theDELETE method above, only the id, rev and a deletedflag are preserved. If you deleted a document by adding“ deleted”:true then all the fields of the document arepreserved. This is to allow, for example, recording thetime you deleted a document, or the reason you deletedit.”
Couchdb Wiki Staff [5]
24/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
Special fields
CouchDB has special fields4
Name Description
id The unique identifier of the document (mandatory and im-mutable)
rev The current MVCC-token/revision of this document (manda-tory and immutable)
attachments If the document has attachments, attachments holds a(meta-)data structure
deleted Indicates that this document has been deleted and previousrevisions will be removed on next compaction run
revisions Revision history of the documentrevs info A list of revisions of the document, and their availabilityconflicts Information about conflictsdeleted conflicts Information about conflictslocal seq Sequence number of the revision in the database (as found
in the changes feed)
4https://wiki.apache.org/couchdb/HTTP_Document_API
25/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
What have we covered?
Had mid-termReviewed assignment #05Covered some of theCouchDB CRUDy stuffRemember Assignment #05due before next class
Next time: continued CRUDy exploration
26/26
Miscellanea Mid-term DB comparisons Assgn. #5 Origins and history Data model CRUDy stuff Conclusion References
References I
[1] J Chris Anderson, Jan Lehnardt, and Noah Slater, Couchdb: The definitiveguide, O’Reilly Media, Inc., 2010.
[2] Bradley Holt, Learning couchdb,http://www.slideshare.net/bradley.holt/oscon-2011-learning-couchdb,2011.
[3] Tim Juravich, Getting started with couchdb and futon,https://www.packtpub.com/books/content/getting-started-couchdb-and-futon
2012.
[4] Eric Redmond and Jim R Wilson, Seven databases in seven weeks,Pragmatic Bookshelf, 2012.
[5] CouchDB Wiki Staff, Http document api,https://wiki.apache.org/couchdb/HTTP_Document_API, 2015.
[6] Nate Vanderwilt, The three ways to remove a document from couchdb,http://n.exts.ch/2012/11/baleting, 2012.