Introduction to couch_db
Upcoming SlideShare
Loading in...5
×
 

Introduction to couch_db

on

  • 1,184 views

 

Statistics

Views

Total Views
1,184
Views on SlideShare
1,184
Embed Views
0

Actions

Likes
0
Downloads
15
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Introduction to couch_db Introduction to couch_db Presentation Transcript

  • Introduction to CouchDB Jon Allen (JJ) http://perl.jonallen.info - jj@jonallen.info
  • What is CouchDB?
    • Document Oriented Database
    Introduction to CouchDB
  • 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
  • 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
  • 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
  • Using CouchDB from Perl
    • HTTP REST API
    • No DBI, DBD, DBIx::Class etc
    • CPAN modules
      • CouchDB::Client
      • AnyEvent::CouchDB
    Introduction to CouchDB
  • 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 "Could not create DB"; };
  • 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 "Could not create document"; };
  • 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 "Could not create document"; }; Document ID, must be unique Revision ID
  • CouchDB GUI Introduction to CouchDB
  • Querying documents Introduction to CouchDB Map function Key, Value Key, Value Key, Value Key, Value All Documents Query by Key Key, Value Key, Value
  • 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
  • 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
  • Designing a view
    • Show messages for a specific user
    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
  • 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;; };
  • 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);
  • 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 };
  • 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
  • KTHKSBYE! Introduction to CouchDB Thank you for listening! Any questions? http://perl.jonallen.info/talks