使用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;
}

解决方法

您有两个选择:

  1. 使用BatchWrite。在这种情况下,DynamoDB本质上将为您处理并发性,并行运行多个单独的写入请求。在处理任何失败的请求时,这里会有一些开销。
  2. 使用PutItem请求,并使用node worker_threads模块在客户端实现并发。