Skip to content
Masayuki Tanaka edited this page Aug 3, 2013 · 19 revisions

This module, dynamodb-php-wrapper, allows you to access DynamoDB more easily through interfaces shown below. Each interface is just a wrapper, so accepts same arguments original one does and you can get that information from the document provided by AWS if needed.

This section assumes that:

  • There is a table named 'User', which has hash key named 'UserId' as Number.
  • There is a table named 'Friend', which has hash key named 'UserId' as Number and range key 'FriendUserId' as Number.

init

First of all, DynamoDBWrapper needs to be instantiated like this:

<?php

require_once '/path/to/aws-autoloader.php'; // provided by AWS
require_once '/path/to/DynamoDBWrapper.php'; // this module

$ddb = new DynamoDBWrapper(array(
    'key'    => 'YOUR_KEY',
    'secret' => 'YOUR_SECRET_KEY',
    'region' => 'SOME_REGION'
));

then, you can access DynamoDB through this instance.

get

get is a wrapper of GetItem and will be used like this:

<?php
$key = array(
    'UserId' => array('N' => 2),
);
$result = $ddb->get('User', $key);
/*
Array
(
    [Name] => User2
    [UserId] => 2
)
*/

batchGet

batchGet is a wrapper of BatchGetItem and will be used like this:

<?php

$keys = array(
    array(
        'UserId' => array('N' => 2),
    ),
    array(
        'UserId' => array('N' => 3),
    ),
);
$result = $ddb->batchGet('User', $keys);
/*
Array
(   
    [0] => Array
        (   
            [Name] => Masayuki Tanaka
            [UserId] => 2
        )

    [1] => Array
        (   
            [Name] => Masayuki Tanaka
            [UserId] => 3
        )

)
*/

batchGet can get more than 25 items by one call. This wrapper sends multiple requests to DynamoDb if needed, and merge them as a single result.

query

scan

count

put

put is a wrapper of putItem and will be used like this:

<?php

$item = array(
    'UserId' => array('N' => 1),
    'Name'   => array('S' => 'Masayuki Tanaka'),
);
$result = $ddb->put('User', $item);
// true, if success

If it needs to be conditional, you can specify the expectation like this:

<?php

$item = array(
    'UserId' => array('N' => 1),
    'Name'   => array('S' => 'User1'),
);
$expected = array(
    'UserId' => array('Exists' => false)
);
$result = $ddb->put('User', $item, $expected);
// false, if UserId = 1 exists

batchPut

batchPut is a wrapper of BatchWriteItem with put requests and will be used like this:

<?php

$items = array(
    array(
        'UserId' => array('N' => 2),
        'Name'   => array('S' => 'User2'),
    ),
    array(
        'UserId' => array('N' => 3),
        'Name'   => array('S' => 'User3'),
    ),
);
$result = $ddb->batchPut('User', $items);
// true, if success

update

delete

batchDelete

createTable

Clone this wiki locally