Amazon SimpleDB 是一种简单且灵活的数据库服务,它允许用户在Amazon的服务器上存储数据,类似于数据库和电子表格的结合体。本文将介绍如何将SimpleDB映射到传统的数据库系统中,并展示如何使用C#语言与之交互。
在Amazon SimpleDB中,与传统数据库系统相比,有以下概念对应关系:
要使用Amazon SimpleDB,首先需要Amazon Web Service SDK。此外,可以使用Mindscape SimpleDB Management Tools来管理SimpleDB。
要开始使用Amazon SimpleDB,首先需要创建一个AmazonSimpleDB对象。这需要一个Amazon SimpleDB账户、访问密钥和私有密钥。以下是一个C#示例代码,演示如何创建该对象:
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);
通常,这两个密钥会存储在应用程序配置中,如下所示:
// 在应用程序配置中
<appSettings>
<add key="AWSAccessKey" value="put_your_AWS_access_key_here" />
<add key="AWSSecretKey" value="put_your_AWS_secret_key_here" />
</appSettings>
然后可以通过以下代码从配置中获取这些密钥:
public static AmazonSimpleDB GetSimpleDBClient()
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
AmazonSimpleDB simpleDBClient = AWSClientFactory.CreateAmazonSimpleDBClient(
appConfig["AWSAccessKey"],
appConfig["AWSSecretKey"]
);
return simpleDBClient;
}
创建域(Domain)类似于创建表。以下是C#代码示例,展示如何创建一个域:
bool 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 });
}
创建域后,可以在管理工具中看到它。创建的域初始没有任何属性或项目。使用管理工具打开域时,会看到一个默认的ID列,该列不是属性,它存储域中每个项目的名称。
接下来,将为域添加新的属性和项目。在SimpleDB中,域的结构相对自由,由多个项目组成,每个项目有多个属性。以下是添加属性和项目的C#代码示例:
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" });
从SimpleDB获取数据有两种方法:通过项目名称获取和通过查询获取。以下是C#代码示例,展示如何通过项目名称获取数据:
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);
以下是通过查询获取数据的C#代码示例:
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);
}
从SimpleDB获取数据时需要注意以下几点:
当需要删除项目时,需要确切的项目名称。以下是C#代码示例,展示如何删除域中的项目:
simpleDBClient.DeleteAttributes(new DeleteAttributesRequest() { DomainName = TableName, ItemName = "DemoItem" });