【问题标题】:unable to get all items using query DynamoDB无法使用查询 DynamoDB 获取所有项目
【发布时间】: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


【解决方案1】:

这对我有用,从这里遵循 Hank 解决方案:How to fetch/scan all items from `AWS dynamodb` using node.js

exports.handler = async (event, context, callback) => {
    let params = {
   ExpressionAttributeNames: {
    "#deviceid": "id",
    "#devicetimestamp": "timestamp"
  },
  ExpressionAttributeValues: {
    ':unitID': {S: event.deviceid },
    ':ftimestamp' : {S: event.fromtime },
    ':ttimestamp' : {S: event.totime }
   },
 KeyConditionExpression: '#deviceid = :unitID and #devicetimestamp BETWEEN :ftimestamp and :ttimestamp',
 TableName: 'IoTdata2'
};

    let scanResults = [];
    let items;
    do {
        items = await b.query(params).promise();
        items.Items.map((item) => scanResults.push(unmarshalItem(item)));
        params.ExclusiveStartKey = items.LastEvaluatedKey;
    } while (typeof items.LastEvaluatedKey != "undefined");
    var len = scanResults.length;
    console.log(len);
    callback(null, scanResults);
};

【讨论】:

  • 干杯,非常优雅的解决方案
猜你喜欢
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多