Here is an example for Java. In withAttributesToGet you specify what exactly you want to read. Before run you have to place credential file to your .aws folder.
public static final String TABLE_NAME = "table_name";
public static final AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.CA_CENTRAL_1)
.build();
public static void main(String[] args) throws IOException, InterruptedException {
downloadAllRecords();
}
public static void downloadAllRecords() throws InterruptedException, IOException {
final Object[] FILE_HEADER = {"key", "some_param"};
CSVFormat csvFormat = CSVFormat.DEFAULT.withRecordSeparator("\n");
CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(TABLE_NAME + ".csv"), csvFormat);
csvPrinter.printRecord(FILE_HEADER);
ScanRequest scanRequest = new ScanRequest()
.withTableName(TABLE_NAME)
.withConsistentRead(false)
.withLimit(100)
.withAttributesToGet("key", "some_param");
int counter = 0;
do {
ScanResult result = client.scan(scanRequest);
Map<String, AttributeValue> lastEvaluatedKey = result.getLastEvaluatedKey();
for (Map<String, AttributeValue> item : result.getItems()) {
AttributeValue keyIdAttribute = item.getOrDefault("key", new AttributeValue());
AttributeValue createdDateAttribute = item.getOrDefault("some_param", new AttributeValue());
counter++;
List record = new ArrayList();
record.add(keyIdAttribute.getS());
record.add(createdDateAttribute.getS());
csvPrinter.printRecord(record);
TimeUnit.MILLISECONDS.sleep(50);
}
scanRequest.setExclusiveStartKey(lastEvaluatedKey);
} while (scanRequest.getExclusiveStartKey() != null);
csvPrinter.flush();
csvPrinter.close();
System.out.println("CSV file generated successfully.");
}
Also specify necessary dependencies.
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sparkjava/spark-template-velocity -->
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-velocity</artifactId>
<version>2.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-logs -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-logs</artifactId>
<version>1.12.132</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-dynamodb -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.12.161</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
Example of credential file
[default]
aws_access_key_id = AAAAAAA
aws_secret_access_key = AAAAAAAA
aws_session_token = AAAAAAA