dev-master
9999999-devAccess AWS DynamoDB through simpler interface in PHP.
MIT
The Requires
aws dynamodb dynamodb php
1.0.0
1.0.0.0Access AWS DynamoDB through simpler interface in PHP.
MIT
The Requires
aws dynamodb dynamodb php
Wallogit.com
2017 © Pedro Peláez
Access AWS DynamoDB through simpler interface in PHP.
Access AWS DynamoDB through simpler interface in PHP, (*1)
This module, dynamodb-php-wrapper, allows you to access DynamoDB more easily through interfaces shown below., (*2)
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., (*3)
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., (*4)
First of all, DynamoDBWrapper needs to be instantiated like this:, (*5)
<?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',
'version' => 'latest'
));
then, you can access DynamoDB through this instance., (*6)
get is a wrapper of GetItem and will be used like this:, (*7)
<?php
$key = array(
'UserId' => 2,
);
$result = $ddb->get('User', $key);
/*
Array
(
[Name] => User2
[UserId] => 2
)
*/
batchGet is a wrapper of BatchGetItem and will be used like this:, (*8)
<?php
$keys = array(
array(
'UserId' => 2,
),
array(
'UserId' => 3,
),
);
$result = $ddb->batchGet('User', $keys);
/*
Array
(
[0] => Array
(
[Name] => User 2
[UserId] => 2
)
[1] => Array
(
[Name] => User 3
[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., (*9)
query is a wrapper of Query and will be used like this:, (*10)
<?php
$keyConditions = array(
'UserId' => 2,
);
$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., (*11)
scan is a wrapper of Scan and will be used like this:, (*12)
<?php
$filter = array(
'UserId' => array('GT', 1),
'FriendUserId' => array('IN', array(4, 5)),
);
$result = $ddb->scan('Friend', $filter);
/*
Array
(
[0] => Array
(
[UserId] => 2
[FriendUserId] => 4
)
[1] => Array
(
[UserId] => 2
[FriendUserId] => 5
)
...
)
*/
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:, (*13)
<?php
$keyConditions = array(
'UserId' => 2,
);
$result = $ddb->count('Friend', $keyConditions);
// 5
put is a wrapper of putItem and will be used like this:, (*14)
<?php
$item = array(
'UserId' => 1,
'Name' => 'User N',
);
$result = $ddb->put('User', $item);
// true, if success
If operation needs to be conditional, you can specify the expectation like this:, (*15)
<?php
$item = array(
'UserId' => 1,
'Name' => 'User 1',
);
$expected = array(
'UserId' => array('Exists' => false)
);
$result = $ddb->put('User', $item, $expected);
// false, if UserId = 1 exists
batchPut is a wrapper of BatchWriteItem with put requests and will be used like this:, (*16)
<?php
$items = array(
array(
'UserId' => 2,
'Name' => 'User2',
),
array(
'UserId' => 3,
'Name' => '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., (*17)
update is a wrapper of UpdateItem and will be used like this:, (*18)
<?php
$key = array(
'UserId' => 2,
);
$update = array(
'Name' => array('PUT', 'New User Name')
);
$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., (*19)
delete is a wrapper of DeleteItem and will be used like this:, (*20)
<?php
$key = array(
'UserId' => 2,
'FriendUserId' => 4,
);
$result = $ddb->delete('Friend', $key);
/*
Array
(
[UserId] => 2
[FriendUserId] => 4
)
*/
If operation succeeded, the deleted item will be returned., (*21)
batchDelete is a wrapper of BatchWriteItem with put requests and will be used like this:, (*22)
<?php
$keys = array(
array(
'UserId' => 2,
'FriendUserId' => 3,
),
array(
'UserId' => 2,
'FriendUserId' => 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., (*23)
createTable is a wrapper of CreateTable and will be used like this:, (*24)
<?php
$result = $ddb->createTable('User', 'UserId');
// Create a table 'User', which has a hash key 'UserId' as STRING.
$result = $ddb->createTable('User', 'UserId::N');
// Create a table 'User', which has a hash key 'UserId' as NUMBER.
$result = $ddb->createTable('Friend', 'UserId', 'FriendUserId');
// Create a table 'Friend', which has a hash key 'UserId' as STRING and a range key 'FriendUserId' as STRING.
If LSI is needed, it can be added like this:, (*25)
<?php
$options = array(
'LocalSecondaryIndexes' => array(
array('name' => "UserName",
'type' => 'S',
'projection_type' => 'KEYS_ONLY'
),
array('name' => "CreatedAt",
'type' => 'S',
'projection_type' => 'KEYS_ONLY'
)
)
);
$result = $ddb->createTable('Friend', 'UserId', 'FriendUserId', $options);
// Create a table 'Friend', which has a hash key 'UserId' as STRING and a range key 'FriendUserId' as STRING and Local Secondary Index 'UserName' and 'CreatedAt' as both STRING.
After requested, it will wait for completion., (*26)
Access AWS DynamoDB through simpler interface in PHP.
MIT
aws dynamodb dynamodb php
Access AWS DynamoDB through simpler interface in PHP.
MIT
aws dynamodb dynamodb php