• Share
  • Email
  • Embed
  • Like
  • Private Content
Developing couchbase part iii   advanced app dev
 

Developing couchbase part iii advanced app dev

on

  • 2,290 views

 

Statistics

Views

Total Views
2,290
Views on SlideShare
2,290
Embed Views
0

Actions

Likes
1
Downloads
10
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
  • 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

Developing couchbase part iii   advanced app dev Developing couchbase part iii advanced app dev Presentation Transcript

  • Developing with Couchbase Part III: The Client Libraries John Zablocki Developer Advocate @codevoyeur 1
  • Querying with couchbase 2
  • The query api (rehash) 3
  • 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
  • The View API 5
  • 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
  • A Beer Document{ "_id": "brewery_cottrell_brewing", "_rev": "1-00003e56c0e830c80000005900000112", "type": "brewery", "name": "Cottrell Brewing", "city": "Pawcatuck", "province": "Connecticut"} 7
  • 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
  • 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
  • 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
  • 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
  • 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
  • Using the query api for calculation 13
  • 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
  • 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();$resultPages->setRowsPerPage(10);$resultPages->setOptions(array( "startkey" => "A", "endkey" => "B");foreach($resultPages AS $row) { echo $row->key . ": " $row->value;} 19
  • 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
  • Error handling 21
  • 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
  • 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
  • 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
  • Q&A 25