在 EBS(Elastic Block Store)出來前(註: 有 EBS 就可以將 MySQL data 目錄 mount 上去.), 若要使用 EC2 搭配的資料庫, 就是得要選 SimpleDB, 雖然現在可以有 EBS 用, 不過 SimpleDB 還是有值得使用的地方, 應用範圍再自行想像囉~ :)
Amazon SimpleDB 儲存的格式是 Domain -> PKeys, PKey -> Attributes, Attributes 有很多 Key ->Value (註: Attributes 就是一筆紀錄下面, 可以有多個 Key -> Value 的紀錄), ex:
Key: 1
Attributes:
Category: Book
Price: 200
Date: 2009-01-01
而且 SimpleDB 也有簡易的 Select 語法可以使用(在此篇還沒詳細研究 Select 語法.)
相關資訊:
Amazon SimpleDB 研究相關文件蒐集
詳細文件
安裝 PHP Library for Amazon SimpleDB
- 於 PHP Library for Amazon SimpleDB 下載 Library
- unzip amazon-simpledb-2007-11-07-php5-library.zip
- 準備 Access Key ID and Secret Access Key (可於: Amazon Web Services Access Identifiers 取得)
簡易 Client 測試(測試是否可以連線存取)
- cd amazon-simpledb-2007-11-07-php5-library/src/
- vim test_client.php
<?php
$AWS_ACCESS_KEY_ID = 'Your Access Key ID';
$AWS_SECRET_ACCESS_KEY = 'Your Secret Access Key';
// Mock: 仿造 Amazon 回應格式
require_once ('Amazon/SimpleDB/Mock.php');
$service = new Amazon_SimpleDB_Mock();
// 正式連到 Amazon 的 Client
require_once ('Amazon/SimpleDB/Client.php');
$service = new Amazon_SimpleDB_Client($AWS_ACCESS_KEY_ID, $AWS_SECRET_ACCESS_KEY);
?>
- 執行 php test_client.php # 不會有任何訊息, 沒有訊息就是好訊息~ :)
測試範例程式前置設定
- vim amazon-simpledb-2007-11-07-php5-library/src/Amazon/SimpleDB/Samples/.config.inc.php
define('AWS_ACCESS_KEY_ID', '<Your Access Key ID>');
define('AWS_SECRET_ACCESS_KEY', '<Your Secret Access Key>');
- 只要將 key 填入, 剩下的範例再自行修改測試.
建立 Domain Name
- vim src/Amazon/SimpleDB/Samples/CreateDomainSample.php
- 於 $service = new Amazon_SimpleDB_Client(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY); 下面加入:
$request = new Amazon_SimpleDB_Model_CreateDomainRequest();
invokeCreateDomain($service, $request->withDomainName('MyDomain')); // MyDomain 請自行替換
- php src/Amazon/SimpleDB/Samples/CreateDomainSample.php
- 即可建立 Domain.
- 註: $service = new Amazon_SimpleDB_Mock(); // 使用 Mock 可模擬回傳的資料, 可以先寫好再實際測試看看~
- 程式執行後的回傳資料如下:
Service Response
==================================
CreateDomainResponse
ResponseMetadata
RequestId
22230000-1111-1111-1111-1111111111
BoxUsage
0.0055590278
列出 Domain Name
- vim src/Amazon/SimpleDB/Samples/ListDomainSample.php
- 移除 invokeListDomains($service, $request); 前面的註解, 並於前面加入 $request 的程式, 完整如下:(換掉 @TODO: set request 這行)
$request = new Amazon_SimpleDB_Model_ListDomainsRequest();
invokeListDomains($service, $request);
- php src/Amazon/SimpleDB/Samples/ListDomainSample.php
- 就可以得到 Domain List. (下述的 MyDomain 就是你建立的 DomainName 囉~)
- 程式執行後的回傳資料如下:
Service Response
==========================================
ListDomainsResponse
ListDomainsResult
DomainName
MyDomain ResponseMetadata
RequestId
0ff30000-aaaa-bbbb-eeee-cc11111111
BoxUsage
0.0000071759
直接呼叫 Library:
$service = new Amazon_SimpleDB_Client(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);
$request = new Amazon_SimpleDB_Model_ListDomainsRequest();
print_r($response = $service->listDomains($request));
新增一筆資料
- 程式:
$domainName = 'MyDomain';
/**
* Load Item_01:
* Category = Clothes
* Name = Cathair Sweater
* Size = Small, Medium, Large
*/
$itemName = 'Item_01';
$attribute1 = new Amazon_SimpleDB_Model_ReplaceableAttribute();
$attribute1->withName('Category')->withValue('Clothes');
$attribute2 = new Amazon_SimpleDB_Model_ReplaceableAttribute();
$attribute2->withName('Name')->withValue('Cathair Sweater');
$attribute3 = new Amazon_SimpleDB_Model_ReplaceableAttribute();
$attribute3->withName('Size')->withValue('Small');
$attribute4 = new Amazon_SimpleDB_Model_ReplaceableAttribute();
$attribute4->withName('Size')->withValue('Medium');
$attribute5 = new Amazon_SimpleDB_Model_ReplaceableAttribute();
$attribute5->withName('Size')->withValue('Large');
$attributeList = array($attribute1, $attribute2, $attribute3, $attribute4, $attribute5);
$request = new Amazon_SimpleDB_Model_PutAttributesRequest();
$request->withDomainName($domainName)->withItemName($itemName)->setAttribute($attributeList);
invokePutAttributes($service, $request);
- 回傳值
Service Response
======================================
PutAttributesResponse
ResponseMetadata
RequestId
11111111-1111-1111-1111-111111111111
BoxUsage
0.0000220157
取得所有資料列表
- vim src/Amazon/SimpleDB/Samples/QuerySample.php
$request = new Amazon_SimpleDB_Model_QueryRequest();
$request->setDomainName('MyDomain');
invokeGetAttributes($service, $request);
- 回傳值
- Service Response
=======================================
QueryResponse
QueryResult
ItemName
Item_01 ItemName
Item_02 ResponseMetadata
RequestId
11111111-1111-1111-1111-111111111111
BoxUsage
0.0000140160
取得一筆資料
- vim src/Amazon/SimpleDB/Samples/GetAttributesSample.php
$request = new Amazon_SimpleDB_Model_GetAttributesRequest();
$request->setDomainName('MyDomain');
$request->setItemName('Item_01');
invokeGetAttributes($service, $request);
- 回傳值
GetAttributesResponse
GetAttributesResult
Attribute
Name
Name
Value
Cathair Sweater
Attribute
Name
Category
Value
Clothes
Attribute
Name
Size
Value
Large
Attribute
Name
Size
Value
Medium
Attribute
Name
Size
Value
Small
ResponseMetadata
RequestId
11111111-1111-1111-1111-111111111111
BoxUsage
0.0000093382
修改一筆資料
- cp PutAttributesSample.php ReplactAttributesSample.php
- vim src/Amazon/SimpleDB/Samples/ReplactAttributesSample.php
$domainName = 'MyDomain';
$attribute = new Amazon_SimpleDB_Model_ReplaceableAttribute();
$attribute->withName('Category')->withValue('Abc')->withReplace(true);
$request = new Amazon_SimpleDB_Model_PutAttributesRequest();
$request->withDomainName('MyDomain')->withItemName('Item_03')->withAttribute($attribute);
invokePutAttributes($service, $request);
- 回傳值
Service Response
========================================
PutAttributesResponse
ResponseMetadata
RequestId
11111111-1111-1111-1111-111111111111
BoxUsage
0.0000219909
取得 Domain 的指定屬性的資料
- cp QuerySample.php GetDomainData.php
- vim src/Amazon/SimpleDB/Samples/GetDomainData.php
$request = new Amazon_SimpleDB_Model_QueryRequest();
$request->setDomainName('MyDomain');
$request->setQueryExpression("['Category' = 'Clothes']");
invokeQuery($service, $request);
- 回傳值
GetAttributesResponse
GetAttributesResult
Attribute
Name
Name
Value
Cathair Sweater
Attribute
Name
Category
Value
Clothes
Attribute
Name
Size
Value
Large
Attribute
Name
Size
Value
Medium
Attribute
Name
Size
Value
Small
ResponseMetadata
RequestId
7274beca-7bf8-4328-90e0-3a07905b2e2d
BoxUsage
0.0000093382
取得 Domain 指定多重屬性的資料(交叉比對)
- cp GetDomainData.php GetDomainDatas.php
- vim src/Amazon/SimpleDB/Samples/GetDomainData.php
$request = new Amazon_SimpleDB_Model_QueryRequest();
$request->setDomainName('MyDomain');
$request->setQueryExpression('[\'Category\' = \'Abc\'] intersection [\'Size\' = \'Small2\']');
invokeQuery($service, $request);
- 回傳值
Service Response
===========================================
QueryResponse
QueryResult
ItemName
Item_03 ResponseMetadata
RequestId
3f9c4d59-1293-4991-a7ac-03924b5cdf43
BoxUsage
0.0000140120
刪除一筆資料
- vim src/Amazon/SimpleDB/Samples/DeleteAttributesSample.php
$attribute = new Amazon_SimpleDB_Model_Attribute();
// $attribute->withName('Name')->withValue('Cathair Sweater'); // 刪除 MyDomain 的 Item_02, Name 是 Cathair Sweater 的紀錄.
$request = new Amazon_SimpleDB_Model_DeleteAttributesRequest();
$request->withDomainName('MyDomain')->withItemName('Item_02')->withAttribute($attribute); // 刪除 Item_02 的紀錄
invokeDeleteAttributes($service, $request);
- 回傳值
Service Response
==========================================
DeleteAttributesResponse
ResponseMetadata
RequestId
11111111-1111-1111-1111-111111111111
BoxUsage
0.0000219909
刪除 Domain Name
注意: 若裡面有資料, 整個 Domain 刪掉時, 並不會特別警告.
vim src/Amazon/SimpleDB/Samples/DeleteDomainSample.php
$request = new Amazon_SimpleDB_Model_DeleteDomainRequest();
$request->setDomainName('MyDomain');
invokeDeleteDomain($service, $request);
Source