Amazon DynamoDB
Developer Guide (API Version 2012-08-10)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Example: Batch Operations Using AWS SDK for PHP

Example: Batch Write Operation Using the AWS SDK for PHP

The following PHP code example uses batch write API to perform the following tasks:

  • Put an item in the Forum table.

  • Put and delete an item from the Thread table.

To learn more about the batch write operation, see Batch Write: Putting and Deleting Multiple Items.

This code example assumes that you have followed the Getting Started ( Getting Started) and created the Forum and Thread tables. Alternatively, you can load the data programmatically using the instructions in the Creating Example Tables and Uploading Data Using the AWS SDK for PHP topic.

Note

For step-by-step instructions to test the following code example, see Running PHP Examples.

<?php

// Include the SDK using the Composer autoloader
require "vendor/autoload.php";

use Aws\DynamoDb\DynamoDbClient;
use Aws\Common\Enum\Region;
use Aws\DynamoDb\Enum\Type;

// Instantiate the DynamoDB client with your AWS credentials
$aws = Aws\Common\Aws::factory("./config.php");
$client = $aws->get("dynamodb");

$tableNameOne = "Forum";
$tableNameTwo = "Thread";

$response = $client->batchWriteItem(array(
    "RequestItems" => array(
        $tableNameOne => array(
            array(
                "PutRequest" => array(
                    "Item" => array(
                        "Name"   => array(Type::STRING => "S3 Forum"),             
                        "Threads" => array(Type::NUMBER => 0)
                    ))
            )
        ),          
         $tableNameTwo => array(
            array(
                "PutRequest" => array(
                    "Item" => array(
                        "ForumName"   => array(Type::STRING => "S3 Forum"),             
                        "Subject" => array(Type::STRING => "My sample question"),
                        "Message"=> array(Type::STRING => "Message Text."),
                        "KeywordTags"=>array(Type::STRING_SET => array("S3", "Bucket"))
                    ))
            ),
            array(
                "DeleteRequest" => array(
                    "Key" => array(
                        "ForumName" =>array(Type::STRING => "Some hash value"),
                        "Subject" => array(Type::STRING => "Some range key")
                    ))
                )
         )
    )
));

?>