CouchConf Tokyo Developing with Couchbase Part III
-
Upload
couchbase -
Category
Technology
-
view
1.896 -
download
1
Transcript of CouchConf Tokyo Developing with Couchbase Part III
![Page 1: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/1.jpg)
1
Developing with Couchbase Part III:Advanced Application Development
Matt IngenthronDirector, Developer Solutions
![Page 2: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/2.jpg)
5
THE QUERY API
![Page 3: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/3.jpg)
6
Querying with PHP – Custom View
<?javascript
// map function
function(doc) {
if(doc.name) {
emit(doc.name);
}
}
<?php
include "Couchbase.php";
$cb = new Couchbase;
$cb->addCouchbaseServer("localhost");
$view = $cb->getView("beer/names"); // "custom" view
$result = $view->query();
foreach($result AS $row) {
echo $row->key . ": " . $row->value;
}
![Page 4: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/4.jpg)
7
Querying with PHP – Custom View Reverse
<?php
include "Couchbase.php";
$cb = new Couchbase;
$cb->addCouchbaseServer("localhost");
$view = $cb->getView("beer/names");
$result = $view->query(array(
"descending" => true
));
foreach($result AS $row) {
echo $row->key . ": " $row->value;
}
![Page 5: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/5.jpg)
8
Querying with PHP – Custom View Key
<?php
include "Couchbase.php";
$cb = new Couchbase;
$cb->addCouchbaseServer("localhost");
$view = $cb->getView("beer/names");
$result = $view->query(array(
"key" => "Hoptimus Prime"
));
foreach($result AS $row) {
echo $row->key . ": " $row->value;
}
![Page 6: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/6.jpg)
9
Querying with PHP – Custom View Key Range
<?php
include "Couchbase.php";
$cb = new Couchbase;
$cb->addCouchbaseServer("localhost");
$view = $cb->getView("beer/names");
$result = $view->query(array(
"startkey" => "A",
"endkey" => "B"
));
foreach($result AS $row) {
echo $row->key . ": " $row->value;
}
![Page 7: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/7.jpg)
10
Querying with PHP – Custom View Key Range Limit
<?php
include "Couchbase.php";
$cb = new Couchbase;
$cb->addCouchbaseServer("localhost");
$view = $cb->getView("beer/names");
$result = $view->query(array(
"startkey" => "A",
"endkey" => "B",
"limit" => 10
));
foreach($result AS $row) {
echo $row->key . ": " $row->value;
}
![Page 8: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/8.jpg)
11
USING THE QUERY API FOR CALCULATION
![Page 9: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/9.jpg)
12
Querying with PHP – Custom View
<?javascript
// map function
function(doc) {
if(doc.category) {
emit(doc.category, 1);
}
}
//reduce: _count
<?php
include "Couchbase.php";
$cb = new Couchbase;
$cb->addCouchbaseServer("localhost");
$view = $cb->getView("beer/categories"); // "custom" view
$result = $view->query(array("group" => true));
foreach($result AS $row) {
echo $row->key . ": " $row->value;
}
![Page 10: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/10.jpg)
13
View Calculation Result
![Page 11: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/11.jpg)
16
PAGING OVER VIEWS
![Page 12: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/12.jpg)
17
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;
}
![Page 13: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/13.jpg)
18
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>';
![Page 14: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/14.jpg)
19
ERROR HANDLING
![Page 15: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/15.jpg)
20
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);
}
![Page 16: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/16.jpg)
21
Error Handling
<?php
// ... skip setup
$result = $cb->set("a", 1);
if($result == false) { // something went wrong
$code = $cb->getResultCode(); // lets find it
switch($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;
}
}
![Page 17: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/17.jpg)
22
• 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
![Page 18: CouchConf Tokyo Developing with Couchbase Part III](https://reader034.fdocuments.in/reader034/viewer/2022052117/58f2dcb61a28ab031f8b45df/html5/thumbnails/18.jpg)
23
Q&A