Introduction
Amazon SimpleDB is a Web Service provided by Amazon which allows you to store data on their servers like you would in a database. For more details, you can go to the link here.
Amazon SimpleDB is a cut-down database system, which has the structure of a database but is more simple, and is similar to a spreadsheet. First, you need to know some concepts about mapping SimpleDB to a normal database system:
Amazon Simple DB |
Normal Database System |
Domain |
Table |
Attribute |
Column |
Item |
Row |
However, in Amazon SimpleDB, there is no relationship, and no data type. This mean that a query to a database can only access a domain and all values returned are of the text type. The domain is quite freely structured. It is made up of a number of items (which you can see as being similar to rows in a worksheet). Each item has a number of attributes (which you can see as being similar to columns). There is no requirement for each row to have the same set of attributes - in relational database terms, it is highly denormalised. To develop an application using Amazon SimpleDB with C#, you need the Amazon Web Service SDK. To manage Amazon SimpleDB, you can use the Mindscape SimpleDB Management Tools.
Services
Create an Amazon SimpleDB object
Now let's start with Amazon SimpleDB. With an Amazon SimpleDB account, access key and private key, we can create an AmazonSimpleDB
object. This object controls all the actions to interact with an Amazon SimpleDB server.
public static String TableName = "DemoTable";
public static String Column1 = "Column 1";
public static String Column2 = "Column 2";
public static String Column3 = "Column 3";
const string AWS_ACCESS_KEY = "put_your_AWS_access_key_here";
const string AWS_SECRET_KEY = "put_your_AWS_secret_key _here";
AmazonSimpleDB client =
new AmazonSimpleDB(AWS_ACCESS_KEY, AWS_SECRET_KEY)
Normally, two keys are stored in the application config:
// In application config
<appSettings>
<add key="AWSAccessKey" value="put_your_AWS_access_key_here"/>
<add key="AWSSecretKey" value="put_your_AWS_secret_key _here"/>
<appSettings>
and we get it from the code like:
public static AmazonSimpleDB GetSimpleDBClient()
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
AmazonSimpleDB simpleDBClient = AWSClientFactory.CreateAmazonSimpleDBClient(
appConfig["AWSAccessKey"],
appConfig["AWSSecretKey"]
);
return simpleDBClient;
}
Create a domain
As mentioned above, in Amazon SimpleDB, we use a domain as a table. And you can create a domain like this:
boolean found = false;
String TableName = "DemoTable";
ListDomainsResponse response =
simpleDBClient.ListDomains(new ListDomainsRequest());
foreach (string domain in response.ListDomainsResult.DomainName)
{
if(domain == TableName) found = true;
}
if(!found)
{
simpleDBClient.CreateDomain(
new CreateDomainRequest() { DomainName = TableName });
}
After creating, you will see it in Management Tools:
When a domain is created, it doesn't have any attributes or items. When we open a domain by using Management Tools, we will see only one column ID. This column is not an attribute, it is the default in Amazon SimpleDB and stores the name of each item in the domain.
After that, we will add a new attribute and new item for the domain. Remember that in SimpleDB, the domain is quite freely structured. It is made up of a number of items (which you can see as being similar to rows in a worksheet). Each item has a number of attributes (which you can see as being similar to columns). There is no requirement for each row to have the same set of attributes - in relational database terms. So, you can not add an attribute without any data. When you want to add a new attribute, you add it by adding a new item having data in that column:
String Column1 = "Column 1";
String Column2 = "Column 2";
String Column3 = "Column 3";
ReplaceableAttribute replaceAttribute1 =
new ReplaceableAttribute() { Name = Column1, Replace= true, Value = "Value 1"};
ReplaceableAttribute replaceAttribute3 =
new ReplaceableAttribute() { Name = Column2, Replace = true, Value = "Value 2" };
ReplaceableAttribute replaceAttribute2 =
new ReplaceableAttribute() { Name = Column3, Replace = true, Value = "Value 3" };
List<ReplaceableAttribute> listReplaceAttribute =
new List<ReplaceableAttribute>() { replaceAttribute1,
replaceAttribute2, replaceAttribute3 };
simpleDBClient.PutAttributes(new PutAttributesRequest() {
Attribute = listReplaceAttribute, DomainName = TableName, ItemName = "DemoItem" });
Get data from SimpleDB
To get data from Amazon Simple DB, there are two methods:
- Get by item name: Item name (data stored in the column ID of the domain) is a unique key we set when creating an item, so we can use it to get the item.
GetAttributesResponse response = simpleDBClient.GetAttributes(
new GetAttributesRequest() { DomainName = TableName, ItemName = "DemoItem" });
String res = "Item: DemoItem has: ";
foreach (Amazon.SimpleDB.Model.Attribute attribute
in response.GetAttributesResult.Attribute)
{
res += "{" + attribute.Name + ", " + attribute.Value + "}, ";
}
res = res.Remove(res.Length - 2);
Console.Out.WriteLine(res);
- Get by query:
Amazon SimpleDB supports using queries to get data. The query structure is similar to other database queries, and is easy to build and understand. This is the most popular way of getting data because it is very flexible.
SelectResponse response = simpleDBClient.Select(new SelectRequest() {
SelectExpression = "Select * from " + TableName
});
String res = TableName + " has: ";
Console.Out.WriteLine(res);
foreach (Item item in response.SelectResult.Item)
{
res = item.Name + ": ";
foreach (Amazon.SimpleDB.Model.Attribute attribute in item.Attribute)
{
res += "{" + attribute.Name + ", " + attribute.Value + "}, ";
}
res = res.Remove(res.Length - 2);
Console.Out.WriteLine(res);
}
When getting data from SimpleDB, we need to remember four things:
- Amazon SimpleDB does not have relationships, so a query can only access one domain, and can not join.
- All attribute value returns have the type string.
- In SimpleDB, there is no requirement for each row to have the same set of attributes. So when you get an item that does not have value for an attribute, you might think that in the result item, that attribute will have a blank value or null, but that is wrong. The result item will not have that attribute in the set of attributes and we cannot get that value.
- When you use "get data by query", the column ID cannot be used to filter, sort... because the ID column is a unique column that refers to the name of the item and it does not exist in the attribute of the item.
Delete item in domain
When you want to delete an item, you need the exact name of the item.
simpleDBClient.DeleteAttributes(new DeleteAttributesRequest() {
DomainName = TableName,
ItemName = "DemoItem"
});
Conclusion
Amazon SimpleDB service is a good service for storing simple databases. Although it is simple, the provided functions are enough for a small database, and in many cases, it will be the best choice for storing online databases. Try to understand it and work with it, and you will find something so wonderful.
History
- 24 Apr., 2011: Initial post.