Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Developing couchbase part iii advanced app dev

3,402 views

Published on

Published in: Technology
  • Be the first to comment

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

×