Amazon Web Services - SimpleDB using the PHP SDK

Written by Vincent on 27 October 2010 – 12:15 am -

Long time no post… here’s a nerdy one which I am writing since I have found no information on it anywhere on the internet.

Amazon Web Services has recently released their PHP SDK. It’s not the most well documented library out there but it does make doing a lot of things on AWS a lot easier. I’ve been working on a few small projects as proof-of-concepts for scalable web applications that can run off a regular web server but leverage the cloud for storage and extra compute capacity.

One component I’ve spent a few hours on working out has been a monitoring component. Basically, a script runs regularly and collects a few statistics (e.g. S3 bucket size), storing all the information into a SimpleDB domain. Anyway, what it does isn’t relevant - the point is that it stores a load of data roughly resembling a logfile using SimpleDB. That bit is all very easy to architect and develop.

The hard part is retrieving this information. I’m planning to use the Google Visualisation API to generate some cool graphs, and I was planning to create a simple PHP script that would basically extract some log data and output it to CSV. This is where the roadblock is. Amazon SimpleDB result sets are restricted to the lesser of 100 rows or 1 MB. Queries with larger result sets are paged, and each result set will contain a token which can be supplied on a subsequent request. Sounds simple.

But the SDK has no method to retrieve the token from the request. The samples don’t mention it and the API reference is just a JavaDoc which doesn’t include much detail. (Although it at least does tell you how to pass the token on the next request). Without a way around this, the PHP SDK is just about useless for SimpleDB in most use cases where you’d use SimpleDB over a traditional RDBMS like MySQL.

It took some analysis and reverse engineering and eventually I found out how to get to the NextToken value in the response. Here’s a code snippet. I just realised I set this blog up for photography so I never bothered to put in a decent code highlighter so bear with me.

$sdb = new AmazonSDB();

$opt = array();
$query = “SELECT * FROM `opengal_monitor`”;

do {
// Send query to SimpleDB
$results = @$sdb->select($query,$opt);

// The value for NextToken is found here
$opt['NextToken'] = $results->body->SelectResult->NextToken;

// Remove all line breaks or subsequent queries will fail
$opt['NextToken'] =
ereg_replace(”/\r|\n/”, “”, $opt['NextToken']);

// Put the items in an array
$items = $results->body->Item();

// Do Stuff

} while ($opt['NextToken']);

And that’s it! Based on this, it would be a trivial task to overload the CFResponse class for SimpleDB Select operations and provide a method that would return the NextToken value in its correct format (i.e. no linebreaks).


japan trip - day zero

22 March 2009 – 7:24 pm

It has now been four months since I left on my trip to Japan. It was an amazing experience, full of all sorts of surprises that we would have never ...

Poor man’s studio

12 March 2009 – 3:56 am

I originally wrote this post last November but for unknown reasons never published it. I might as well, because I couldn't find any instructions for my setup on the internet. ...

Gallery Works!

15 January 2009 – 5:55 am

Well, I realised that developing a full featured gallery was never going to succeed because I keep changing the plans, so I decided to take another development approach. Make the ...

Windows fail at logic

28 December 2008 – 9:39 pm

Microsoft has all kinds of logical reasons behind making things not work.

lawl

13 December 2008 – 6:57 pm

I'm still alive. And yes, I really should post something. But I'm far too lazy. Meanwhile, check out some of these other blogs: Euphoric Trance Rusty - Just Another Blog Cameland

Atop Mt Dandenong

26 April 2008 – 1:33 pm

Bored shitless on Anzac Day, so what better to do than head up to the top of Mt Dandenong once again - this time circumventing the entry fee at Sky ...

Too much camera gear.

22 April 2008 – 2:52 pm

Hmm, I seem to have gathered a decent amount of stuff over the years. (crappy picture yes, but obviously all my good cameras are in the photo) Left to right: Nikon AF Nikkor ...

Fed Square, Southbank and Surrounds at Night (11/3)

6 April 2008 – 8:34 pm

This is just testing the new gallery functionality in Wordpress 2.5. If this works, I won't have to develop my custom gallery. [gallery]

Unpacking Jacob’s Polaroid

29 February 2008 – 11:00 am

The awesomeness of my own vintage Polaroid convinced Jacob to also purchase a 360 - here are the unpacking photos. 35 year old packaging yay: 30 year old batteries ftw: Fresh batteries, $20 ...

About Me

This is the personal blog of Vincent Quach. I'm currently a student part way through a Bachelor of Business Information Systems degree and from time to time will engage in activities such as web development, photography, piano playing, writing pointless rants or anything else that I can use as an excuse to avoid doing something more productive. More

Want to subscribe?

 Subscribe in a reader   Or, subscribe via email:
Enter your email address:  
Find entries :