我正在尝试将csv文件数据插入到dynamodb中,但我只能在我的dynamodb表中写入1537条记录。
编码
try {
S3EventNotificationRecord record = s3event.getRecords().get(0);
String srcBucket = record.getS3().getBucket().getName();
String srcKey = record.getS3().getObject().getKey().replace('+', ' ');
srcKey = URLDecoder.decode(srcKey, "UTF-8");
AmazonS3 s3Client = new AmazonS3Client();
S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, srcKey));
statusReport.setFileSize(s3Object.getObjectMetadata().getContentLength());
BufferedReader br = new BufferedReader(new InputStreamReader(s3Object.getObjectContent()));
CSVReader reader = new CSVReader(br);
AmazonDynamoDB dynamoDBClient = new AmazonDynamoDBClient();
dynamoDBClient.setRegion(AWS_REGION);
DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
TableWriteItems energyDataTableWriteItems = new TableWriteItems(DYNAMO_TABLE_NAME);
List<Item> itemList = new ArrayList<Item>();
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
Item newItem = helper.parseIt(nextLine);
itemList.add(newItem);
}
for (List<Item> partition : Lists.partition(itemList, 25)) {
energyDataTableWriteItems.withItemsToPut(partition);
BatchWriteItemOutcome outcome = dynamoDB.batchWriteItem(energyDataTableWriteItems);
do {
Map<String, List<WriteRequest>> unprocessedItems = outcome.getUnprocessedItems();
if (outcome.getUnprocessedItems().size() > 0) {
logger.log("Retrieving the unprocessed " + String.valueOf(outcome.getUnprocessedItems().size())
+ " items.");
outcome = dynamoDB.batchWriteItemUnprocessed(unprocessedItems);
}
} while (outcome.getUnprocessedItems().size() > 0);*/
}
logger.log("Load finish in " + String.valueOf(System.currentTimeMillis() - startTime) + "ms");
reader.close();
br.close();
s3Object.close();
statusReport.setStatus(true);
} catch (Exception ex) {
logger.log(ex.getMessage());
}
statusReport.setExecutiongTime(System.currentTimeMillis() - startTime);
return statusReport;
}
我能知道它的行为是这样的吗?我尝试了超过5个表,但我得到了相同的结果。
答案 0 :(得分:0)
您可能遇到了吞吐量限制:
来自docs:
BatchWriteItem操作在一个或多个表中放置或删除多个项目。对BatchWriteItem的单次调用最多可以写入16 MB的数据,最多可包含25个put或delete请求。要写入的单个项目可以大到400 KB。
此外:
如果DynamoDB返回任何未处理的项目,您应该对这些项目重试批处理操作。
当您将写入分区为25个项目块时,这不是唯一的限制。如果您查看BatchWriteItemOutcome
的JavaDoc,您会看到函数getUnprocessedItems()
。
最后,你没有显示你的catch
块,但我们可以假设它确实有效吗?