Developing with Couchbase Part III:          The Client Libraries                     John Zablocki                    Dev...
Querying with couchbase                          2
The query api (rehash)                         3
Querying with PHP – All Documents<?phpinclude "Couchbase.php";$cb = new Couchbase;$cb->addCouchbaseServer("localhost");$vi...
The View API               5
PreambleAll examples are in PHP using the Couchbase PHP SDK 1.1Download at couchbase.com/develop/php/nextHeres how to conn...
A Beer Document{    "_id": "brewery_cottrell_brewing",    "_rev": "1-00003e56c0e830c80000005900000112",    "type": "brewer...
Querying with PHP – Custom View_design/beer/_view/names// map functionfunction(doc) {  if(doc.name) {    emit(doc.name, 1)...
Querying with PHP – Custom View ReverseFlip it withdescending = true// PHP Code$result= $cb->view(“beers“, “by_name?descen...
Querying with PHP – Custom View Key// PHP Code$result= $cb->view("beer”, “by_name?=‘Hoptimus Prime’”);foreach($result AS $...
Querying with PHP – Custom View Key Range// PHP Code$result= $cb->view(“beers“, “by_name?startkey=‘A’&endkey=‘B’”);foreach...
Querying with PHP – Custom View Key Range Limit// PHP Code$view = $cb->getView("beer/names");$result = $view->query(array(...
Using the query api for calculation                                      13
Querying with PHP – Custom View_design/beer/_view/categories// map functionfunction(doc) {  if(doc.category) {    emit(doc...
View Calculation Result                          15
TBD: _stats & _sum                     16
TBD: Custom reduce                     17
Paging over views                    18
Querying with PHP – Custom View Pagination<?php$view = $cb->getView("beer/names");$resultPages = $view->getResultPaginator...
Querying with PHP – Custom View Pagination<?php$view = $cb->getView("beer/names");$pageKey = $_GET["pageKey"]; // URL ESCA...
Error handling                 21
Error Handling<?php// ... skip setup$cb->set("a", 1); // Silent Oops.$result = $cb->set("a", 1);if($result == false) {  $c...
Error Handling<?php// ... skip setup$result = $cb->set("a", 1);if($result == false) { // something went wrong  $code = $cb...
Cluster Ready Error Handling• Would be great if everything went perfectly in a cluster,  but…  – Node failures  – System b...
Q&A      25
Upcoming SlideShare
Loading in …5
×

Developing couchbase part iii advanced app dev

2,797
-1

Published on

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

No Downloads
Views
Total Views
2,797
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • make JS code screenshot of admin UI or show admin UI
  • make JS code screenshot of admin UI or show admin UI
  • make JS code screenshot of admin UI or show admin UI
  • descending = true
  • All beers with &amp;quot;A”
  • All beers with &amp;quot;A&amp;quot;, first 10. Pagination: later in the deck
  • make JS code screenshot of admin UI or show admin UI shows categories with their respective beer count.
  • TBD: make product screenshot
  • All beers with &amp;quot;A&amp;quot;, 10 per page, first page.
  • All beers with &amp;quot;A&amp;quot;, 10 per page, more pages.
  • More Details, many more, see the docs
  • Developing couchbase part iii advanced app dev

    1. 1. Developing with Couchbase Part III: The Client Libraries John Zablocki Developer Advocate @codevoyeur 1
    2. 2. Querying with couchbase 2
    3. 3. The query api (rehash) 3
    4. 4. Querying with PHP – All Documents<?phpinclude "Couchbase.php";$cb = new Couchbase;$cb->addCouchbaseServer("localhost");$view = $cb->getView("_all_docs"); // "built in" view$result = $view->query();foreach($result AS $row) { echo $row->key . ": ” . $row->value;} 4
    5. 5. The View API 5
    6. 6. PreambleAll examples are in PHP using the Couchbase PHP SDK 1.1Download at couchbase.com/develop/php/nextHeres how to connect to Couchbase Server:<?php// Setup Couchbase connected objectstry { $cb = new Couchbase("127.0.0.1:8091", "", "", "beer-sample");} catch (ErrorException $e) { die($e->getMessage());}// $cb will be used throughout to reference the Couchbase object 6
    7. 7. A Beer Document{ "_id": "brewery_cottrell_brewing", "_rev": "1-00003e56c0e830c80000005900000112", "type": "brewery", "name": "Cottrell Brewing", "city": "Pawcatuck", "province": "Connecticut"} 7
    8. 8. Querying with PHP – Custom View_design/beer/_view/names// map functionfunction(doc) { if(doc.name) { emit(doc.name, 1); }}via PHP$result = $cb->view(“beers“, “by_name”); // "custom" viewforeach($result AS $row) { echo $row->key . ": " . $row->value;} 8
    9. 9. Querying with PHP – Custom View ReverseFlip it withdescending = true// PHP Code$result= $cb->view(“beers“, “by_name?descending=true");foreach($result AS $row) { echo $row->key . ": " $row->value;}CAUTION: its not always what you think 9
    10. 10. Querying with PHP – Custom View Key// PHP Code$result= $cb->view("beer”, “by_name?=‘Hoptimus Prime’”);foreach($result AS $row) { echo $row->key . ": " $row->value;} 10
    11. 11. Querying with PHP – Custom View Key Range// PHP Code$result= $cb->view(“beers“, “by_name?startkey=‘A’&endkey=‘B’”);foreach($result AS $row) { echo $row->key . ": " $row->value;} 11
    12. 12. Querying with PHP – Custom View Key Range Limit// PHP Code$view = $cb->getView("beer/names");$result = $view->query(array( "startkey" => "A", "endkey" => "B", "limit" => 10));foreach($result AS $row) { echo $row->key . ": " $row->value;} 12
    13. 13. Using the query api for calculation 13
    14. 14. Querying with PHP – Custom View_design/beer/_view/categories// map functionfunction(doc) { if(doc.category) { emit(doc.category, 1); }}// reduce: _countvia PHP$view = $cb->getView("beer/categories");$result = $view->query(array( "group" => true));foreach($result AS $row) { echo $row->key . ": " $row->value;} 14
    15. 15. View Calculation Result 15
    16. 16. TBD: _stats & _sum 16
    17. 17. TBD: Custom reduce 17
    18. 18. Paging over views 18
    19. 19. Querying with PHP – Custom View Pagination<?php$view = $cb->getView("beer/names");$resultPages = $view->getResultPaginator();$resultPages->setRowsPerPage(10);$resultPages->setOptions(array( "startkey" => "A", "endkey" => "B");foreach($resultPages AS $row) { echo $row->key . ": " $row->value;} 19
    20. 20. Querying with PHP – Custom View Pagination<?php$view = $cb->getView("beer/names");$pageKey = $_GET["pageKey"]; // URL ESCAPE!$resultPages = $view->getResultPaginator();$resultPages->setPageKey($pageKey);$resultPages->setRowsPerPage(10);$resultPages->setOptions(array( "startkey" => "A", "endkey" => "B");foreach($resultPages AS $row) { echo $row->key . ": " $row->value;}$pageKey = $resultPages->key();echo <a href="?pageKey= . $pageKey. ">Next</a>; 20
    21. 21. Error handling 21
    22. 22. Error Handling<?php// ... skip setup$cb->set("a", 1); // Silent Oops.$result = $cb->set("a", 1);if($result == false) { $code = $cb->getResultCode(); echo $cb->getResultMessage($code);} 22
    23. 23. Error Handling<?php// ... skip setup$result = $cb->set("a", 1);if($result == false) { // something went wrong $code = $cb->getResultCode(); // lets find itswitch($code) { case COUCHBASE::RES_HOST_LOOKUP_FAILURE: echo "DNS lookup to Server failed"; break; case COUCHBASE::RES_UNKNOWN_READ_FAILURE: echo "Failed to read from network"; break; case COUCHBASE::RES_WRITE_FAILURE: echo "Failed to read from network"; break; case COUCHBASE:RES_TIMEOUT: echo "Operation timed out"; break;}} 23
    24. 24. Cluster Ready Error Handling• Would be great if everything went perfectly in a cluster, but… – Node failures – System becomes overloaded – Temporary out of memory conditions 24
    25. 25. Q&A 25
    1. A particular slide catching your eye?

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

    ×