如何使用 node.js 从`AWS dynamodb` 获取/扫描所有项目 [英] How to fetch/scan all items from `AWS dynamodb` using node.js

查看:41
本文介绍了如何使用 node.js 从`AWS dynamodb` 获取/扫描所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 node.jsAWS dynamodb 获取/扫描所有项目.我在这里发布我的代码.

How to fetch/scan all items from AWS dynamodb using node.js. I am posting my code here.

var docClient = new aws.DynamoDB.DocumentClient();
    var params = {
    TableName:"users",
    KeyConditionExpression:"user_status=:status",
    ExpressionAttributeValues: {
        ":status": "Y"
    }
    };

    var queryExecute = function(callback) {
        docClient.query(params,function(err,result) {
            if(err) {
                console.log(err)
                callback(err);
                } else {
                console.log(result);

                if(result.LastEvaluatedKey) {
                    params.ExclusiveStartKey = result.LastEvaluatedKey;
                    queryExecute(callback);
                    } else {
                        callback(err,items);
                    }
                }
            });
        }
        queryExecute(callback); 

这给了我以下错误.

ValidationException: Query condition missed key schema element: `user_id`.

这里的主键是 user_id.我不想在我的查询条件中使用它,因为如果我在 KeyConditionExpression 中提到了主键,我需要设置一个值.可能是我错了.但是,请建议我从 dynamodb 获取所有项目的好方法,它具有 user_status = "Y"

Here primary key is user_id. I don't want to use it with my query condition, because I need to set a value if I mentioned primary key in KeyConditionExpression. May be I am wrong. However please suggest me a good way to fetch all items from dynamodb, which is having user_status = "Y"

推荐答案

如果您想从 DynamoDB 获取数据而不使用 Hash 键值,则需要使用 Scan API.

If you would like to get the data from DynamoDB without using Hash key value, you need to use Scan API.

注意: Scan API 读取表中的所有项目以获取结果.因此,这是 DynamoDB 中的一项代价高昂的操作.

Note: The Scan API reads all the items in the table to get the results. So, it is a costly operation in DynamoDB.

替代方法:使用 GSI

以上场景扫码:-

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

var params = {
    TableName: "users",
    FilterExpression: "#user_status = :user_status_val",
    ExpressionAttributeNames: {
        "#user_status": "user_status",
    },
    ExpressionAttributeValues: { ":user_status_val": 'somestatus' }

};

docClient.scan(params, onScan);
var count = 0;

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.");
        data.Items.forEach(function(itemdata) {
           console.log("Item :", ++count,JSON.stringify(itemdata));
        });

        // continue scanning if we have more items
        if (typeof data.LastEvaluatedKey != "undefined") {
            console.log("Scanning for more...");
            params.ExclusiveStartKey = data.LastEvaluatedKey;
            docClient.scan(params, onScan);
        }
    }
}

这篇关于如何使用 node.js 从`AWS dynamodb` 获取/扫描所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