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

Carvia Tech | September 19, 2019 | 1 min read | 214 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.

DownloadRecords.java
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);
        csvPrinter.printRecord(FILE_HEADER);

        ScanRequest scanRequest = new ScanRequest()
                .withTableName(tableName)
                .withConsistentRead(false)
                .withLimit(100)
                .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();
                System.out.println(item);
                AttributeValue userIdAttribute = item.getOrDefault("userId", new AttributeValue());
                AttributeValue idAttribute = item.getOrDefault("id", new AttributeValue());
                AttributeValue createdDateAttribute = item.getOrDefault("createdDate", new AttributeValue());
                record.add(idAttribute.getS());
                record.add(userIdAttribute.getS());
                record.add(createdDateAttribute.getS());
                csvPrinter.printRecord(record);
                TimeUnit.MILLISECONDS.sleep(50);
            }
            scanRequest.setExclusiveStartKey(lastEvaluatedKey); (1)
        } while (scanRequest.getExclusiveStartKey() != null);
        csvPrinter.flush();
        csvPrinter.close();
        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. Python: Send event from AWS Lambda to AWS SQS
  2. Invoke AWS Lambda from a Kotlin Client
  3. AWS SDK 2.0 - S3 Object Operations using Spring Boot
  4. Invoking AWS Lambda from a Java Client
  5. Creating AWS Lambda in Kotlin using Spring Cloud Function
  6. How to implement Atomic Counters in DynamoDB for high throughput
  7. Creating AWS Lambda using python 3.6
See all articles in AWS Tutorials
Top articles in this category:
  1. How to implement Atomic Counters in DynamoDB for high throughput
  2. How will you ensure that no two threads update the same db record in parallel in amazon DynamoDB
  3. Amazon DynamoDB Java Interview Questions
  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
NoSQL

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