Introduction to CouchDB

1,546 views

Published on

Presented at the YAPC::Europe conference in Pisa, Italy

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,546
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction to CouchDB

  1. 1. Introduction to CouchDBJon Allen (JJ) – jj@opusvl.com YAPC::Europe 2010 Introduction to CouchDB Open Source Business Systems www.opusvl.com
  2. 2. What is CouchDB? •  Document Oriented Database Introduction to CouchDB Open Source Business Systems www.opusvl.com
  3. 3. What is CouchDB? •  Document Oriented Database –  No schema •  Stores "documents" (i.e. data structures) –  No SQL •  Uses "MapReduce" queries written in JavaScript Introduction to CouchDB Open Source Business Systems www.opusvl.com
  4. 4. What is CouchDB? •  Document Oriented Database –  No schema •  Stores "documents" (i.e. data structures) –  No SQL •  Uses "MapReduce" queries written in JavaScript•  Written in Erlang•  REST API•  Replication, scalability Introduction to CouchDB Open Source Business Systems www.opusvl.com
  5. 5. Why use CouchDB? •  Store complex data structures (JSON)•  Store variable data structures (no schema)•  De-normalised / self contained•  Add attachments to documents•  Scalable and fault tolerant•  Better fit for certain problem domains –  Messaging –  CMS Introduction to CouchDB Open Source Business Systems www.opusvl.com
  6. 6. Using CouchDB from Perl •  HTTP REST API•  No DBI, DBD, DBIx::Class etc•  CPAN modules –  CouchDB::Client –  AnyEvent::CouchDB Introduction to CouchDB Open Source Business Systems www.opusvl.com
  7. 7. Creating a database use CouchDB::Client;use Try::Tiny;my $client = CouchDB::Client->new( uri => http://localhost:5984);my $db = $client->newDB(jj_test); # lower case!try { $db->create;} catch { die "Could not create DB";}; Introduction to CouchDB Open Source Business Systems www.opusvl.com
  8. 8. Inserting a document my $client = CouchDB::Client->new();my $db = $client->newDB(jj_test);my $doc = $db->newDoc(docid, undef, { type => message, text => Hello, World, to => [JJ, Barbie, Brian],});try { $doc->create;} catch { die "Could not create document";}; Introduction to CouchDB Open Source Business Systems www.opusvl.com
  9. 9. Inserting a document my $client = CouchDB::Client->new();my $db = $client->newDB(jj_test);my $doc = $db->newDoc(docid, undef, { type => message, text => Hello, World, to => [JJ, Barbie, Brian],});try { Document ID, Revision ID $doc->create; must be unique} catch { die "Could not create document";}; Introduction to CouchDB Open Source Business Systems www.opusvl.com
  10. 10. CouchDB GUI Introduction to CouchDBOpen Source Business Systems www.opusvl.com
  11. 11. Querying documents All Documents Map function Key, ValueKey, Value Key, Value Query by KeyKey, Value Key, Value Key, Value Introduction to CouchDB Open Source Business Systems www.opusvl.com
  12. 12. Views •  A view is a JavaScript function –  Like a stored procedure in SQL•  Map function is executed on every document in the database•  Emits key/value pairs –  Can emit 0, 1, or more KV pairs for each document in the database –  Keys and values can be complex data structures•  KV pairs are then ordered and indexed by key Introduction to CouchDB Open Source Business Systems www.opusvl.com
  13. 13. Queries •  Queries are run against views –  i.e. searches the "key" of the list of KV pairs•  Query types: –  Exact: key = x –  Range: key is between x and y –  Multiple: key is in list (x,y,z) •  Reduce functions –  e.g. count, sum, group Introduction to CouchDB Open Source Business Systems www.opusvl.com
  14. 14. Designing a view•  Show messages for a specific user // This is JavaScript function(doc) { if (doc.type && doc.to && doc.text) { if (doc.type == message) { for (var dest in doc.to) { emit(doc.to[dest], doc.text); } } } } Introduction to CouchDB Open Source Business Systems www.opusvl.com
  15. 15. Creating a view•  Can either use the GUI or the API•  View stored in database as a "Design Document" –  A design document can contain multiple views•  Example –  Design document "_design/myview" –  View name "messages_to" Introduction to CouchDB Open Source Business Systems www.opusvl.com
  16. 16. Querying a viewuse Data::Dumper;my $client = CouchDB::Client->new();my $db = $client->newDB(jj_test);my $view = $db->newDesignDoc(_design/myview) ->retrieve;my $result = try { $view->queryView(messages_to, (key => JJ));} catch { die "Could not query view";};say Dumper($result); Introduction to CouchDB Open Source Business Systems www.opusvl.com
  17. 17. Query results varos:talk_scripts jj$ perl5.10.1 query_view.pl$VAR1 = { total_rows => 3, rows => [ { value => Hello, World, id => docid, key => JJ } ], offset => 2 }; Introduction to CouchDB Open Source Business Systems www.opusvl.com
  18. 18. Conclusion •  Different mindset to SQL database•  Quite low-level, but very powerful•  Additional features –  Replication –  Document revisions –  Reduce functions –  Changes API•  More information: http://books.couchdb.org/relax Introduction to CouchDB Open Source Business Systems www.opusvl.com
  19. 19. KTHKSBYE! Thank you for listening! Any questions? http://www.opusvl.com http://perl.jonallen.info/talks Introduction to CouchDB Open Source Business Systems www.opusvl.com

×