递归获取所有项目在DynamoDB使用节点JS

递归获取所有项目在DynamoDB使用节点JS

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

问题描述

这是可能更多的JS /异步的问题不是DynamoDB具体的问题 -

我想获取与亚马逊DynamoDB散列关键字表中的所有项目。该表也有它的范围键。

我使用的是NodeJS库,它是围绕AWS DynamoDB REST API的包装。 -节点DynamoDB

DynamoDB只返回1 MB值得每个查询的结果。若要获取结果的提醒,它包括的 lastEvaluatedKey 。我们可以将此在另一个查询获取另一1 MB价值的结果等等...

我在写一个递归异步函数,它应该打的服务顺序,直到我可以拿回所有的结果遇到困难。 (表将永远不会有超过10 MB的我的使用情况下,没有机会失控查询)

一些伪$ C $下图:

  ddb.query(产品,PrimaryKey的,{},功能(错了,结果){
    //检查ERR
    如果(结果和安培;&安培; result.lastEvaluatedKey){
        //再次运行查询
        VAR TEMPSET = result.items;
        //临时存储result.items,所以我们可以继续获取剩余项目。
    }
    其他{
        VAR finalSet = result.items;
        //弄清楚如何与之前获取的项目进行合并。
    }
});
 

解决方案

伪code:

  VAR GETALL =功能(PrimaryKey的,CB)
    {
        VAR finalSet = [],
            查询=函数(列克)
            {
                ddb.query(产品,PrimaryKey的,{exclusiveStartKey:列克},功能(错了,结果)
                {
                    如果(ERR)返程CB(ERR);

                    如果(result.items.length)
                      finalSet.push.apply(finalSet,result.items);

                    如果(result.lastEvaluatedKey)
                      查询(result.lastEvaluatedKey);
                    其他
                      CB(NULL,finalSet);
                });
            };

        查询();
    };


GETALL(PrimaryKey的,功能(ERR,全部)
{
    的console.log(错了,所有的);
});
 

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

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.

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

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...

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.
    }
});
解决方案

pseudocode:

var getAll= function (primarykey,cb)
    {
        var finalSet= [],
            query= 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)
                      query(result.lastEvaluatedKey);
                    else
                      cb(null,finalSet);
                });
            };

        query();
    };


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

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

08-24 04:48