更新 DynamoDB 中的多条记录 [英] Updating multiple records in DynamoDB
问题描述
如何在单个查询中更新 DynamoDB 中的多条记录?我有一个 csv 文件作为基于 csv 文件的输入,我必须更新数据库中的多条记录(只有一个属性).有没有可用的API?或者这可以使用批处理(Spring-batch)来完成?
How can i update multiple records in DynamoDB in single query? I have a csv file as an input based on the csv file I have to update multiple records(only one attribute) in the DB. Is there any API available for this? or This can be done using batch processing(Spring-batch)?
推荐答案
DynamoDB 没有直接的 batchUpdate
API.它确实有 batch get item
和 batch write item
API.
DynamoDB doesn't have batchUpdate
API directly. It does have batch get item
and batch write item
API.
但是,您可以使用 batchWriteItem
API 来更新项目.
However, you can use batchWriteItem
API to update the item.
1) 使用下面的 BatchWriteItemSpec
类来构造请求
1) Use the below BatchWriteItemSpec
class to construct the request
2) 使用下面的 TableWriteItems
类来构造需要更新的项目
2) Use the below TableWriteItems
class to construct the item that need to be updated
3) 使用下面的addItemToPut
方法(或withItemsToPut
)添加新属性
3) Use the below addItemToPut
method (or withItemsToPut
) to add the new attribute
如果项目(即分区键)不可用,batchWriteItem
API 会创建一个新项目.如果分区键可用,它将更新现有项目.您的用例属于此类别.
The batchWriteItem
API creates a new item if the item (i.e. partition key) is not available. If the partition key is available, it will update the existing item. Your use case falls under this category.
代码示例:-
files - 是表名
fileName - 是分区键
transcriptionnew - 添加新属性(或者也可以更新现有属性值)
transcriptionnew - Adding the new attribute (or can update the existing attribute value as well)
DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
Item itemUpdate1 = new Item();
itemUpdate1.withKeyComponent("fileName", "file1")
.withString("transcriptionnew", "new value");
Item itemUpdate2 = new Item();
itemUpdate2.withKeyComponent("fileName", "file2")
.withString("transcriptionnew", "new value");
TableWriteItems tableWriteItems = new TableWriteItems("files").withItemsToPut(itemUpdate1, itemUpdate2);
BatchWriteItemSpec batchWriteItemSpec = new BatchWriteItemSpec().withTableWriteItems(tableWriteItems);
BatchWriteItemOutcome batchWriteItemOutCome = dynamoDB.batchWriteItem(batchWriteItemSpec);
if (batchWriteItemOutCome.getUnprocessedItems().isEmpty()) {
//All items are processed
return true;
}
这篇关于更新 DynamoDB 中的多条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!