【发布时间】:2020-06-24 18:21:36
【问题描述】:
如何使用 node.js 从 AWS dynamodb 扫描所有项目。我在这里发布我的代码。
//Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
var unmarshalItem = require('dynamodb-marshaler').unmarshalItem;
// Set the region
AWS.config.update({region: 'us-east-1'});
// Create DynamoDB service object
var b = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = (event, context, callback) => {
var params = {
TableName: 'IoTdata2',
FilterExpression: "#deviceid = :unitID and #devicetimestamp BETWEEN :ftimestamp and :ttimestamp",
ExpressionAttributeNames: {
"#deviceid": "id",
"#devicetimestamp": "timestamp"
},
ExpressionAttributeValues: {
':unitID': {S: 'arena-MXHGMYzBBP5F6jztnLUdCL' },
':ftimestamp' : {S: '1584022680000' },
':ttimestamp' : {S: '1584023280000' }
},
};
b.scan(params, onScan);
function onScan(err, data) {
if (err) {
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Scan succeeded.");
var items = data.Items.map(function(val){
return unmarshalItem(val);
})
// continue scanning if we have more items
if (typeof data.LastEvaluatedKey != "undefined") {
console.log("Scanning for more...");
params.ExclusiveStartKey = data.LastEvaluatedKey;
b.scan(params, onScan);
}
}
callback(null, items);
}
};
我已经点击链接https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.04.html
一段时间后,我将在这里抽出时间。我也检查了这个链接 How to fetch/scan all items from `AWS dynamodb` using node.js 我想我无法正确返回数据,有什么建议吗?谢谢
【问题讨论】:
-
在最后评估的键处重新运行扫描的迭代解决方案将比这种递归方法更好。累积所有项目,然后将它们全部解组将是我的建议。这是假设您确实需要同时将所有项目读入内存(如果是大表,则不理想)。根据需要实施退避/重试,或评估您的 DynamoDB 表支持执行此完整扫描所需的 RCU 的能力。
-
看起来你正在使用 lambda。确保延长执行时间并将 async, await 添加到您的函数中。
标签: node.js amazon-web-services amazon-dynamodb