使用Node JS递归获取DynamoDB查询中的所有项目 [英] Recursive Fetch All Items In DynamoDB Query using Node JS

查看:255
本文介绍了使用Node JS递归获取DynamoDB查询中的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个JS /异步问题,而不是DynamoDB特定问题-

This is probably more of an JS/Async question than a DynamoDB specific question -

我想在亚马逊的数据库中使用哈希键获取表中的所有项目DynamoDB。该表中还包含Range键。

I want to fetch all the items in a table with a hash key in Amazon's DynamoDB. The table also has Range key in it.

我正在使用NodeJS库,该库是AWS DynamoDB REST API的包装。 -
Node-DynamoDB

I am using a NodeJS library which is a wrapper around AWS DynamoDB REST API. - Node-DynamoDB

DynamoDB仅返回每个查询价值1 MB的结果。要获取结果提醒,它包括 lastEvaluatedKey 。我们可以将其包含在另一个查询中,以获取另外1 MB的结果,依此类推...

DynamoDB only returns 1 MB worth of results with each query. To fetch reminder of results, it includes lastEvaluatedKey . We can include this in another query to fetch another 1 MB worth of results and so on...

我在编写递归异步函数时遇到了困难,该函数应该可以依次提供服务,直到我可以将所有结果取回。 (对于我的用例,表永远不会超过10 MB,不可能有失控的查询)

I am facing difficulty in writing a recursive async function which should hit the service sequentially till i can get all the results back. (table will never have more than 10 MB for my use case, no chance of a runaway query)

一些用于说明的伪代码:

Some pseudo code for illustration:

ddb.query('products', primarykey, {}, function(err,result){
    //check err
    if(result && result.lastEvaluatedKey){
        //run the query again
        var tempSet = result.items;
        //temporarily store result.items so we can continue and fetch remaining items.
    }
    else{
        var finalSet = result.items;
        //figure out how to merge with items that were fetched before.
    }
});


推荐答案

var getAll = function(primarykey, cb) {
    var finalSet = [],
        nextBatch = function(lek) {
            ddb.query('products', primarykey, {
                exclusiveStartKey: lek
            }, function(err, result) {
                if (err) return cb(err);

                if (result.items.length)
                    finalSet.push.apply(finalSet, result.items);

                if (result.lastEvaluatedKey)
                    nextBatch(result.lastEvaluatedKey);
                else
                    cb(null, finalSet);
            });
        };

    nextBatch();
};


getAll(primarykey, function(err, all) {
    console.log(err, all);
});

这篇关于使用Node JS递归获取DynamoDB查询中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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