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.");
}
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
DownloadRecords.java
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:
- AWS Lambda Interview Questions for Developers
- AWS SDK 1.x - S3 file download & upload
- Python: Send event from AWS Lambda to AWS SQS
- AWS SDK 2: SQS Object Operations using Spring Boot
- S3 File upload & download with AWS Java SDK v2
- AWS Lambda in Kotlin using Spring Cloud Function
- Creating AWS Lambda using python 3.6
Top articles in this category:
- How will you ensure that no two threads update the same db record in parallel in amazon DynamoDB
- AWS DynamoDB Java interview questions
- What are Best Practices for Using Amazon DynamoDB?
- How to implement Atomic Counters in DynamoDB for high throughput
- How to automatically Retry DynamoDB Write on ProvisionedThroughputExceededException
- What are different types of NoSql databases in general?
- What are Conditional Writes in AWS DynamoDB
Recommended books for interview preparation:
Book you may be interested in..
Book you may be interested in..