Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fetch all items from a DynamoDB table without specifying the primary key?

I have a table called products with primary key Id. I want to select all items in the table. This is the code is I'm using:

$batch_get_response = $dynamodb->batch_get_item(array(     'RequestItems' => array(          'products' => array(             'Keys' => array(                 array( // Key #1                     'HashKeyElement'  => array( AmazonDynamoDB::TYPE_NUMBER => '1'),                     'RangeKeyElement' => array( AmazonDynamoDB::TYPE_NUMBER => $current_time),                 ),                 array( // Key #2                     'HashKeyElement'  => array( AmazonDynamoDB::TYPE_NUMBER => '2'),                     'RangeKeyElement' => array( AmazonDynamoDB::TYPE_NUMBER => $current_time),                 ),             )         )     )    )); 

Is it possible to select all items without specifying the primary key? I'm using the AWS SDK for PHP.

like image 544
Warrior Avatar asked May 04 '12 14:05

Warrior


People also ask

Can we query DynamoDB without primary key?

Hash key in DynamoDB The primary reason for that complexity is that you cannot query DynamoDB without the hash key. So, it's not allowed to query the entire database. That means you cannot do what you would call a full table scan in other databases.

How do I get items from DynamoDB table?

To read an item from a DynamoDB table, use the GetItem operation. You must provide the name of the table, along with the primary key of the item you want. The following AWS CLI example shows how to read an item from the ProductCatalog table. With GetItem , you must specify the entire primary key, not just part of it.

Does DynamoDB query return all items?

The Query operation in Amazon DynamoDB finds items based on primary key values. You must provide the name of the partition key attribute and a single value for that attribute. Query returns all items with that partition key value.

Which is the most efficient operation to retrieve data from a DynamoDB table?

GetItem – Retrieves a single item from a table. This is the most efficient way to read a single item because it provides direct access to the physical location of the item. (DynamoDB also provides the BatchGetItem operation, allowing you to perform up to 100 GetItem calls in a single operation.)


1 Answers

Amazon DynamoDB provides the Scan operation for this purpose, which returns one or more items and its attributes by performing a full scan of a table. Please be aware of the following two constraints:

  • Depending on your table size, you may need to use pagination to retrieve the entire result set:

    Note
    If the total number of scanned items exceeds the 1MB limit, the scan stops and results are returned to the user with a LastEvaluatedKey to continue the scan in a subsequent operation. The results also include the number of items exceeding the limit. A scan can result in no table data meeting the filter criteria.

    The result set is eventually consistent.

  • The Scan operation is potentially costly regarding both performance and consumed capacity units (i.e. price), see section Scan and Query Performance in Query and Scan in Amazon DynamoDB:

    [...] Also, as a table grows, the scan operation slows. The scan operation examines every item for the requested values, and can use up the provisioned throughput for a large table in a single operation. For quicker response times, design your tables in a way that can use the Query, Get, or BatchGetItem APIs, instead. Or, design your application to use scan operations in a way that minimizes the impact on your table's request rate. For more information, see Provisioned Throughput Guidelines in Amazon DynamoDB. [emphasis mine]

You can find more details about this operation and some example snippets in Scanning Tables Using the AWS SDK for PHP Low-Level API for Amazon DynamoDB, with the most simple example illustrating the operation being:

$dynamodb = new AmazonDynamoDB();  $scan_response = $dynamodb->scan(array(     'TableName' => 'ProductCatalog'  ));  foreach ($scan_response->body->Items as $item) {     echo "<p><strong>Item Number:</strong>"          . (string) $item->Id->{AmazonDynamoDB::TYPE_NUMBER};     echo "<br><strong>Item Name: </strong>"          . (string) $item->Title->{AmazonDynamoDB::TYPE_STRING} ."</p>"; } 
like image 90
Steffen Opel Avatar answered Sep 28 '22 16:09

Steffen Opel