iteration over dynamodb partition keys

576 Views Asked by At

I am using AWS.DynamoDB.DocumentClient. I want to iterate over the items and conditionally update them.

I have a table which contains 4000 items. When I scan the table, even if I use ProjectionExpression, I get only 480 results. This is because of scan size limit (1 MB). I'm pretty sure if I get only partition keys, it will be less than 1 MB.

There are some similar questions about scanning specific items. But that's not what I struggle. What can I do to list all partition keys of my table? Thanks.

Here is my scan operation;

docClient.scan({
  TableName: "Recipes",
  "ProjectionExpression": "#key",
  "ExpressionAttributeNames": {
    "#key": "id"
   }
 }, async (err, recipes) => {
   console.log("scanned recipes:" + recipes.Items.length)
   //output: 477 (but the list have 4000 items)
 }
2

There are 2 best solutions below

3
On BEST ANSWER

Can you show the scan operation you've tried but isn't working for you?

The following worked for me (my partition key is named PK)


ddbClient.scan(
  {
    "TableName": "<MY TABLE NAME>",
    "ProjectionExpression": "#PK,
    "ExpressionAttributeNames": {
      "#PK": "PK"
    }
  }
)

Keep in mind that DynamoDB will consider the entire item size when calculating the 1MB limit, even if you use a projection expression that limits the response to just a few attributes. If your scan result returns a LastEvaluatedKey, you know that DynamoDB is paginating the results.

0
On

I found the solution on documentary. ExclusiveStartKey is the answer. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.04.html