Hands-on Tutorial for Getting Started with Amazon SimpleDB

Exclusive offer: get 50% off this eBook here
Amazon SimpleDB Developer Guide

Amazon SimpleDB Developer Guide — Save 50%

Scale your application's database on the cloud using Amazon SimpleDB with this book and eBook

$23.99    $12.00
by Prabhakar Chaganti Rich Helms | May 2010 | Enterprise Articles Java PHP Web Development

With Amazon SimpleDB you can scale your application's database on the cloud. In this article by Prabhakar Chaganti and Rich Helms, authors of Amazon SimpleDB Developer Guide, we going to sign up for an AWS account, download and install the necessary libraries, and create little code snippets for exploring SimpleDB. We will introduce the libraries as well as the SimpleDB Firefox plugin for manipulating SimpleDB. We will also examine the two methods for accessing SimpleDB: SOAP and ReST. For PHP users we will introduce the PHP sample library. You can install the samples on your PHP5 server so that you can try the samples as you read about them.

In this article we will:

  • Set up an AWS account
  • Enable SimpleDB service for the account
  • Install and set up libraries for Java, PHP, and Python
  • Illustrate several SimpleDB operations using these libraries

(For more resources on SimpleDB, see here.)

Creating an AWS account

In order to start using SimpleDB, you will first need to sign up for an account with AWS.

Visit http://aws.amazon.com/ and click on Create an AWS Account.

Amazon SimpleDB Developer Guide

You can sign up either by using your e-mail address for an existing Amazon account, or by creating a completely new account. You may wish to have multiple accounts to separate billing for projects. This could make it easier for you to track billing for separate accounts. After a successful signup, navigate to the main AWS page— http://aws.amazon.com/, and click on the Your Account link at any time to view your account information and make any changes to it if needed.

Amazon SimpleDB Developer Guide

Enabling SimpleDB service for AWS account

Once you have successfully set up an AWS account, you must follow these steps to enable the SimpleDB service for your account:

  1. Log in to your AWS account.
  2. Navigate to the SimpleDB home page—http://aws.amazon.com/simpledb/.
  3. Click on the Sign Up For Amazon SimpleDB button on the right side of the page.
  4. Provide the requested credit card information and complete the signup process.

    Amazon SimpleDB Developer Guide

You have now successfully set up your AWS account and enabled it for SimpleDB.

All communication with SimpleDB or any of the Amazon web services must be through either the SOAP interface or the Query/ReST interface. The request messages sent through either of these interfaces is digitally signed by the sending user in order to ensure that the messages have not been tampered within transit, and that they really originate from the sending user. Requests that use the Query/ReST interface will use the access keys for signing the request, whereas requests to the SOAP interface will use the x.509 certificates.

Amazon SimpleDB Developer Guide

Your new AWS account is associated with the following items:

  • A unique 12-digit AWS account number for identifying your account.
  • AWS Access Credentials are used for the purpose of authenticating requests made by you through the ReST Request API to any of the web services provided by AWS. An initial set of keys is automatically generated for you by default. You can regenerate the Secret Access Key at any time if you like. Keep in mind that when you generate a new access key, all requests made using the old key will be rejected.
    • An Access Key ID identifies you as the person making requests to a web service.
    • A Secret Access Key is used to calculate the digital signature when you make requests to the web service.
    • Be careful with your Secret Access Key, as it provides full access to the account, including the ability to delete all of your data.

    Amazon SimpleDB Developer Guide

  • All requests made to any of the web services provided by AWS using the SOAP protocol use the X.509 security certificate for authentication. There are no default certificates generated automatically for you by AWS. You must generate the certificate by clicking on the Create a new Certificate link, then download them to your computer and make them available to the machine that will be making requests to AWS.
    • Public and private key for the x.509 certificate. You can either upload your own x.509 certificate if you already have one, or you can just generate a new certificate and then download it to your computer.

    Amazon SimpleDB Developer Guide

