【发布时间】:2020-02-27 22:41:55
【问题描述】:
这里有点Node新手...
我正在尝试编写一个函数,从 S3 中提取 CSV 并将项目批量写入 DynamoDB。 DynamoDB 在每批中限制为 25 个,因此我需要随时编写条目。我遇到的问题是我的await 执行数据库写入的函数只在.end() 触发,而不是在我检查时触发。
我知道我无法执行这样的操作,但我不确定如何解决?我正在使用 Node12。
谢谢。
async function populateTable(
dataFile: bucketKey,
tableName: string
): Promise<void> {
const s3 = getS3Client();
const stream = s3.getObject(dataFile).createReadStream();
const BATCH_COUNT = 25; // Max size to write to DynamoDB
let counter = 0;
let datarows: any = [];
let datarow = {};
stream
.pipe(parse(DATA_HEADERS))
.on("data", async function(data: DataRow) {
counter++;
datarow = {
PutRequest: {
Item: data
}
};
datarows.push(datarow);
if (counter % BATCH_COUNT === 0) {
console.log("before batch write " + counter); // This fires!
await batchWriteToDynamo(datarows, tableName); // I want this function to fully execute before moving on
console.log("after batch write " + counter); // This does not
datarows = [];
}
})
.on("end", async function() {
await batchWriteToDynamo(datarows, tableName); // This fires!
});
}
【问题讨论】:
-
batchWriteToDynamo()的代码在哪里?另外,我们在这里讨论的项目总共有多少?
标签: javascript typescript amazon-dynamodb