11
Developing with Couchbase Part III:
The Client LibrariesJohn ZablockiDeveloper Advocate
@codevoyeur
55
The View API
6
Preamble
All examples are in PHP using the Couchbase PHP SDK 1.1
Download at couchbase.com/develop/php/next
Here's 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
7
A Beer Document
{"_id": "brewery_cottrell_brewing","_rev": "1-00003e56c0e830c80000005900000112","type": "brewery","name": "Cottrell Brewing","city": "Pawcatuck","province": "Connecticut"
}
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;}
9
Querying with PHP – Custom View Reverse
Flip it withdescending = true
// PHP Code
$result= $cb->view(“beers“, “by_name?descending=true");
foreach($result AS $row) { echo $row->key . ": " $row->value;}
CAUTION: it's not always what you think
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;}
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;}
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;}
1313
Using the query api for calculation
14
Querying with PHP – Custom View
_design/beer/_view/categories
// map functionfunction(doc) { if(doc.category) { emit(doc.category, 1); }}// reduce: _count
via PHP
$view = $cb->getView("beer/categories");$result = $view->query(array( "group" => true));
foreach($result AS $row) { echo $row->key . ": " $row->value;}
15
View Calculation Result
1818
Paging over views
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;}
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>';
2121
Error handling
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);}
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;}}
24
• Would be great if everything went perfectly in a cluster, but…– Node failures– System becomes overloaded– Temporary out of memory conditions
Cluster Ready Error Handling
2525
Q&A