如何在BatchGetItem中使用过滤器 [英] How to use filter in BatchGetItem

查看:87
本文介绍了如何在BatchGetItem中使用过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用BatchGetItem从dynamodb获取多个项目。但是我需要根据某些条件过滤项目。

I am fetching multiple items from dynamodb using BatchGetItem. But I need to filter items based on some conditions .

例如:我有一个具有ID和状态的任务表。我需要获取ID为1、2、3的项目,其中status = done。

For example: I have a task table having id and status. I need to fetch items of id 1, 2, 3 where status=done.

#set($ids = [1,2,3])
{    
    "operation" : "BatchGetItem",
    "tables" : {
        "userTable": {
            "keys": $util.toJson($ids)
        }
    }
}


推荐答案

BatchGetItem 不支持过滤器表达式。将 BatchGetItem 视为 GetItem 操作的批处理版本。 GetItem 仅支持基于其主键检索项目。

Filter expressions are not supported in BatchGetItem. Think of BatchGetItem as a batched version of the GetItem operation. GetItem only supports retrieving an item based on its primary key.

在您的示例中,如果状态列不是主键的一部分,则 BatchGetItem GetItem

In your example, if the status column is not part of the primary key, then BatchGetItem or GetItem will not work for you.

示例中提供的任务表的分区键为 id ,常规为 status 列,根据表的大小和期望返回的项目数,有多种方法可以达到相同的结果。

Provided the task table in your example has id as partition key and status as regular column, there are multiple ways you can achieve the same result depending on the size of your table and the number of items you are expecting to be returned back.

如果输入项目的数量相对较少,则可以使用 BatchGetItem ,然后用代码(在VTL中)过滤掉 status!= done 的代码。

If the number of input items is relatively small, you could just use BatchGetItem, and then filter out in code (here in VTL) the ones where status!=done.

优点:您无需更改表架构或添加索引

Pros: You don't need to change your table schema or add indices

缺点:您将支付全部费用e在过滤之前读取检索项目所需的容量单位。由于您的API将下载可能不必要的项目,因此也会产生延迟。

Cons: You will pay for all the Read Capacity Units necessary to retrieve the items before filtering. There will also be a latency cost since your API will download potentially unnecessary items.

如果表很小,则可以扫描整个表并提供IN过滤器表达式。

If the table is small you can scan the entire table and provide an IN filter expression.

优点:您无需更改表架构或添加索引

Pros: You don't need to change your table schema or add indices

缺点:您将需要扫描整个

如果您不希望更改表键架构,则这些建议有效。

These recommendations are valid if you do not wish to change your table key schema.

这篇关于如何在BatchGetItem中使用过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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