Query API and authentication

There are two interfaces to SimpleDB. The SOAP interface uses the SOAP protocol for the messages, while the ReST Requests uses HTTP requests with request parameters to describe the various SimpleDB methods and operations. In this book, we will be focusing on using the ReST Requests for talking to SimpleDB, as it is a much simpler protocol and utilizes straightforward HTTP-based requests and responses for communication, and the requests are sent to SimpleDB using either a HTTP GET or POST method.

The ReST Requests need to be authenticated in order to establish that they are originating from a valid SimpleDB user, and also for accounting and billing purposes. This authentication is performed using your access key identifiers. Every request to SimpleDB must contain a request signature calculated by constructing a string based on the Query API and then calculating an RFC 2104-compliant HMAC-SHA1 hash, using the Secret Access Key.

The basic steps in the authentication of a request by SimpleDB are:

  • You construct a request to SimpleDB.
  • You use your Secret Access Key to calculate the request signature, a Keyed-Hashing for Message Authentication code (HMAC) with an SHA1 hash function.
  • You send the request data, the request signature, timestamp, and your Access Key ID to AWS.
  • AWS uses the Access Key ID in the request to look up the associated Secret Access Key.
  • AWS generates a request signature from the request data using the retrieved Secret Access Key and the same algorithm you used to calculate the signature in the request.
  • If the signature generated by AWS matches the one you sent in the request, the request is considered to be authentic. If the signatures are different, the request is discarded, and AWS returns an error response. If the timestamp is older than 15 minutes, the request is rejected.

The procedure for constructing your requests is simple, but tedious and time consuming. This overview was intended to make you familiar with the entire process, but don't worry—you will not need to go through this laborious process every single time that you interact with SimpleDB. Instead, we will be leveraging one of the available libraries for communicating with SimpleDB, which encapsulates a lot of the repetitive stuff for us and makes it simple to dive straight into playing with and exploring SimpleDB!

Amazon SimpleDB Developer Guide Scale your application's database on the cloud using Amazon SimpleDB with this book and eBook
Published: June 2010
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

(For more resources on SimpleDB, see here.)

SimpleDB libraries

There are libraries available for interacting with SimpleDB from a wide variety of languages. Most of these libraries provide support for all of the basic operations of SimpleDB. However, Amazon has been working hard to enhance and improve the functionality of SimpleDB, and as a result, they add new features frequently. You will want to leverage these new features as quickly as possible in your own applications. It is important that you select a library that has an active development cycle, so the new features are available fairly quickly after Amazon has released them. Another important consideration is the community around each library. An active community that uses the library ensures good quality and also provides a great way to get your questions answered. There are five libraries that meet all of these criteria:

  • Java Library for Amazon SimpleDB: This is the official Java library provided by Amazon. In our experience, this library is a bit too verbose and requires a lot of boilerplate code.
  • Typica: This is an open source Java library that provides access to all of the latest functionalities provided by SimpleDB. It is actively maintained and has a large community of users.
  • SDB-PHP and S3-PHP: SDB-PHP is an open source PHP library that provides an easy ReST-based interface to Amazon's SimpleDB service (http://sourceforge.net/projects/php-sdb/), and S3-PHP is an open source PHP library to access S3-PHP (http://undesigned.org.za/2007/10/22/amazon-s3-php-class).
  • RightAWS: An open source Ruby library for SimpleDB, which is quite popular with users who are building Ruby on Rails-based webapps that need SimpleDB functionality. It is actively maintained and has a large community of users.
  • Boto: An open source Python library for SimpleDB. This is a comprehensive library that provides access to all of the SimpleDB features.

These are all great libraries, and they will be useful to you if your application is written in one of these languages. We will include samples in three of the languages—Java, PHP, and Python.

SDBtool — Firefox plugin

SDBtool is a Firefox plugin by Bizo Engineering for manipulating SimpleDB. As you go through the sample code, you can then view the results in the database. This is invaluable in both viewing results as well as updating or deleting data.

