Deletes a single item in a table by primary key.
You can perform a conditional delete operation that deletes the item if it exists, or if it has an expected attribute value.
Access
public
Parameters
Parameter |
Type |
Required |
Description |
|---|---|---|---|
|
|
Optional |
An associative array of parameters that can have the following keys:
|
Returns
Type |
Description |
|---|---|
|
A |
Examples
Create a new table with a Hash Key.
// Instantiate the class
$dynamodb = new AmazonDynamoDB();
$table_name = 'my-table-' . time();
$current_time = (int) time();
####################################################################
# Create a new DynamoDB table
$response = $dynamodb->create_table(array(
'TableName' => $table_name,
'KeySchema' => array(
'HashKeyElement' => array(
'AttributeName' => 'id',
'AttributeType' => AmazonDynamoDB::TYPE_NUMBER
),
'RangeKeyElement' => array(
'AttributeName' => 'date',
'AttributeType' => AmazonDynamoDB::TYPE_NUMBER
)
),
'ProvisionedThroughput' => array(
'ReadCapacityUnits' => 50,
'WriteCapacityUnits' => 50
)
));
// Check for success...
if ($response->isOK())
{
echo '# Kicked off the creation of the DynamoDB table...' . PHP_EOL;
}
else
{
print_r($response);
}
####################################################################
# Sleep and poll until the table has been created
$count = 0;
do {
sleep(1);
$count++;
$response = $dynamodb->describe_table(array(
'TableName' => $table_name
));
}
while ((string) $response->body->Table->TableStatus !== 'ACTIVE');
echo "The table \"${table_name}\" has been created (slept ${count} seconds)." . PHP_EOL;
####################################################################
# Adding data to the table
echo PHP_EOL . PHP_EOL;
echo "# Adding data to the table..." . PHP_EOL;
// Add items to the batch
$response = $dynamodb->batch_write_item(array(
'RequestItems' => array(
$table_name => array(
array(
'PutRequest' => array(
'Item' => $dynamodb->attributes(array(
'id' => 1, // Primary (Hash) Key
'date' => $current_time, // Range Key
'key1' => 'value1',
'key2' => 'value2',
'key3' => array('sub-value1', 'sub-value2'),
))
)
),
array(
'PutRequest' => array(
'Item' => $dynamodb->attributes(array(
'id' => 2, // Primary (Hash) Key
'date' => $current_time, // Range Key
'key1' => 'value3',
'key2' => 'value4',
'key3' => array('sub-value1', 'sub-value2'),
))
)
),
array(
'PutRequest' => array(
'Item' => $dynamodb->attributes(array(
'id' => 3, // Primary (Hash) Key
'date' => $current_time, // Range Key
'key1' => 'value5',
'key2' => 'value6',
'key3' => array('sub-value1', 'sub-value2'),
))
)
),
)
)
));
// Check for success...
if ($response->isOK())
{
echo "The data has been added to the table." . PHP_EOL;
}
else
{
print_r($response);
}
####################################################################
# Getting an item
echo PHP_EOL . PHP_EOL;
echo "# Getting an item from the table..." . PHP_EOL;
// Get an item
$response = $dynamodb->get_item(array(
'TableName' => $table_name,
'Key' => $dynamodb->attributes(array(
'HashKeyElement' => 3, // "id" column
'RangeKeyElement' => $current_time, // "date" column
)),
'AttributesToGet' => array('id', 'date', 'key1', 'key2', 'key3'),
'ConsistentRead' => 'true'
));
// Check for success...
if ($response->isOK())
{
var_dump((string) $response->body->Item->key1->{AmazonDynamoDB::TYPE_STRING});
}
else
{
print_r($response);
}
####################################################################
# Updating an item
echo PHP_EOL . PHP_EOL;
echo "# Updating an item from the table..." . PHP_EOL;
// Updating an item
$response = $dynamodb->update_item(array(
'TableName' => $table_name,
'Key' => $dynamodb->attributes(array(
'HashKeyElement' => 3, // "id" column
'RangeKeyElement' => $current_time, // "date" column
)),
'AttributeUpdates' => array(
'key1' => array(
'Action' => AmazonDynamoDB::ACTION_PUT,
'Value' => array(AmazonDynamoDB::TYPE_STRING => 'updated-value1')
),
'key2' => array(
'Action' => AmazonDynamoDB::ACTION_DELETE
),
'key3' => array(
'Action' => AmazonDynamoDB::ACTION_ADD,
'Value' => array(AmazonDynamoDB::TYPE_STRING_SET => array('sub-value3'))
),
'key4' => array(
'Action' => AmazonDynamoDB::ACTION_PUT,
'Value' => $dynamodb->binary('binary-value')
),
),
'Expected' => array(
'key1' => array(
'Value' => array( AmazonDynamoDB::TYPE_STRING => 'value5' )
)
)
));
// Check for success...
if ($response->isOK())
{
echo 'Updating the item...' . PHP_EOL;
}
else
{
print_r($response);
}
####################################################################
# Checking the binary result
echo PHP_EOL . PHP_EOL;
echo "# Getting an item from the table..." . PHP_EOL;
// Get an item
$response = $dynamodb->get_item(array(
'TableName' => $table_name,
'Key' => $dynamodb->attributes(array(
'HashKeyElement' => 3, // "id" column
'RangeKeyElement' => $current_time, // "date" column
)),
'AttributesToGet' => array('key4'),
'ConsistentRead' => 'true'
));
// Check for success...
if ($response->isOK())
{
var_dump((string) $response->body->Item->key4->{AmazonDynamoDB::TYPE_BINARY});
}
else
{
print_r($response);
}
####################################################################
# Deleting an item
echo PHP_EOL . PHP_EOL;
echo "# Deleting an item from the table..." . PHP_EOL;
// Deleting an item
$response = $dynamodb->delete_item(array(
'TableName' => $table_name,
'Key' => $dynamodb->attributes(array(
'HashKeyElement' => 1, // "id" column
'RangeKeyElement' => $current_time, // "date" column
)),
));
// Check for success...
if ($response->isOK())
{
echo 'Deleting the item...' . PHP_EOL;
}
else
{
print_r($response);
}
####################################################################
# Deleting the table
echo PHP_EOL . PHP_EOL;
echo "# Deleting the \"${table_name}\" table..." . PHP_EOL;
$response = $dynamodb->delete_table(array(
'TableName' => $table_name
));
// Check for success...
if ($response->isOK())
{
echo 'The table is in the process of deleting...' . PHP_EOL;
}
else
{
print_r($response);
}
####################################################################
# Sleep and poll until the table has been deleted.
$count = 0;
do {
echo '.';
sleep(1);
$count++;
$response = $dynamodb->describe_table(array(
'TableName' => $table_name
));
}
while ((integer) $response->status !== 400);
echo "The table \"${table_name}\" has been deleted (slept ${count} seconds)." . PHP_EOL;
Source
Method defined in services/dynamodb.class.php | Toggle source view (6 lines) | View on GitHub

