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 'User', which has hash key 'UserId' as Number.
  • There is a table 'Friend', which has hash key '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

query is a wrapper of Query and will be used like this:

<?php

$keyConditions = array(
    'UserId' => array(
        'AttributeValueList' => array(
            array('N' => 2),
        ),
        'ComparisonOperator' => 'EQ'
    ),
);
$result = $ddb->query('Friend', $keyConditions);
/*
Array
(
    [0] => Array
        (
            [UserId] => 2
            [FriendUserId] => 3
        )
    [1] => Array
        (
            [UserId] => 2
            [FriendUserId] => 4
        )
    ...
)
*/

query can accept options as $options, such as Limit, ExclusiveStartKey, ScanIndexForward and IndexName. If you use SLI, IndexName must be included in options.

scan

scan is a wrapper of Scan and will be used like this:

<?php

$filter = array(
    'UserId' => array(
        'AttributeValueList' => array(
            array('N' => 1),
        ),
        'ComparisonOperator' => 'GT'
    ),
    'FriendUserId' => array(
        'AttributeValueList' => array(
            array('N' => 4),
            array('N' => 5),
        ),
        'ComparisonOperator' => 'IN'
    ),
);
$result = $ddb->scan('Friend', $filter);
/*
Array
(
    [0] => Array
        (
            [UserId] => 2
            [FriendUserId] => 4
        )
    [1] => Array
        (
            [UserId] => 2
            [FriendUserId] => 5
        )
    ...
)
*/

count

count is a wrapper of Query with Select param as COUNT. This return the number of items query fetches and will be used like this:

<?php

$keyConditions = array(
    'UserId' => array(
        'AttributeValueList' => array(
            array('N' => 2),
        ),
        'ComparisonOperator' => 'EQ'
    ),
);
$result = $ddb->count('Friend', $keyConditions);
// 5

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 operation 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

If the number of requests is more than 25, this module divides the requests and send them continuously until all of requests are completed.

update

update is a wrapper of UpdateItem and will be used like this:

<?php

$key = array(
    'UserId' => array('N' => 2),
);
$update = array(
    'Name' => array(
        'Value'  => array('S' => 'New User Name'),
        'Action' => 'PUT',
    ),
);
$result = $ddb->update('User', $key, $update);
/*
Array
(
    [Name] => New User Name
)
*/

If operation succeeded, the updated value will be returned. If operation needs to be conditional, you can specify the expectation as 3rd arg.

delete

delete is a wrapper of DeleteItem and will be used like this:

<?php

$key = array(
    'UserId'       => array('N' => 2),
    'FriendUserId' => array('N' => 4),
);
$result = $ddb->delete('Friend', $key);
/*
Array
(
    [UserId] => 2
    [FriendUserId] => 4
)
*/

If operation succeeded, the deleted item will be returned.

batchDelete

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

<?php

$keys = array(
    array(
        'UserId'       => array('N' => 2),
        'FriendUserId' => array('N' => 3),
    ),
    array(
        'UserId'       => array('N' => 2),
        'FriendUserId' => array('N' => 4),
    ),
    ...
);
$result = $ddb->batchDelete('Friend', $keys);
// true, if success

If the number of requests is more than 25, this module divides the requests and send them continuously until all of requests are completed.

createTable

createTable is a wrapper of CreateTable and will be used like this:

<?php

$result = $ddb->createTable('User', 'UserId', 'N');
// Create a table 'User', which has a hash key 'UserId' as Number.
 
$result = $ddb->createTable('Friend', 'UserId', 'N', 'FriendUserId', 'N');
// Create a table 'Friend', which has a hash key 'UserId' as Number and a range key 'FriendUserId' as Number.

If LSI is needed, it can be added like this:

<?php

$LSI = array(
    array('name' => "UserName",
          'type' => 'S',
          'projection_type' => 'KEYS_ONLY'
    ),
    array('name' => "CreatedAt",
          'type' => 'S',
          'projection_type' => 'KEYS_ONLY'
    )
);
$result = $ddb->createTable('Friend', 'UserId', 'N', 'FriendUserId', 'N', $LSI);
// Create a table 'Friend', which has a hash key 'UserId' as Number and a range key 'FriendUserId' and Local Secondary Index 'UserName' and 'CreatedAt'.
Clone this wiki locally