Dynamodb过滤器表达式未返回所有结果 [英] Dynamodb filter expression not returning all results

查看:125
本文介绍了Dynamodb过滤器表达式未返回所有结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扫描最近7天的所有项目,所以我要做的是生成7天的时间戳,并过滤大于该值的时间戳。但是此扫描返回了一些结果。

I want to scan all items for last 7 days, so what I do is I generate timestamp for 7 days back and filter for timestamp greater than that value. But this scan is returning a few results.

请参见以下Javascript:

See the following Javascript:

const daysBack = (days) => {
  let date = new Date();
  date.setDate(date.getDate() - days);
  return date.getTime() ; 
}


const params = {
  TableName: process.env.DYNAMODB_TABLE,
  FilterExpression: "#ts > :z",
  ExpressionAttributeNames:{
      "#ts": "timestamp"
  },
  ExpressionAttributeValues: {
      ":z": daysBack(7)
  },
};

dynamoDb.scan(params, (error, result) => {
  // ... 
}


推荐答案

这是因为在 SCAN操作 c中dynamoDb仅发送数据最多不超过1mb 。如果所需记录的大小超过1mb,则会自动分页。

It is because on a SCAN operation dynamoDb will only send data upto 1mb only. If the records you want are of size more than 1mb automatically pagination happens.

如果记录您的结果,然后您将找到一个名为 LastEvaluatedKey
的属性(如果存在),则必须再次调用以获取剩余数据。要递归地实现,并且当不存在 LastEvaluatedKey 属性时,必须停止它。

If you log your Result then you will find an attribute called LastEvaluatedKey if this attribute is present then you will have to make another call to get the remaining data. This call has to be implemented recursively and you have to stop it when LastEvaluatedKey attribute is not present.

让我们看下面的示例递归地提取项目数据,然后将整个数据附加到数组中,然后发送。

Lets see this example where project data is been fetched recursively and the whole data is appended in the array and then send.

let getFromDb = function (params, callback) {
    params.ConsistentRead = true;
    let projectCollection = [];
    dynamodbclient.scan(params, onQuery);

    function onQuery(err, data) {
        const methodName = 'onQuery';
        if (err) {
            callback(err);
            log.error(err, {
                class: className,
                func: methodName
            });
        } else {
            for (let i = constant.LENGTH_ZERO; i < data.Items.length; i++) {
                projectCollection.push(data.Items[i]);
            }
            if (typeof data.LastEvaluatedKey !== 'undefined') {
                params.ExclusiveStartKey = data.LastEvaluatedKey;
                dynamodbclient.scan(params, onQuery);
            } else {
                callback(err, projectCollection); //recursive call
            }
        }
    }
}; 

这篇关于Dynamodb过滤器表达式未返回所有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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