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).


Polaroids!

21 January 2008 – 9:10 pm

My D70 is still waiting for repairs, and they called today saying that it won't be another 7 days until they can even take a look to find out what's ...

A new (old) camera - Nikon F90X

16 January 2008 – 11:34 pm

I do have a habit of taking photos as many of you know, and since my D70 is dead and waiting to be fixed at the service centre, I got ...

Some HDR and some stitching

25 December 2007 – 3:41 pm

This is my first successful attempt at stitching a series of HDR images together to make a panorama, I'm pretty satisfied with the result. The final result is a composite ...

HDR Experimentation

16 November 2007 – 2:30 am

After work today, apart from going to JB Hi-fi to buy two albums I liked which I discovered by downloading them off a torrent site (i.e. record companies are full ...

End of uni

14 November 2007 – 4:26 pm

Yesterday was my final day as a second year student, and I can finally enjoy a relative degree of freedom. Lots of things to do, even if I won't get ...

Trying some infrared photography…

5 November 2007 – 12:38 am

A few weeks ago, I ordered an infrared filter over ebay, after having seen various pictures of scenes photographed in infrared. It's very bizarre when you're doing this with a ...

Utty says…

31 October 2007 – 4:00 pm

ure last name is like the sound ducks make lol or maybe like when u step on something and it squashes but i thas to be something that makes a sound that goes quach

Alternatives to study

29 October 2007 – 4:40 pm

Okay, so exams are looming. The first one ("Decision Support Systems Fundamentals") will begin in less than 24 hours. I'm not ready, but then again, I don't know anyone who ...

Teh blog

28 October 2007 – 3:09 am

Nothing to write here, just testing integration between Facebook and my self hosted blog which I nuked and re-installed today (as an excuse to not study).

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 :