Introduction to CouchDB Jon Allen (JJ) http://perl.jonallen.info - jj@jonallen.info
What is CouchDB? <ul><li>Document Oriented Database </li></ul>Introduction to CouchDB
What is CouchDB? <ul><li>Document Oriented Database </li></ul><ul><ul><li>No schema </li></ul></ul><ul><ul><ul><li>Stores ...
What is CouchDB? <ul><li>Document Oriented Database </li></ul><ul><ul><li>No schema </li></ul></ul><ul><ul><ul><li>Stores ...
Why use CouchDB? <ul><li>Store complex data structures (JSON) </li></ul><ul><li>Store variable data structures (no schema)...
Using CouchDB from Perl <ul><li>HTTP REST API </li></ul><ul><li>No DBI, DBD, DBIx::Class etc </li></ul><ul><li>CPAN module...
Creating a database Introduction to CouchDB use CouchDB::Client; use Try::Tiny; my $client = CouchDB::Client->new( uri => ...
Inserting a document Introduction to CouchDB my $client = CouchDB::Client->new(); my $db  = $client->newDB('jj_test'); my ...
Inserting a document Introduction to CouchDB my $client = CouchDB::Client->new(); my $db  = $client->newDB('jj_test'); my ...
CouchDB GUI Introduction to CouchDB
Querying documents Introduction to CouchDB Map function Key, Value Key, Value Key, Value Key, Value All Documents Query by...
Views <ul><li>A view is a JavaScript function  </li></ul><ul><ul><li>Like a stored procedure in SQL </li></ul></ul><ul><li...
Queries <ul><li>Queries are run against views </li></ul><ul><ul><li>i.e. searches the &quot;key&quot; of the list of KV pa...
Designing a view <ul><li>Show messages for a specific user </li></ul>Introduction to CouchDB my $view = <<EOT; function(do...
Creating a view Introduction to CouchDB my $client = CouchDB::Client->new(); my $db  = $client->newDB('jj_test'); my $doc ...
Querying a view Introduction to CouchDB use Data::Dumper; my $client = CouchDB::Client->new(); my $db  = $client->newDB('j...
Query results Introduction to CouchDB varos:talk_scripts jj$ perl5.10.1 query_view.pl $VAR1 = { 'total_rows' => 3, 'rows' ...
Conclusion <ul><li>Different mindset to SQL database </li></ul><ul><li>Quite low-level, but very powerful </li></ul><ul><l...
KTHKSBYE! Introduction to CouchDB Thank you for listening! Any questions? http://perl.jonallen.info/talks
Upcoming SlideShare
Loading in...5
×

Introduction to couch_db

1,083

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,083
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to couch_db

  1. 1. Introduction to CouchDB Jon Allen (JJ) http://perl.jonallen.info - jj@jonallen.info
  2. 2. What is CouchDB? <ul><li>Document Oriented Database </li></ul>Introduction to CouchDB
  3. 3. What is CouchDB? <ul><li>Document Oriented Database </li></ul><ul><ul><li>No schema </li></ul></ul><ul><ul><ul><li>Stores &quot;documents&quot; (i.e. data structures) </li></ul></ul></ul><ul><ul><li>No SQL </li></ul></ul><ul><ul><ul><li>Uses MapReduce queries written in JavaScript </li></ul></ul></ul>Introduction to CouchDB
  4. 4. What is CouchDB? <ul><li>Document Oriented Database </li></ul><ul><ul><li>No schema </li></ul></ul><ul><ul><ul><li>Stores &quot;documents&quot; (i.e. data structures) </li></ul></ul></ul><ul><ul><li>No SQL </li></ul></ul><ul><ul><ul><li>Uses &quot;MapReduce&quot; queries written in JavaScript </li></ul></ul></ul><ul><li>Written in Erlang </li></ul><ul><li>REST API </li></ul><ul><li>Replication, scalability </li></ul>Introduction to CouchDB
  5. 5. Why use CouchDB? <ul><li>Store complex data structures (JSON) </li></ul><ul><li>Store variable data structures (no schema) </li></ul><ul><li>De-normalised / self contained </li></ul><ul><li>Add attachments to documents </li></ul><ul><li>Scalable and fault tolerant </li></ul><ul><li>Better fit for certain problem domains </li></ul><ul><ul><li>Messaging </li></ul></ul><ul><ul><li>CMS </li></ul></ul>Introduction to CouchDB
  6. 6. Using CouchDB from Perl <ul><li>HTTP REST API </li></ul><ul><li>No DBI, DBD, DBIx::Class etc </li></ul><ul><li>CPAN modules </li></ul><ul><ul><li>CouchDB::Client </li></ul></ul><ul><ul><li>AnyEvent::CouchDB </li></ul></ul>Introduction to CouchDB
  7. 7. Creating a database Introduction to CouchDB 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 &quot;Could not create DB&quot;; };
  8. 8. Inserting a document Introduction to CouchDB 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 &quot;Could not create document&quot;; };
  9. 9. Inserting a document Introduction to CouchDB 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 &quot;Could not create document&quot;; }; Document ID, must be unique Revision ID
  10. 10. CouchDB GUI Introduction to CouchDB
  11. 11. Querying documents Introduction to CouchDB Map function Key, Value Key, Value Key, Value Key, Value All Documents Query by Key Key, Value Key, Value
  12. 12. Views <ul><li>A view is a JavaScript function </li></ul><ul><ul><li>Like a stored procedure in SQL </li></ul></ul><ul><li>Map function is executed on every document in the database </li></ul><ul><li>Emits key/value pairs </li></ul><ul><ul><li>Can emit 0, 1, or more KV pairs for each document in the database </li></ul></ul><ul><ul><li>Keys and values can be complex data structures </li></ul></ul><ul><li>KV pairs are then ordered and indexed by key </li></ul>Introduction to CouchDB
  13. 13. Queries <ul><li>Queries are run against views </li></ul><ul><ul><li>i.e. searches the &quot;key&quot; of the list of KV pairs </li></ul></ul><ul><li>Query types: </li></ul><ul><ul><li>Exact: key = x </li></ul></ul><ul><ul><li>Range: key is between x and y </li></ul></ul><ul><ul><li>Multiple: key is in list (x,y,z) </li></ul></ul><ul><li>Reduce functions </li></ul><ul><ul><li>e.g. count, sum, group </li></ul></ul>Introduction to CouchDB
  14. 14. Designing a view <ul><li>Show messages for a specific user </li></ul>Introduction to CouchDB my $view = <<EOT; 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); } } } } EOT
  15. 15. Creating a view Introduction to CouchDB my $client = CouchDB::Client->new(); my $db = $client->newDB('jj_test'); my $doc = $db->newDesignDoc('_design/myview',undef, { views => { messages_to => { map => $view }, }, }); try { $doc->create; } catch { die &quot;Could not create document&quot;; };
  16. 16. Querying a view Introduction to CouchDB use 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 &quot;Could not query view&quot;; }; say Dumper($result);
  17. 17. Query results Introduction to CouchDB varos:talk_scripts jj$ perl5.10.1 query_view.pl $VAR1 = { 'total_rows' => 3, 'rows' => [ { 'value' => 'Hello, World', 'id' => 'docid', 'key' => 'JJ' } ], 'offset' => 2 };
  18. 18. Conclusion <ul><li>Different mindset to SQL database </li></ul><ul><li>Quite low-level, but very powerful </li></ul><ul><li>Additional features </li></ul><ul><ul><li>Replication </li></ul></ul><ul><ul><li>Document revisions </li></ul></ul><ul><ul><li>Reduce functions </li></ul></ul><ul><ul><li>Changes API </li></ul></ul><ul><li>More information: http://books.couchdb.org/relax </li></ul>Introduction to CouchDB
  19. 19. KTHKSBYE! Introduction to CouchDB Thank you for listening! Any questions? http://perl.jonallen.info/talks
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×