承诺的 DynamoDB 不会保存 Promise.map 中循环的所有项目

Promised DynamoDB doesn't save all the items cycled in a Promise.map

我在 DynamoDB 中保存多个项目时遇到问题。我的代码结构如下:

var Promise = require("bluebird");
var AWS = require('aws-sdk');

//config promise environment for AWS SDK
AWS.config.setPromisesDependency(require("bluebird"));
AWS.config.update({
    accessKeyId: "*",
    secretAccessKey: "*",
    region: 'eu-west-1'
});

var dynamodb = new AWS.DynamoDB.DocumentClient();

var arrayReading = []; //array of element we are going to save
for (var i = 0; i < 10; i++) { //filling the array
    var reading = {
        "device": null,
        "timestamp": new Date().toISOString(),
        "installation": "1",
        "values": []
    };
    console.log("creation: DEV" + i);
    reading.device = "DEV" + i;
    arrayReading.push(reading);
}
console.log(arrayReading.length);
Promise.map(arrayReading, function (reading) {
    console.log("saving device", reading.device);
    var params = {
        "TableName": "readings-test",
        "Item": reading
    };

    return dynamodb.put(params).promise() //saving
        .catch(function (err, data) {
            console.log("Error:",err);
        });
}).then(function () {
    console.log("done")
});

我得到的控制台日志输出是这样的:

creation: DEV0
[...]
creation: DEV9
10
saving device DEV0
[...]
saving device DEV9
done

所以我假设所有元素都已保存。相反,当我去检查我的 DynamoDB 时 table 我看到我的元素(3 到 6)中只有一个随机子集被保存了。

我做错了什么?我尝试提高 table 的 write/read 容量,但这并没有解决问题。

问题出在 primary/sort 键配置中。

我的主键是安装,排序键是时间戳。这导致一组快速异步写入具有相同的时间戳,覆盖了之前的条目。