SlideShare a Scribd company logo
Introduction to CouchDB

Jon Allen (JJ) – jj@opusvl.com
     YAPC::Europe 2010


     Introduction to CouchDB
     Open Source Business Systems
   www.opusvl.com
What is CouchDB?
                           

•  Document Oriented Database




               Introduction to CouchDB
               Open Source Business Systems
   www.opusvl.com
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
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
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
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
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
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
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
CouchDB GUI
           




Introduction to CouchDB
Open Source Business Systems
   www.opusvl.com
Querying documents
                              

  All Documents
                                                  Map function




                                                   Key, Value
Key, Value                                         Key, Value
                        Query by Key
Key, Value
                                                   Key, Value

                                                   Key, Value


                  Introduction to CouchDB
                  Open Source Business Systems
     www.opusvl.com
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
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
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
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
Querying a view
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 "Could not query view";
};

say Dumper($result);

              Introduction to CouchDB
              Open Source Business Systems
   www.opusvl.com
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
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
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

More Related Content

PDF
Software Development with Open Source
PDF
OpenERP and Perl
PDF
Perl in the Real World
PPTX
Saving Time with WP-CLI
PDF
You Got React.js in My PHP
PPTX
Best Practices for WordPress in Enterprise
PPTX
Best Practices for Building WordPress Applications
PDF
JSON REST API for WordPress
Software Development with Open Source
OpenERP and Perl
Perl in the Real World
Saving Time with WP-CLI
You Got React.js in My PHP
Best Practices for WordPress in Enterprise
Best Practices for Building WordPress Applications
JSON REST API for WordPress

What's hot (19)

PPTX
The JSON REST API for WordPress
PDF
Best Practices for WordPress
PDF
Mini Rails Framework
PPT
DSpace UI Prototype Challenge: Spring Boot + Thymeleaf
PDF
Fast Web Applications Development with Ruby on Rails on Oracle
PDF
PDF
Vibe Custom Development
PPTX
Day 7 - Make it Fast
PDF
Custom Development with Novell Teaming
PDF
Php converted pdf
PDF
Top ten-list
PDF
Put a Button on It: Removing Barriers to Going Fast
PDF
Transforming WordPress Search and Query Performance with Elasticsearch
PDF
Rapid prototyping with solr - By Erik Hatcher
PPTX
Web Ninja
PPT
Website designing company_in_delhi_phpwebdevelopment
PDF
Ruby On Rails Introduction
PDF
Modernizing WordPress Search with Elasticsearch
The JSON REST API for WordPress
Best Practices for WordPress
Mini Rails Framework
DSpace UI Prototype Challenge: Spring Boot + Thymeleaf
Fast Web Applications Development with Ruby on Rails on Oracle
Vibe Custom Development
Day 7 - Make it Fast
Custom Development with Novell Teaming
Php converted pdf
Top ten-list
Put a Button on It: Removing Barriers to Going Fast
Transforming WordPress Search and Query Performance with Elasticsearch
Rapid prototyping with solr - By Erik Hatcher
Web Ninja
Website designing company_in_delhi_phpwebdevelopment
Ruby On Rails Introduction
Modernizing WordPress Search with Elasticsearch
Ad

Similar to Introduction to CouchDB (20)

PDF
Introduction to couchdb
PPT
No sql Database
PDF
Python-CouchDB Training at PyCon PL 2012
PPTX
CouchDB
PPTX
Couch db
PDF
CouchDB
PPT
NoSQL - "simple" web monitoring
PDF
Intro Couchdb
PDF
CouchDB
PDF
Couch db
PPT
Introduction to couch_db
PDF
Vidoop CouchDB Talk
PPTX
Cluster of unreliable commodity hardware (couchdb) (2)
PPTX
Couch DB
PDF
CouchDB Open Source Bridge
PPT
Couch db
PDF
NoSQL and CouchDB: the view from MOO
PDF
Couchdb Nosql
PDF
Introduction to CouchDB
Introduction to couchdb
No sql Database
Python-CouchDB Training at PyCon PL 2012
CouchDB
Couch db
CouchDB
NoSQL - "simple" web monitoring
Intro Couchdb
CouchDB
Couch db
Introduction to couch_db
Vidoop CouchDB Talk
Cluster of unreliable commodity hardware (couchdb) (2)
Couch DB
CouchDB Open Source Bridge
Couch db
NoSQL and CouchDB: the view from MOO
Couchdb Nosql
Introduction to CouchDB
Ad

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
cuic standard and advanced reporting.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
Teaching material agriculture food technology
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Modernizing your data center with Dell and AMD
Unlocking AI with Model Context Protocol (MCP)
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
Mobile App Security Testing_ A Comprehensive Guide.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation_ Review paper, used for researhc scholars
cuic standard and advanced reporting.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Network Security Unit 5.pdf for BCA BBA.
Teaching material agriculture food technology
Understanding_Digital_Forensics_Presentation.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Building Integrated photovoltaic BIPV_UPV.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Big Data Technologies - Introduction.pptx
Chapter 3 Spatial Domain Image Processing.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Modernizing your data center with Dell and AMD

Introduction to CouchDB

  • 1. Introduction to CouchDB Jon Allen (JJ) – jj@opusvl.com YAPC::Europe 2010 Introduction to CouchDB Open Source Business Systems www.opusvl.com
  • 2. What is CouchDB? •  Document Oriented Database Introduction to CouchDB Open Source Business Systems www.opusvl.com
  • 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. 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. 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. 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. 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. 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. 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. CouchDB GUI Introduction to CouchDB Open Source Business Systems www.opusvl.com
  • 11. Querying documents All Documents Map function Key, Value Key, Value Key, Value Query by Key Key, Value Key, Value Key, Value Introduction to CouchDB Open Source Business Systems www.opusvl.com
  • 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. 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. 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. 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. Querying a view 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 "Could not query view"; }; say Dumper($result); Introduction to CouchDB Open Source Business Systems www.opusvl.com
  • 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. 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. 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