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

Carvia Tech | September 19, 2019 | 1 min read | 1,505 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 SDK 2: SQS Object Operations using Spring Boot
  2. AWS SDK 1.x - file download and upload from S3 bucket
  3. Invoking AWS Lambda from a Java Client
  4. Invoke AWS Lambda from a Kotlin Client
  5. How to implement Atomic Counters in DynamoDB for high throughput
  6. Creating 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. AWS DynamoDB Java interview questions
  2. How to implement Atomic Counters in DynamoDB for high throughput
  3. How will you ensure that no two threads update the same db record in parallel in amazon DynamoDB
  4. How to automatically Retry DynamoDB Write on ProvisionedThroughputExceededException
  5. What are Best Practices for Using Amazon DynamoDB?
  6. What are Conditional Writes in AWS DynamoDB
  7. What is Eventual Consistency in DynamoDB?

Find more on this topic:
NoSQL image

No SQL design and development - Amazon DynamoDB, MongoDB, MySQL document DB.

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info