Amazon Web Services and SimpleDB

Recently finished a simple Java Servlet using Amazon Web Services which required a simple data persistence layer.  The natural choice was SimpleDB.  And I found some gotchas…

First of all – a thumbnail sketch of SimpleDB.  It’s a schemaless data structure consisting of hash tables with sets of key value pairs.  A nice write-up is here.  The AWS SimpleDB site is here.

My typical dev process was build and test local, then move up to AWS.  I’m using Java version jdk1.6; Apache Ant 1.8.1; Jetty 8.0.1x.

Start by downloading the AWS Java SDK .  Amazon provides a good resource of Java programs providing examples of using their various cloud products – the two I used as reference for this project was in the samples\AmazonSimpleDB and samples\AwsConsoleApp directories.

There were four issues I ran into before being able to successfully use the product:

1.  Connect to SimpleDB thru the supplied AWSCredentials API.

2. Adding the AWS jar file libraries to the ant build.xml file.

3. Add the AWS jar files to Jetty for development and testing.

4. Make sure the AWS instance had access to the jar files.

First issue, connection using a credential set.  You create your credentials when you set up your Amazon Web Services account.  And a blank credential file is provided in the SDK demo programs.  Since this is a servlet program, the code resides as child directories of a WEB-INF folder.  I placed my credentials file into the WEB-INF\resources folder.

Next thing is to bring the credential file into the various methods that need access to the SimpleDB.  First I defined a String variable containing the path to the credential file:

String filename = “/WEB-INF/resources/AwsCredentials.properties”;

The file is brought in as a stream:

InputStream is = context.getResourceAsStream(filename);

An AWS credentials object is set using the data from the stream:

AWSCredentials credentials = new PropertiesCredentials(is);

And finally an AWS SimpleDB object is created, using the AWS credentials.  This SimpleDB object is what you use to connect to your SimpleDB domain:

AmazonSimpleDB sdb = new AmazonSimpleDBClient(credentials);

I’ll cover the other issues in future posts.