【问题标题】:Bulk Upsert using DynamoDB使用 DynamoDB 批量更新插入
【发布时间】:2020-12-01 00:04:29
【问题描述】:

是否可以在 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;
}

【问题讨论】:

    标签: node.js mongodb amazon-dynamodb nestjs


    【解决方案1】:

    你有两个选择:

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

    【讨论】:

      猜你喜欢
      • 2022-12-21
      • 1970-01-01
      • 2011-09-29
      • 2011-04-15
      • 2020-12-13
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      相关资源
      最近更新 更多