Skip to content

Instantly share code, notes, and snippets.

@GromNaN
Last active March 25, 2020 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GromNaN/1e7a651871fd1ac174d02254bf9b3fe3 to your computer and use it in GitHub Desktop.
Save GromNaN/1e7a651871fd1ac174d02254bf9b3fe3 to your computer and use it in GitHub Desktop.
Trying async-aws/dynamodb
<?php
namespace Testing;
use AsyncAws\DynamoDb\DynamoDbClient;
require __DIR__.'/vendor/autoload.php';
$dynamodbClient = new DynamoDbClient(['region' => 'eu-west-1']);
try {
// Create an "errors" table
$dynamodbClient->createTable([
'TableName' => 'errors',
'AttributeDefinitions' => [
['AttributeName' => 'id', 'AttributeType' => 'N'],
['AttributeName' => 'time', 'AttributeType' => 'N'],
],
'KeySchema' => [
['AttributeName' => 'id', 'KeyType' => 'HASH'],
['AttributeName' => 'time', 'KeyType' => 'RANGE'],
],
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 20
],
])->resolve();
echo "> creating table ...\n";
// Wait until the table is created and active
$dynamodbClient->tableExists(['TableName' => 'errors'])->wait();
echo "> table created\n";
// Update the provisioned throughput capacity of the table
$dynamodbClient->updateTable([
'TableName' => 'errors',
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 15,
'WriteCapacityUnits' => 25,
],
]);
echo "> updating table\n";
// Wait until the table is active again after updating
$dynamodbClient->tableExists(['TableName' => 'errors'])->wait();
echo "> table updated\n";
echo "> describe table\n";
$result = $dynamodbClient->describeTable([
'TableName' => 'errors'
]);
// The result of an operation as an object representation
echo $result->getTable()->getItemCount() . "\n";
//> 0
// Retrieve deeply nested object values
echo $result->getTable()->getProvisionedThroughput()->getReadCapacityUnits() . "\n";
//> 15
echo "> listing tables\n";
$result = $dynamodbClient->listTables();
// Iterate over the tables names
foreach ($result->getIterator() as $tableName) {
echo $tableName . "\n";
}
echo "> Adding item\n";
$time = (string) time();
// You can add an item to our errors table using the putItem() method of the client.
$result = $dynamodbClient->putItem([
'TableName' => 'errors',
'Item' => [
'id' => ['N' => '1201'],
'time' => ['N' => $time],
'error' => ['S' => 'Executive overflow'],
'message' => ['S' => 'no vacant areas'],
]
]);
echo "Consumed capacity: " . ($result->getConsumedCapacity() ? $result->getConsumedCapacity()->getCapacityUnits() : null) . "\n";
echo "> Retrieving items\n";
$result = $dynamodbClient->getItem([
'ConsistentRead' => true,
'TableName' => 'errors',
'Key' => [
'id' => ['N' => '1201'],
'time' => ['N' => $time],
]
]);
// Grab value from the result object like an array
echo $result->getItem()['id']->getN() . "\n";
//> 1201
echo $result->getItem()['error']->getS() . "\n";
//> Executive overflow
echo $result->getItem()['message']->getS() . "\n";
//> no vacant areas
echo "> Query\n";
$iterator = $dynamodbClient->query([
'TableName' => 'errors',
'KeyConditions' => [
'id' => [
'AttributeValueList' => [
['N' => '1201']
],
'ComparisonOperator' => 'EQ'
],
'time' => [
'AttributeValueList' => [
['N' => (string) strtotime("-15 minutes")]
],
'ComparisonOperator' => 'GT'
]
]
]);
// Each item will contain the attributes we added
/** @var AttributeValue $item */
foreach ($iterator as $item) {
// Grab the time number value
echo $item['time']->getN() . "\n";
// Grab the error string value
echo $item['error']->getS() . "\n";
}
echo "> Scan\n";
$iterator = $dynamodbClient->scan([
'TableName' => 'errors',
'ScanFilter' => [
'error' => [
'AttributeValueList' => [
['S' => 'overflow']
],
'ComparisonOperator' => 'CONTAINS'
],
'time' => [
'AttributeValueList' => [
['N' => (string) strtotime('-15 minutes')]
],
'ComparisonOperator' => 'GT'
]
]
]);
// Each item will contain the attributes we added
foreach ($iterator as $item) {
// Grab the time number value
echo $item['time']->getN() . "\n";
// Grab the error string value
echo $item['error']->getS() . "\n";
}
echo "> Deleting items\n";
$scan = $dynamodbClient->scan(['TableName' => 'errors']);
foreach ($scan as $item) {
$dynamodbClient->deleteItem([
'TableName' => 'errors',
'Key' => [
'id' => ['N' => $item['id']->getN()],
'time' => ['N' => $item['time']->getN()]
]
]);
}
} finally {
echo "> Deleting table\n";
$dynamodbClient->deleteTable(['TableName' => 'errors']);
$dynamodbClient->tableNotExists(['TableName' => 'errors'])->wait();
echo "> Table deleted\n";
}
$ php dynamodb.php
> creating table ...
> table created
> updating table
> table updated
> describe table
0
15
> listing tables
...
errors
...
> Adding item
Consumed capacity:
> Retrieving items
1201
Executive overflow
no vacant areas
> Query
1584628068
Executive overflow
> Scan
1584628068
Executive overflow
> Deleting items
> Deleting table
> Table deleted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment