Your SlideShare is downloading. ×
0

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Developing couchbase part iii advanced app dev

2,372

Published on

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

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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 "A”
  • All beers with "A", 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 "A", 10 per page, first page.
  • All beers with "A", 10 per page, more pages.
  • More Details, many more, see the docs
  • Transcript

    • 1. Developing with Couchbase Part III: The Client Libraries John Zablocki Developer Advocate @codevoyeur 1
    • 2. Querying with couchbase 2
    • 3. The query api (rehash) 3
    • 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. The View API 5
    • 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. A Beer Document{ "_id": "brewery_cottrell_brewing", "_rev": "1-00003e56c0e830c80000005900000112", "type": "brewery", "name": "Cottrell Brewing", "city": "Pawcatuck", "province": "Connecticut"} 7
    • 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. 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. 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. 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. 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. Using the query api for calculation 13
    • 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. View Calculation Result 15
    • 16. TBD: _stats & _sum 16
    • 17. TBD: Custom reduce 17
    • 18. Paging over views 18
    • 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. 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. Error handling 21
    • 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. 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. 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. Q&A 25

    ×