| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
The Scan method scans the entire table and you should therefore use queries
to retrieve information. To learn more about performance related to scan and query operations,
see Query and Scan Operations.
The following tasks guide you through scanning a table using the AWS SDK for NET low-level API:
Create an instance of the DynamoDbClient class (the
client).
Provide the parameters for the scan operation to the client
instance.
The only required parameter is the table name. You can set up a filter as part of the scan if you want to find a set of values that meet specific comparison results. You can limit the results to a subset to provide pagination of the results. Read results are eventually consistent.
Load the response into a local variable, such as $response, for
use in your application.
Consider the following Reply table that stores replies for various forum threads.
Reply ( Id, ReplyDateTime, Message, PostedBy )
The table maintains all the replies for various forum threads. Therefore, the primary key is made of both the Id (hash attribute) and ReplyDateTime (range attribute). The following PHP code snippet scans the table.
Note
This code example assumes that you have already loaded data into Amazon DynamoDB for your account by following the instructions in the Getting Started section. 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.
For step-by-step instructions to run the following example, see Running PHP Examples.
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Enum\Type;
// Instantiate the DynamoDB client with your AWS credentials
$aws = Aws\Common\Aws::factory("./config.php");
$client = $aws->get("dynamodb");
$response = $client->scan(array(
"TableName" => "Reply"
));
foreach ($response['Items'] as $key => $value) {
echo "Id: " . $value['Id'][Type::STRING] . PHP_EOL;
echo "ReplyDateTime: " . $value['ReplyDateTime'][Type::STRING] . PHP_EOL;
echo "Message: " . $value['Message'][Type::STRING] . PHP_EOL;
echo "PostedBy: " . $value['PostedBy'][Type::STRING] . PHP_EOL;
echo PHP_EOL;
}The scan operation response is a Guzzle\Service\Resource\Model object. You can perform operations on the object contents. For example, the following code snippet scans the ProductCatalog table, and prints the product Id and Title values.
$response = $client->scan(array(
"TableName" => "ProductCatalog"
));
foreach ($response['Items'] as $key => $value) {
echo "<p><strong>Item Number:</strong>". $value['Id'][Type::NUMBER] . "</p>";
echo "<br><strong>Item Name: </strong>". $value['Title'][Type::STRING] ."</p>";
}The scan function supports several optional parameters. For example, you
can optionally use a scan filter to filter the scan result. In a scan filter you specify
a condition and an attribute name on which you want the condition evaluated. For more
information about the parameters and the API, see Scan.
The following PHP code scans the ProductCatalog table to find items that are priced less than 0. The sample specifies the following optional parameters:
ScanFilter to retrieve only the items priced less than 0 (error
condition).
AttributesToGet to specify a list of attributes to retrieve for items
in the query results
The following PHP code snippet scans the ProductCatalog table to find all items priced less than 201.
$response = $client->scan(array(
"TableName" => "ProductCatalog",
"AttributesToGet" => array("Id"),
"ScanFilter" => array(
"Price" => array(
"ComparisonOperator" => ComparisonOperator::LT,
"AttributeValueList" => array(
array( Type::NUMBER => "201" )
)
),
)
));
foreach ($response['Items'] as $key => $value) {
echo "Id: " . $value['Id']['N'] . PHP_EOL;
echo PHP_EOL;
}You can also optionally limit the page size, the number of items per page, by adding
the optional Limit parameter. Each time you execute the scan
function, you get one page of results with a specified number of items. To fetch the
next page you execute the scan function again by providing primary key
value of the last item in the previous page so the scan function can return
the next set of items. You provide this information in the request by setting the
ExclusiveStartKey property. Initially this property can be null. For
retrieving subsequent pages you must update this property value to the primary key of
the last item in the preceding page.
The following PHP code snippet scans the ProductCatalog table. In the request it
specifies the Limit and ExclusiveStartKey optional
parameters.
$tableName = "ProductCatalog";
# The Scan API is paginated. Issue the Scan request multiple times.
do {
echo "Scanning table $tableName" . PHP_EOL;
$request = array(
"TableName" => $tableName,
"ScanFilter" => array(
"Price" => array(
"ComparisonOperator" => ComparisonOperator::LT,
"AttributeValueList" => array(
array(Type::NUMBER => "201")
)
)
),
"Limit" => 2
);
# Add the ExclusiveStartKey if we got one back in the previous response
if(isset($response) && isset($response['LastEvaluatedKey'])) {
$request['ExclusiveStartKey'] = $response['LastEvaluatedKey'];
}
$response = $client->scan($request);
foreach ($response['Items'] as $key => $value) {
echo "Id: " . $value['Id'][Type::NUMBER] . PHP_EOL;
echo "ProductCategory: " . $value['ProductCategory'][Type::STRING] . PHP_EOL;
echo "Title: " . $value['Title'][Type::STRING] . PHP_EOL;
echo "Price: " . $value['Price'][Type::NUMBER] . PHP_EOL;
echo PHP_EOL;
}
} while(isset($response['LastEvaluatedKey'])); # If there is no LastEvaluatedKey in the response, there are no more items matching this Scan invocationThe following PHP code example prepares sample data to be used by subsequent examples. The program deletes and then re-creates the ProductCatalog table, then loads the table with data.
Note
For step-by-step instructions to run these code examples, see Running PHP Examples.
<?php
require __DIR__ . '/vendor/autoload.php';
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Enum\KeyType;
use Aws\DynamoDb\Enum\Type;
// Instantiate the DynamoDB client with your AWS credentials
$aws = Aws\Common\Aws::factory("./config.php");
$client = $aws->get("dynamodb");
$tableName = "ProductCatalog";
// Delete an old DynamoDB table
echo "Deleting the table...\n";
$result = $client->deleteTable(array(
"TableName" => $tableName
));
$client->waitUntilTableNotExists(array("TableName" => $tableName));
echo "The table {$tableName} has been deleted.\n";
// Create a new DynamoDB table
echo "# Creating table $tableName..." . PHP_EOL;
$result = $client->createTable(array(
"TableName" => $tableName,
"AttributeDefinitions" => array(
array(
"AttributeName" => "Id",
"AttributeType" => Type::NUMBER
)
),
"KeySchema" => array(
array(
"AttributeName" => "Id",
"KeyType" => KeyType::HASH
)
),
"ProvisionedThroughput" => array(
"ReadCapacityUnits" => 5,
"WriteCapacityUnits" => 6
)
));
$client->waitUntilTableExists(array("TableName" => $tableName));
echo "Table {$tableName} has been created.\n";
// Populate DynamoDB table
echo "# Populating Items to $tableName...\n";
for ($i = 1; $i <= 100; $i++) {
$response = $client->putItem(array(
"TableName" => $tableName,
"Item" => array(
"Id" => array( Type::NUMBER => $i ), // Primary Key
"Title" => array( Type::STRING => "Book {$i} Title" ),
"ISBN" => array( Type::STRING => "111-1111111111" ),
"Price" => array( Type::NUMBER => 25 ),
"Authors" => array( Type::STRING_SET => array("Author1", "Author2") )
)
));
$response = $client->getItem(array(
"TableName" => "ProductCatalog",
"Key" => array(
"Id" => array( Type::NUMBER => $i )
)
));
echo "Item populated: {$response['Item']['Title']['S']}\n";
sleep(1);
}
echo "{$tableName} is populated with items.\n";
The following PHP code example performs a serial Scan on the ProductCatalog table.
Note
Before you run this program, you will need to populate the ProductTable with data. For more details, see Example - Loading Data Using PHP.
For step-by-step instructions to run these code examples, see Running PHP Examples.
<?php
require __DIR__ . '/vendor/autoload.php';
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Enum\Type;
// Instantiate the DynamoDB client with your AWS credentials
$aws = Aws\Common\Aws::factory("./config.php");
$client = $aws->get("dynamodb");
$tableName = "ProductCatalog";
$params = array(
"TableName" => $tableName,
"ScanFilter" => array(
"Title" => array(
"AttributeValueList" => array(
array(Type::STRING => "Book")
),
"ComparisonOperator" => "CONTAINS"
)
),
"Limit" => 10,
);
// Execute scan operations until the entire table is scanned
$count = 0;
do {
$response = $client->scan($params);
$items = $response->get("Items");
$count = $count + count($items);
// Do something with the $items
foreach($items as $item) {
echo "Scanned item with Title \"{$item['Title']['S']}\".\n";
}
// Set parameters for next scan
$params["ExclusiveStartKey"] = $response["LastEvaluatedKey"];
} while ($params["ExclusiveStartKey"]);
echo "{$tableName} table scanned completely. {$count} items found.\n";
The following Java code example demonstrates a parallel scan, running multiple Scan requests at the same time. Finally, the program prints a summary of run time statistics.
Note
Before you run this program, you will need to populate the ProductTable with data. For more details, see Example - Loading Data Using PHP.
For step-by-step instructions to run these code examples, see Running PHP Examples.
<?php
require __DIR__ . '/vendor/autoload.php';
use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Enum\Type;
// Instantiate the DynamoDB client with your AWS credentials
$aws = Aws\Common\Aws::factory("./config.php");
$client = $aws->get("dynamodb");
$tableName = "ProductCatalog";
$totalSegments = 5;
$params = array(
"TableName" => $tableName,
"ScanFilter" => array(
"Title" => array(
"AttributeValueList" => array(
array(Type::STRING => "Book")
),
"ComparisonOperator" => "CONTAINS"
)
),
"Limit" => 10,
"TotalSegments" => $totalSegments
);
// Create initial scan commands for each segment
$pendingScanCommands = array();
for ($segment = 0; $segment < $totalSegments; $segment++) {
$params["Segment"] = $segment;
$pendingScanCommands[] = $client->getCommand("Scan", $params);
}
// Execute scan operations in parallel until the entire table is scanned
while(count($pendingScanCommands) > 0) {
// Executes the 5 scan operations in parallel using cURL multi handles
$completedScanCommands = $client->execute($pendingScanCommands);
$pendingScanCommands = array();
// Process results of each scan command
foreach ($completedScanCommands as $scanCommand) {
$response = $scanCommand->getResult();
$segment = $scanCommand->getPath("Segment");
$items = $response->get("Items");
// Do something with the items
foreach($items as $item) {
echo "Scanned item with Title \"{$item['Title']['S']}\" and Segment \"{$segment}\".\n";
}
// If LastEvaluatedKey is present we should continue scanning this segment
// Otherwise we've reached to end of this segment
if ($response["LastEvaluatedKey"]) {
echo "LastEvaluatedKey found creating new scan command for Segment: {$segment}.\n";
$params["Segment"] = $segment;
$params["ExclusiveStartKey"] = $response["LastEvaluatedKey"];
$pendingScanCommands[] = $client->getCommand("Scan", $params);
} else {
echo "Segment: {$segment} scanned completely!\n";
}
}
}
echo "Table {$tableName} scanned completely.\n";