October 23, 2011

Couchbase at ZendCon 2011

The organizers behind ZendCon recognized all we've been doing with Couchbase recently, and accepted a session on Couchbase.  At first, J. Chris was to discuss everything we've been up to with Couchbase Mobile, but there was an unavoidable scheduling conflict.

It turns out this conflict may have been fortunate, as it afforded me the opportunity to give updates on Couchbase for PHP developers, along with an overview of everything Couchbase: Mobile, Single Server and Server.

I have posted the slides.

Since the conference revolves around PHP, I did a demonstration with the Developer Preview of the Couchbase Client for PHP.  This client library gives developers super fast access to Couchbase Server and a PHP-familiar, nicely simplified method of querying Couchbase views.  This was put together by my colleague, Jan Lehnardt.  While he's throughly versed in the ways of PHP, I'm definitely not.

I'm not brand new to it either-- I've made things work whenever I need to.  The good news here is that it was quite straightforward for me to query Couchbase Server in PHP.

First up at ZendCon 2011, I demonstrated clustering of Couchbase Server.  Then I showed some views based on a game simulator I've written for various testing activities.  After showing the development, and manipulation of views in the Couchbase Server Web Console, I showed how you'd use the very same views from PHP.

The two views I focused on were items_per_player and player_by_level. 

The items_per_player shows how one can create a view of data built on the relationships between documents in the system.  In this case, we show all of the items a player owns. This view has a map function with no reduce:

function (doc) {
  if (doc.jsonType == "item"){
        emit(doc.ownerId, doc.name);
        }
}

 The players_by_level view, is a leaderboard of sorts.  Well, it becomes one with how we query it:

function (doc) {
  if (doc.jsonType == "player"){
    emit(["Level",  doc.level], doc._id);
  }
}

 With those two views in there, the Couchbase PHP client library is easily able to query the data bucket.  For instance, to see all items by player:

<?php

require("Couchbase.php");
$cb = new Couchbase;
$cb->addCouchbaseServer("localhost"); // connects to Couchbase ports 11211
                                      // and 8091 by default.
                                      // setup skipped


// Go get the items owned by players
echo "Items per player\n";
$view = $cb->getView("dev_players", "items_per_player");
$result = $view->getResult();
foreach($result->rows AS $row) {
  printf("Player %s owns %s\n", $row->key, $row->value);
}

Quite simple, as you see.  All I need to do is make a request of the view, and then I can iterate over the results with regular PHP iteration code.  Optionally, one would specify a specific player or a range query of players.

The leaderboard (I built this one up over the course of the demo) looks like so:

/* same setup as before */
$lbview = $cb->getView("dev_players", "player_by_level");
$leaderPages = $lbview->getResultPaginator();
$leaderPages->setRowsPerPage(10);
$leaderPages->setOptions(array("descending" => true));
$apage = $leaderPages->current();
foreach($apage->rows AS $row) {
  printf("Level %s for user %s.\n", $row->key[1], $row->value);
}

Again, really simple, but here showing a few more powerful options.  Here I'm paginating over the responses.  This is a pretty normal thing to do with web applications, and the Couchbase PHP client library does what is recommended for fast paging in the Couch guide.  In this case, you as a developer don't need to know how to implement paging; you simply say you want it and it's there for you! 

Paginating with 10 results per page, descending and looking into the JSON array returned in the view is simple.  In the demo I did, I had some 18K entries in the development view-- far too much to put in a web page.  These 10 entries, descending, is just right for my PHP app.

All in all we had a great time chatting with folks at ZendCon 2011 both at the session and in the exhibition hall.  I was very happy to see the Couchbase stickers and t-shirts wandering the halls.  Good to see all of you and see you at the next conference!

Comments