Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDb : Scan query does not return all the data

I have a DynamoDb table with thousands of data. I am scanning the table using Scan function and I have applied "Between" FilterExpression. However , the query response only gives 3 records whereas it should return about 100 records.

I have created the Lambda function using Node js.

like image 486
Sumera Avatar asked Apr 14 '17 11:04

Sumera


People also ask

Does DynamoDB Scan return all items?

A Scan operation in Amazon DynamoDB reads every item in a table or a secondary index. By default, a Scan operation returns all of the data attributes for every item in the table or index. You can use the ProjectionExpression parameter so that Scan only returns some of the attributes, rather than all of them.

What does DynamoDB Scan return?

The Scan operation returns one or more items and item attributes by accessing every item in a table or a secondary index. To have DynamoDB return fewer items, you can provide a FilterExpression operation.

What is the maximum limit of data that can be retrieved by a Scan operation in DynamoDB?

Scan basics DynamoDB has a 1MB limit on the amount of data it will retrieve in a single request. Scans will often hit this 1MB limit if you're using your table for real use cases, which means you'll need to paginate through results.

What order does DynamoDB Scan return?

From the documentation: Query results are always sorted by the range key. If the data type of the range key is Number , the results are returned in numeric order. Otherwise, the results are returned in order of UTF-8 bytes.


2 Answers

The other common issue could be whether the scan is executed until LastEvaluatedKey is empty.

If you are already doing this and still not getting all the items, please show your code to look at it in detail.

If the total number of scanned items exceeds the maximum data set size limit of 1 MB, the scan stops and results are returned to the user as a LastEvaluatedKey value 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.

If LastEvaluatedKey is empty, then the "last page" of results has been processed and there is no more data to be retrieved.

If LastEvaluatedKey is not empty, it does not necessarily mean that there is more data in the result set. The only way to know when you have reached the end of the result set is when LastEvaluatedKey is empty.

like image 178
notionquest Avatar answered Oct 02 '22 07:10

notionquest


Here's example code to get all results:

 Map<String, AttributeValue> lastKeyEvaluated = null;
    do {
        ScanRequest sr = new ScanRequest()
                .withTableName("tableName")
                .withProjectionExpression("id")
                .withExclusiveStartKey(lastKeyEvaluated);
        ScanResult result = client.scan(sr);
        for (Map<String, AttributeValue> item : result.getItems()) {
            System.out.println(item.get("id").getS());
        }
        lastKeyEvaluated = result.getLastEvaluatedKey();
    } while (lastKeyEvaluated != null);
like image 24
Michael Avatar answered Oct 02 '22 08:10

Michael