The program is a Firefox web browser (http://www.mozilla.com/firefox/) plugin. One of Firefox's key features is the ability to install plugins to expand the capabilities. Firefox is available for Microsoft Windows and Apple Mac OS X as well as Linux.

To install SDBtool, visit http://code.google.com/p/SDBtool/ with a Firefox browser. Then click on the Click here to install link. Firefox will ask for a confirmation to install the plugin.

Amazon SimpleDB Developer Guide

To start SDBtool, click on Tools | SDB Tool in the top menu.

Amazon SimpleDB Developer Guide

When SDBtool starts for the first time, it is necessary to configure your Access Keys. Click on the Config button and enter your Access Key and Secret Key. There is also an checkbox that sets if the tool can delete a domain. If you are working on a production database, it is wise to leave this unchecked.

Amazon SimpleDB Developer Guide

A connection to your SimpleDB database will open in a new browser tab. The list of available domains will be listed in the domain area.

The SDBtool screen is divided into four areas:

Amazon SimpleDB Developer Guide

This area is used to create or display domains.

Amazon SimpleDB Developer Guide

This area is used to write SQL queries. You can use the simple select * from domain_name to view all items in a domain.

Amazon SimpleDB Developer Guide

This area is used to display SQL query results.

Amazon SimpleDB Developer Guide

This area is used to add or replace attribute values.

Sample outline — performing basic operations

In this sample, the sample goals, as well as common SimpleDB principles, will be introduced. The sample will break into three streams: Java, PHP, and Python.

The purpose of this sample is to introduce code snippets to create, list, and delete domains as well as create, query, and delete items.

Each domain is a container for storing items. Any item that does not have any attributes is considered empty and is automatically deleted by SimpleDB. You can therefore have empty domains stored in SimpleDB, but not items with zero attributes. Each value is stored as a UTF-8 string in SimpleDB. This is an important consideration, and you need to be aware of it when storing and querying different data types, such as numbers or dates. You must convert their data into an appropriate string format, so that your queries against the data return expected results. The conversion of data adds a little bit of extra work on your application side, but it also provides you with the flexibility to enforce data ReSTrictions at your application layer without the need for the data store to enforce the constraints.

Basic operations with Java

Java is a very popular language used for building enterprise applications. In this section we will download typica and then use it for exploring SimpleDB.

Exploring SimpleDB with Java

Download typica from the project site at Google Code—http://code.google.com/p/typica/.

The latest version of typica at the time of writing this is 1.6. Download the ZIP file from the website. Unzip to the folder of your choice and add the typica.jar to your classpath. You also need some third-party libraries used by typica and can download each of these dependencies and add the corresponding JAR files to your classpath:

We are going to learn about and explore SimpleDB from Java by writing small snippets of Java code for interacting with it. Here is the skeleton of a Java class named ExploreSdb that contains a main method. We will add code to the main method, and you can run the class to see it in action from the console or in the IDE of your choice.

package simpledbbook;
public class ExploreSdb {
public static void main(String[] args) {
SimpleDB sdb = new SimpleDB(awsAccessId, awsSecretKey, true);
}
}

In this class, we create a SimpleDB object from the class provided by typica. This will be our connection to Amazon SimpleDB and will be used for interacting with it. As we have discussed earlier, in order to use the API, we will need to specify the AWS keys. Typica lets you store the keys in a file named aws.properties, or you can explicitly provide them when you are creating a connection. In this chapter, we will use the explicit way. In each of the sections below, we will add snippets of code to the main() method.

Creating a domain with Java

A SimpleDB domain is a container for storing data, and is similar in concept to a table in a relational database. You create a domain by calling the createDomain() method and specifying a name for the domain.

public static void main(String[] args) {
SimpleDB sdb = new SimpleDB(awsAccessId, awsSecretKey, true);
try {
Domain domain = sdb.createDomain("cars");
} catch (SDBException ex) {
System.out.println(ex.getMessage());
}
}

Listing domains with Java

You can display a list of domains by calling the listDomains() method. This will return a list of domain objects.

public static void main(String[] args) {
SimpleDB sdb = new SimpleDB(awsAccessId, awsSecretKey, true);
try {
ListDomainsResult domainsResult = sdb.listDomains();
List<Domain> domains = domainsResult.getDomainList();
for (Domain dom : domains) {
System.out.println("Domain : " + dom.getName());
}
} catch (SDBException ex) {
System.out.println(ex.getMessage());
}
}

Manipulating items with Java

We will now add some items to our newly-created domain. You create an item and then specify its attributes as follows:

public static void main(String[] args) {
SimpleDB sdb = new SimpleDB(awsAccessId, awsSecretKey, true);
try {
Domain domain = sdb.getDomain("cars");
Item item = domain.getItem("car1");
List<ItemAttribute> list = new ArrayList<ItemAttribute>();
list.add(new ItemAttribute("make", "BMW", false));
list.add(new ItemAttribute("color", "grey", false));
list.add(new ItemAttribute("year", "2008", false));
list.add(new ItemAttribute("desc", "Sedan", false));
list.add(new ItemAttribute("model", "530i", false));
item.putAttributes(list);

item = domain.getItem("car2");
list = new ArrayList<ItemAttribute>();
list.add(new ItemAttribute("make", "BMW", false));
list.add(new ItemAttribute("color", "white", false));
list.add(new ItemAttribute("year", "2007", false));
list.add(new ItemAttribute("desc", "Sports Utility Vehicle",
false));
list.add(new ItemAttribute("model", "X5", false));
item.putAttributes(list);
} catch (SDBException ex) {
System.out.println(ex.getMessage());
}
}

Now retrieve the items from the domain by using a simple SELECT query.

public static void main(String[] args) {
SimpleDB sdb = new SimpleDB(awsAccessId, awsSecretKey, true);
try {
Domain domain = sdb.getDomain("cars");
String queryString = "SELECT * FROM `cars`";
int itemCount = 0;
String nextToken = null;
do {
QueryWithAttributesResult queryResults =
domain.selectItems(queryString, nextToken);
Map<String, List<ItemAttribute>> items =
queryResults.getItems();
for (String id : items.keySet()) {
System.out.println("Item : " + id);
for (ItemAttribute attr : items.get(id)) {
System.out.println(attr.getName() + " = "
+ attr.getValue());
}
itemCount++;
}
nextToken = queryResults.getNextToken();
} while (nextToken != null && !nextToken
.trim().equals(""));
} catch (SDBException ex) {
System.out.println(ex.getMessage());
}
}

You can retrieve an individual item and its attributes by specifying the item name.

public static void main(String[] args) {
SimpleDB sdb = new SimpleDB(awsAccessId, awsSecretKey, true);
try {
Domain domain = sdb.getDomain("cars");
Item car1 = domain.getItem("car1");
List<ItemAttribute> itemAttrs = car1.getAttributes();
for (ItemAttribute attr : itemAttrs) {
System.out.println(attr.getName()
+ " = " + attr.getValue());
}
} catch (SDBException ex) {
System.out.println(ex.getMessage());
}
}

Deleting a domain with Java

Finally, you can delete a domain by specifying its name. Once you delete a domain, the data is gone forever. So use caution when deleting a domain!

public static void main(String[] args) {
SimpleDB sdb = new SimpleDB(awsAccessId, awsSecretKey, true);
try {
sdb.deleteDomain("cars");
} catch (SDBException ex) {
System.out.println(ex.getMessage());
}
}

Amazon SimpleDB Developer Guide Scale your application's database on the cloud using Amazon SimpleDB with this book and eBook
Published: June 2010
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

(For more resources on SimpleDB, see here.)

Basic operations with PHP

PHP is a popular scripting language for writing web applications. Many of the most popular open source applications such as WordPress are written with PHP. There are several SimpleDB APIs available. The PHP samples are based on an API written by Dan Myers. This program is easy to understand, use, and expand. Rich Helms has expanded the API and provided samples.

All of the sample code can be downloaded and run from your site, or executed from our site, with your Access Keys to your SimpleDB. Note that the user interface in these samples is very basic. The focus is on illustrating the SimpleDB interface.

Exploring SimpleDB with PHP

Rich Helms: "When I wrote my first SimpleDB PHP program, I struggled to find a working sample to build on. After a number of APIs, I found Dan Myers' SDB-PHP. The entire API was in one file and simple to understand. Working with Dan, I expanded the API to provide complete SimpleDB functionality including data normalization. When I needed a PHP S3 API for backup/ReSTore of SimpleDB, I used S3-PHP by Donovan Schonknecht. SDB-PHP was based on S3-PHP".

Visit http://www.webmasterinresidence.ca/simpledb/ to download the PHP sample package, which has the sample programs discussed in this article. All programs are complete and can be run unaltered on your server. The samples use PHP 5.

The menu (index.php) provides access to all samples. When a program is run, the source is shown below in a box. If the free package Generic Syntax Highlighter (GeSHi) is installed, the source will be formatted when displayed. To get GeSHi, go to http://qbnz.com/highlighter/.

Amazon SimpleDB Developer Guide

The samples are structured into groups: entering your Access Keys, domains, items, and attributes; data normalization, select, and S3; and backing up SimpleDB into S3. As you go through the programs, use the Firefox SDBtool plugin to examine the database and see the results.

Select the first menu item to set the keys.

Amazon SimpleDB Developer Guide

The Key and Secret Key values are stored in two session variables. Program config.inc.php reads the session variables to set two defined keys: awsAccessKey and awsSecretKey. If you are downloading and running the source from your site, you can just define the keys and avoid the session variables. Using session variables enables you to try the code at my location and still talk to your SimpleDB without me having access to your keys.

require_once('config.inc.php');
if (!empty($_POST["key"])) {
$_SESSION['key'] = $_POST["key"];
}
if (!empty($_POST["secretkey"])) {
$_SESSION['secretkey'] = $_POST["secretkey"];
}

The program calls itself passing the value of the input fields.

session_start();
if (!empty($_SESSION['key'])) $key = $_SESSION['key'];
if (!empty($_SESSION['secretkey'])) $secretkey =
$_SESSION['secretkey'];
// if your own installation, just replace $key and $secretkey
// with your values
if (!defined('awsAccessKey')) define('awsAccessKey', $key);
if (!defined('awsSecretKey')) define('awsSecretKey', $secretkey);

When the second menu item is selected, the session variables are destroyed.

require_once('config.inc.php');
session_destroy();

To access SimpleDB, first we create a connection. File sdb.php has the API functions.

require_once('config.inc.php');
if (!class_exists('SimpleDB')) require_once('sdb.php');
$sdb = new SimpleDB(awsAccessKey, awsSecretKey);

Creating a domain with PHP

A SimpleDB domain is a container for storing data, and is similar in concept to a table in a relational database. Once the connection to SimpleDB is made, to create a domain, call the createDomain function passing the domain name.

$domain = "cars";
$sdb->createDomain($domain);

Listing domains with PHP

To display the domains, make a connection, and then call listDomains. The function returns an array of values. Retrieving a list of all our domains will return an array of domain names. In addition to the domain name ($domainname), the function domainMetadata is called to return the information on the domain, such as when the domain was created, the number of items and attributes, and the sizes of attribute names and values.

$domainList = $sdb->listDomains();
if ($domainList) {
foreach ($domainList as $domainName) {
echo "Domain: <b>$domainName</b><br>";
print_r($sdb->domainMetadata($domainName));
}
}

Manipulating items with PHP

This sample creates two items in the most basic manner of one at a time. This sample also only deals with a single value for each attribute. Once the connection is made, three variables are prepared. The item name ($item_name) is the primary key of the item. An array is built with attribute names and values. Then the three variables are passed to the putAttributes function.

$domain = "cars";
$item_name = "car1";
$putAttributesRequest["make"] = array("value" => "BMW");
$putAttributesRequest["color"] = array("value" => "grey");
$putAttributesRequest["year"] = array("value" => "2008");
$putAttributesRequest["desc"] = array("value" => "Sedan");
$putAttributesRequest["model"] = array("value" => "530i");

$sdb->putAttributes($domain,$item_name,$putAttributesRequest);
$item_name = "car2";
$putAttributesRequest["make"] = array("value" => "BMW");
$putAttributesRequest["color"] = array("value" => "white");
$putAttributesRequest["year"] = array("value" => "2007");
$putAttributesRequest["desc"] = array("value" =>
"Sports Utility Vehicle");
$putAttributesRequest["model"] = array("value" => "X5");

$sdb->putAttributes($domain,$item_name,$putAttributesRequest);

You can query for items within a domain by specifying the attribute and value to match. The SELECT syntax is a new addition to SimpleDB and allows searching your domains using simple SQL-like query expressions. The previous version of SimpleDB supported only a query-style syntax that is now being deprecated in favor of the simpler and easier-to-use SELECT expressions. Now that the items are created in the cars domain, we can list them as follows:

$domain = "cars";
print_r($sdb->select($domain,"select * from $domain"));

To retrieve a select item by the attribute value, you can use the following:

$domain = "cars";
$item_make = "BMW";
print_r($sdb->select($domain,"select * from $domain where
make='$item_make'"));

To retrieve a specific item by the item name, you can use the following:

$domain = "cars";
$item_name = "car1";
print_r($sdb->getAttributes($domain,$item_name));

To delete a specific item, you can use the following:

$domain = "cars";
$item_name = "car1";
$sdb->deleteAttributes($domain,$item_name);

To delete specific attribute values, but leave the item, use the following lines of code:

Note: if all attributes are deleted, then the item is deleted.

$domain = "cars";
$item_name = "car2";
$deleteAttributesRequest = array("make", "color", "year",
"desc", "model");
$deleteAttributesRequest["desc"] = "Sports Utility Vehicle";
$deleteAttributesRequest["model"] = "X5";
$sdb->deleteAttributes($domain,$item_name,$deleteAttributesRequest);

This code deletes the desc and model attributes from the car2 item.

Deleting a domain with PHP

Finally, you delete a domain by specifying its name. Once you delete a domain, the data is gone forever. So use caution when deleting a domain!

$domain = "cars";
$sdb->deleteDomain($domain);

Basic operations with Python

Prabhakar Chaganti: "My personal preference is for the Python library—boto, which has a very nicely designed interface and a great community of users".

Python is an elegant, open source, object-oriented programming language that is great for rapid application development. Python is a stable, mature language that has been around for quite a long period of time, and is widely used across many of the industries and in a large variety of applications. It comes with an interactive console that can be used for quick evaluation of code snippets and makes experimentation with new APIs very easy. Python is a dynamically-typed language that gives you the power to program in a compact and concise manner. There is no such verbosity that is associated with a statically-typed language such as Java. It will be much easier to grasp the concepts of SimpleDB without drowning in a lot of lines of repetitive code. Most importantly, Python will bring fun back into your programming!

Introducing boto

Boto is an open source Python library for communicating with all of the Amazon web services, including SimpleDB. It was originally conceived by Mitch Garnaat and is currently maintained and enhanced by him and a community of developers. It is by far Prabhakar's favorite library for interacting with AWS, and is very easy to use. Boto works with most recent versions of Python, but please make sure that you are using at least a 2.5.x version of Python. Do not use Python 3.x, as boto will not currently work with it. All versions of Linux usually ship with Python, but if you are running on Windows or Mac OS X, you will need to download and install a version of Python for your platform from http://python.org/download/. There are installers available for Windows and Mac OS X, and the installation process is as simple as downloading the correct file and then double-clicking on the file. If you have Python already installed, you can easily verify the version from a terminal window.

$ python


Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12)

[GCC 4.0.1 (Apple Inc. build 5465)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>>

You will need a copy of the setuptools package before you can install boto. Download the latest version for your platform from the setuptools page—http://pypi.python.org/pypi/setuptools. If you are on Windows, just run the downloaded EXE file. If you are running on Linux, use your existing package manager to install it. For instance, on Ubuntu, you can install setuptools using the apt package manager.

$ sudo apt-get install python-setuptools

Download boto from the project page at http://code.google.com/p/boto/. The latest version at the time of writing this article is boto-1.8d, and is provided as a g-zipped distribution that needs to be un-archived after download.

$ tar -zxvf boto-1.8d.tar.gz

This will create a folder named boto-1.8d and un-archive all the files. Now change into this new folder and run the install script to install boto.

$ sudo python setup.py install

This will byte-compile and install boto into your system. Before you use boto, you must set up your environment so that boto can find your AWS Access key identifiers. You can get your Access Keys from your AWS account page. Set up two environment variables to point to each of the keys.

$ export AWS_ACCESS_KEY_ID=Your_AWS_Access_Key_ID

$ export AWS_SECRET_ACCESS_KEY=Your_AWS_Secret_Access_Key

You can now check if boto is correctly installed and available by using the Python Interpreter and importing the library. If you don't have any errors, then you have boto installed correctly.

$ python

Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12)

[GCC 4.0.1 (Apple Inc. build 5465)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>>

>>> import boto

Exploring SimpleDB with Python

We will now use the installed and configured boto library to run some basic operations in SimpleDB using the Python console. This will quickly get you familiar with both boto and various SimpleDB operations. Boto will use the environment variable for the Access Keys that we set up in the previous section for connecting to SimpleDB.

We first create a connection to SimpleDB.

>>> import boto

>>> sdb_connection = boto.connect_sdb()

>>>

Boto is using the AWS Access Keys we previously set up in the environmental variables in this case. You can also explicitly specify the Access Keys on creation.

>>> import boto

>>> sdb_connection = boto.connect_sdb(access_key,secret_key)

>>>

Creating a domain with Python

A SimpleDB domain is a container for storing data, and is similar in concept to a table in a relational database. A new domain can be created by specifying a name for the domain.

>>> domain1 = sdb_connection.create_domain('prabhakar-dom-1')

Retrieving a domain with Python

Retrieving a list of all our domains will return a Python result set object that can be iterated over in order to access each domain.

>>> all_domains = sdb_connection.get_all_domains()

>>>

>>> len(all_domains)

1

>>>

>>> for d in all_domains:

... print d.name

...

prabhakar-dom-1

You can also retrieve a single domain by specifying its name.

>>> my_domain = sdb_connection.get_domain('prabhakar-dom-1')

>>>

>>> print my_domain.name

prabhakar-dom-1

Creating items with Python

You can create a new item by specifying the attributes for the item along with the name of the item to be created.

>>>

>>> my_domain.put_attributes('car1', {'make':'BMW',
'color':'grey','year':'2008','desc':'Sedan','model':'530i'})

>>>

>>> my_domain.put_attributes('car2', {'make':'BMW', 'color':
'white','year':'2007','desc':' Sports Utility Vehicle','model':'X5'})

>>>

Items stored within a domain can be retrieved by specifying the item name. The name of an item must be unique and is similar to the concept of a primary key in a relational database. The uniqueness of the item name within a domain will cause your existing item attributes to be overwritten with the new values if you try to store new attributes with the same item name.

>>> my_item = my_domain.get_item('car1')

>>>

>>> print my_item

{u'color': u'grey', u'model': u'530i', u'desc': u'Sedan',
u'make': u'BMW', u'year': u'2008'}

>>>

You can query for items within a domain by specifying the attribute and value to match. The SELECT syntax is a new addition to SimpleDB and allows searching your domains using simple SQL-like query expressions. The previous version of SimpleDB only supported a query-style syntax that is now being deprecated in favor of the simpler and easier-to-use SELECT expressions.

>>> rs = my_domain.select("SELECT name FROM `prabhakar-dom-1`
WHERE make='BMW'")

>>> for result in rs:

... print result.name

...

car1

car2

>>>

Multiple attributes can also be specified as a part of the query.

>>> rs = my_domain.select("SELECT name FROM `prabhakar-dom-1`
WHERE make='BMW' AND model='X5'")

>>> for result in rs:

... print result.name

...

car2

>>>

You can delete a specific item and all of its attributes from a domain.

>>> sdb_connection.get_attributes('prabhakar-dom-1','car1')
{u'color': u'grey', u'model': u'530i', u'desc': u'Sedan',
u'make': u'BMW', u'year': u'2008'}

>>>

>>> sdb_connection.delete_attributes('prabhakar-dom-1','car1')

True

>>> sdb_connection.get_attributes('prabhakar-dom-1',car1')

{}

>>>

Finally, you delete a domain by specifying its name. Once you delete a domain, the data is gone forever. So use caution when deleting a domain!

>>> sdb_connection.delete_domain('prabhakar-dom-1')

True

>>>

Summary

In this article, we set up an AWS account, enabled SimpleDB service for the account, and installed and set up libraries for Java, PHP, and Python. We explored several SimpleDB operations using these libraries. In the next article, we will examine the differences between SimpleDB and the relational database model in detail.


Further resources on this subject:


About the Author :


Prabhakar Chaganti

Prabhakar Chaganti is the founder and CTO of Ylastic, a start-up that is building a single unified interface to architect, manage, and monitor a user's entire AWS Cloud computing environment: EC2, S3, RDS, AutoScaling, ELB, Cloudwatch, SQS, and SimpleDB. He is the author of Xen Virtualization and GWT Java AJAX Programming, and is also the winner of the community choice award for the most innovative virtual appliance in the VMware Global Virtual Appliance Challenge. He hangs out on Twitter as @pchaganti.

Read about his tips on time management...

Rich Helms

"It's never been done" is a call to action for Rich Helms. He has built a career on breaking new ground in the computer field. He developed CARES (Computer Assisted Recovery Enhancement System) for the Metropolitan Toronto Police in Canada. CARES was the first computer system in the world for aging missing children. CARES has been internationally recognized as pioneering work in child aging. Rich has also created several generations of e-Learning platforms including Learn it Script and most recently Educate Press.

Rich is a seasoned software developer with over 30 years of experience. He spent 22 years in various positions at IBM including Chief Image Technology Architect. His credentials range from deep technical work (five patents in hardware and software) to running multinational R&D.

Books From Packt


Python 3 Object Oriented Programming
Python 3 Object Oriented Programming

Oracle SQL Developer 2.1
Oracle SQL Developer 2.1

Object-Oriented Programming with PHP5
Object-Oriented Programming with PHP5

Microsoft Silverlight 4 Data and Services Cookbook
Microsoft Silverlight 4 Data and Services Cookbook

Microsoft Dynamics GP 2010 Cookbook
Microsoft Dynamics GP 2010 Cookbook

IBM WebSphere eXtreme Scale 6
IBM WebSphere eXtreme Scale 6

JDBC 4.0 and Oracle JDeveloper for J2EE Development
JDBC 4.0 and Oracle JDeveloper for J2EE Development

Oracle Database 11g – Underground Advice for Database Administrators
Oracle Database 11g – Underground Advice for Database Administrators


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software