Scan all records of a Amazon DynamoDB table using a Java Code

Upasana | September 19, 2019 | 1 min read | 1,966 views | AWS Tutorials

Amazon DynamoDB Record Scanning

Sometimes we need to download all records from a remote dynamod db table for backup purpose. Amazon DynamoDB provides mechanism to scan and retrieve all records using ScanRequest.
public void downloadAllRecords() throws InterruptedException, IOException {
        final Object[] FILE_HEADER = {"id", "userId", "createdDate"};
        CSVFormat csvFormat = CSVFormat.DEFAULT.withRecordSeparator("\n");
        CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(tableName + ".csv"), csvFormat);

        ScanRequest scanRequest = new ScanRequest()
                .withAttributesToGet("id", "userId", "createdDate");
        int counter = 0;
        do {
            ScanResult result = client.scan(scanRequest);
            Map<String, AttributeValue> lastEvaluatedKey = result.getLastEvaluatedKey();
            for (Map<String, AttributeValue> item : result.getItems()) {
                List record = new ArrayList();
                AttributeValue userIdAttribute = item.getOrDefault("userId", new AttributeValue());
                AttributeValue idAttribute = item.getOrDefault("id", new AttributeValue());
                AttributeValue createdDateAttribute = item.getOrDefault("createdDate", new AttributeValue());
            scanRequest.setExclusiveStartKey(lastEvaluatedKey); (1)
        } while (scanRequest.getExclusiveStartKey() != null);
        System.out.println("CSV file generated successfully.");
1 The primary key of the first item that this operation will evaluate. Use the value that was returned for LastEvaluatedKey in the previous operation.

AWS Tutorials:
  1. AWS Lambda Interview Questions for Developers
  2. AWS SDK 1.x - S3 file download & upload
  3. Python: Send event from AWS Lambda to AWS SQS
  4. AWS SDK 2: SQS Object Operations using Spring Boot
  5. S3 File upload & download with AWS Java SDK v2
  6. AWS Lambda in Kotlin using Spring Cloud Function
  7. Creating AWS Lambda using python 3.6
See all articles in AWS Tutorials
Top articles in this category:
  1. How will you ensure that no two threads update the same db record in parallel in amazon DynamoDB
  2. AWS DynamoDB Java interview questions
  3. What are Best Practices for Using Amazon DynamoDB?
  4. How to implement Atomic Counters in DynamoDB for high throughput
  5. How to automatically Retry DynamoDB Write on ProvisionedThroughputExceededException
  6. What are different types of NoSql databases in general?
  7. What are Conditional Writes in AWS DynamoDB

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.