如何解决使用DynamoDB的批量上载
是否可以在DynamoDB中对项目进行批量增补,就像MongoDB批量写入可以做到这一点一样?
我在MongoDB中具有此功能,希望在DynamoDB中做类似的事情:
async bulkSync(
model: any | null,context: any[],filterKeys: Array<string> = ['_id'],): Promise<any> {
let operations = []; // Initialize the bulk operations array
let i = 1;
for (const record of context) {
const filter: any = {};
for (const key of filterKeys) {
const path = key.split('.');
const value = R.path(path,record);
filter[key] = value;
}
operations.push({
updateOne: {
filter: filter,update: {
$set: record,$setOnInsert: {
created_at: record.updated_at,},upsert: true,// example update operation,setDefaultsOnInsert: true,});
if (operations.length % 500 === 0 || i === context.length) {
await model.collection.bulkWrite(operations,{
ordered: true,w: 1,});
operations = [];
}
i++;
}
return;
}
解决方法
您有两个选择:
- 使用BatchWrite。在这种情况下,DynamoDB本质上将为您处理并发性,并行运行多个单独的写入请求。在处理任何失败的请求时,这里会有一些开销。
- 使用PutItem请求,并使用node worker_threads模块在客户端实现并发。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。