What’s Amazon DynamoDB?
DynamoDB is one of the most recent services offered by Amazon.com. Announced on January 18, 2012, it is a fully managed NoSQL database service that provides fast and predictable performance along with excellent scalability. Let’s quickly analyze its positive and negative aspects in the lists below:
- Hosted by Amazon
- Good SDK
- Free account for small amount of reads/writes
- Pricing based on throughput
- Poor documentation
- Limited data types
- Poor query comparison operators
- Unable to do complex queries
- 64KB limit on row size
- 1MB limit on querying
Setup and simple usage
Below I will provide a step-by-step guide to setup and start using DynamoDB
This tutorial uses the following environment:
- Mac OS 10.7.3
- Aptana 3.0.8
- PHP 5.3.8
- AWS sdk 1.5.3
0) Sign up for an Amazon AWS account
Go to www.aws.amazon.com and signup for a free AWS account. With a free account you can obtain DynamoDB with up to 5 reads / sec and 5 writes / sec.
1) Install AWS sdk for php
Assuming you have pear already installed in your system enter the following commands (for more information on pear, see http://pear.php.net/):
pear channel-discover pear.amazonwebservices.com pear install aws/sdk
Now some configuration files need to be modified in order to connect to your DynamoDB:
Go to your AWS management console (www.console.aws.amazon.com), click on your user name (top-right corner) and select “Security credentials”. Here you’ll find your Access Key Id and your Secret Access Key. Insert these credentials in the following file:
and save the file with the name “config.inc.php”
sudo mv ~/pear/share/pear/AWSSDKforPHP/sample-config.inc.php ~/pear/share/pear/AWSSDKforPHP/config.inc.php
Once the sdk is installed and configured, you can use its APIs by including it with the following command:
and establish a connection with your DynamoDB with:
$dynamodb = new AmazonDynamoDB();
2) Install AWS Toolkit for Eclipse/Aptana
With the AWS Toolkit for Eclipse/Aptana, it’s possible to manage your DynamoDB tables without having to login to the AWS management console.
To install it, follow the next steps:
- Start Eclipse/Aptana
- Open Help -> Install New Software….
- Enter http://aws.amazon.com/eclipse in the text box labeled “Work with” at the top of the dialog.
- Select “AWS Toolkit for Eclipse” from the list below.
- Click “Next”. Eclipse/Aptana guides you through the remaining installation steps.
- Restart Eclipse/Aptana;
- Go to preferences -> AWS Toolkit and insert your account’s credentials*.
3) Start using DynamoDB
Below I provide some instructions to start using DynamoDB through the AWS SDK. Starting from creating a new table, loading data into it and perform a simple query.
3.1) Create Table
To create a table, go to the AWS explorer in Eclipse/Aptana, right click on Amazon DynamoDB and click on “Create Table”. Define the Table name, the key attribute and value and define (if needed) the range key attribute and value. This last parameter represents a secondary key to filter data.
Let’s assume that you want to create a table called “Websites” that has two columns: id (key)(String) and URL (String)
3.2) Load data
To insert one row in your new table use the following function:
$dynamodb = new AmazonDynamoDB(); $generatedId = uniqid(); $websiteUrl = ‘http://www.grio.com’; $put_response = $dynamodb->put_item(array( 'TableName' => “Websites”, 'Item' => array( 'id' => array( AmazonDynamoDB::TYPE_STRING => $generatedId ), 'URL' => array( AmazonDynamoDB::TYPE_STRING => $websiteUrl ) ) ));
3.3) Query data
To query DynamoDB two methods can be used. The first one is called “query” and helps you to find the row identified by the provided key.
$dynamodb = new AmazonDynamoDB(); $grioId = “4f605c68e8c14” $response = $dynamodb->query(array( 'TableName' => “Websites”, 'HashKeyValue' => array( AmazonDynamoDB::TYPE_STRING => $grioId ), ));
This will return the URL corresponding to the generated unique id (key) in the table “Webistes”.
The second method for querying DynamoDB is called “scan” and allows you to get the all table and filter it using more than one field.
$dynamodb = new AmazonDynamoDB(); $scan_response = $dynamodb->scan(array('TableName' => 'ProductCatalog', 'AttributesToGet' => array('Id'), 'ScanFilter' => array('Price' => array( 'ComparisonOperator' => AmazonDynamoDB::CONDITION_LESS_THAN, 'AttributeValueList' => array( array( AmazonDynamoDB::TYPE_NUMBER => '100' )) )) ));
Suppose you have a table called ProductCatalog that contains the ids and the prices of all the products.
The response of the above request will contain all the ids of Products with a Price lower than 100$.
“Scan” allows you to perform “more complex” queries but “query” guarantees better performances.
3.4) Parse response
The results of the above queries will be an Object of the type ECFResponse whose most important part is the body (that is a CFSimpleXML Object). To access the number of rows returned by the query it’s sufficient to look at the Count attribute:
To get a specific field (e.g. URL) of the nth element of the response it’s necessary to access the Items attribute in the following way:
Where the cast ( “(string)” ) and the last letter “S”, depend on the type of the field ( in this case URL it’s an AmazonDynamoDB::TYPE_STRING as specified in 3.2).
Now you have all you need to start using DynamoDB for your application.
Summarizing, Amazon DynamoDB is a useful service if you need a simple, plug-and-play, scalable db. It becomes less handy if scalability is not your first concern or if your application needs to perform very complex queries. This said, we’re still talking of a very new product, which is going to improve over time.