Amazon SimpleDB 操作筆記 與 程式範例(PHP)

在 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

  1. PHP Library for Amazon SimpleDB 下載 Library
  2. unzip amazon-simpledb-2007-11-07-php5-library.zip
  3. 準備 Access Key ID and Secret Access Key (可於: Amazon Web Services Access Identifiers 取得)

簡易 Client 測試(測試是否可以連線存取)

  1. cd amazon-simpledb-2007-11-07-php5-library/src/
  2. 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);
    ?>

  3. 執行 php test_client.php # 不會有任何訊息, 沒有訊息就是好訊息~ 🙂

測試範例程式前置設定

  1. 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>');

  2. 只要將 key 填入, 剩下的範例再自行修改測試.

建立 Domain Name

  1. vim src/Amazon/SimpleDB/Samples/CreateDomainSample.php
  2. 於 $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 請自行替換

  3. php src/Amazon/SimpleDB/Samples/CreateDomainSample.php
  4. 即可建立 Domain.
  5. 註: $service = new Amazon_SimpleDB_Mock(); // 使用 Mock 可模擬回傳的資料, 可以先寫好再實際測試看看~
  6. 程式執行後的回傳資料如下:

    Service Response
    =============================================================================
    CreateDomainResponse
        ResponseMetadata
            RequestId
                22230000-1111-1111-1111-1111111111
            BoxUsage
                0.0055590278

列出 Domain Name

  1. vim src/Amazon/SimpleDB/Samples/ListDomainSample.php
  2. 移除 invokeListDomains($service, $request); 前面的註解, 並於前面加入 $request 的程式, 完整如下:(換掉 @TODO: set request 這行)

    $request = new Amazon_SimpleDB_Model_ListDomainsRequest();
    invokeListDomains($service, $request);

  3. php src/Amazon/SimpleDB/Samples/ListDomainSample.php
  4. 就可以得到 Domain List. (下述的 MyDomain 就是你建立的 DomainName 囉~)
  5. 程式執行後的回傳資料如下:

    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));

新增一筆資料

  1. 程式:

    $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);

  2. 回傳值

    Service Response
    =============================================================================
    PutAttributesResponse
        ResponseMetadata
            RequestId
                11111111-1111-1111-1111-111111111111
            BoxUsage
                0.0000220157

取得所有資料列表

  1. vim src/Amazon/SimpleDB/Samples/QuerySample.php

    $request = new Amazon_SimpleDB_Model_QueryRequest();
    $request->setDomainName('MyDomain');
    invokeGetAttributes($service, $request);

  2. 回傳值
  3. Service Response
    =============================================================================
    QueryResponse
        QueryResult
            ItemName
                Item_01                ItemName
                Item_02            ResponseMetadata
            RequestId
                11111111-1111-1111-1111-111111111111
            BoxUsage
                0.0000140160

取得一筆資料

  1. vim src/Amazon/SimpleDB/Samples/GetAttributesSample.php


    $request = new Amazon_SimpleDB_Model_GetAttributesRequest();
    $request->setDomainName('MyDomain');
    $request->setItemName('Item_01');
    invokeGetAttributes($service, $request);

  2. 回傳值

    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

修改一筆資料

  1. cp PutAttributesSample.php ReplactAttributesSample.php
  2. 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);

  3. 回傳值

    Service Response
    =============================================================================
    PutAttributesResponse
        ResponseMetadata
            RequestId
                11111111-1111-1111-1111-111111111111
            BoxUsage
                0.0000219909

取得 Domain 的指定屬性的資料

  1. cp QuerySample.php GetDomainData.php
  2. vim src/Amazon/SimpleDB/Samples/GetDomainData.php

    $request = new Amazon_SimpleDB_Model_QueryRequest();
    $request->setDomainName('MyDomain');
    $request->setQueryExpression("['Category' = 'Clothes']");
    invokeQuery($service, $request);

  3. 回傳值

    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 指定多重屬性的資料(交叉比對)

  1. cp GetDomainData.php GetDomainDatas.php
  2. 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);

  3. 回傳值

    Service Response
    =============================================================================
    QueryResponse
        QueryResult
            ItemName
                Item_03            ResponseMetadata
        RequestId
            3f9c4d59-1293-4991-a7ac-03924b5cdf43
        BoxUsage
            0.0000140120

刪除一筆資料

  1. 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);

  2. 回傳值

    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);

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料