dynamoDB:如何查询从X索引开始的N个元素 [英] dynamoDB: How to query N elements starting at X index

查看:241
本文介绍了dynamoDB:如何查询从X索引开始的N个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用python3的代码示例,没有指向文档的链接.我没有在文档中找到示例.

I'm looking for examples of the code using python3, no links to the documentation. I havent found examples in the documentation.

我正在查询从ID 1开始的2个类别为红色"的元素.

I'm looking to query 2 elements with the category "red" starting at the ID 1.

这是我的桌子:

| ID | category | description |
| 0  | red      | ....        |
| 1  | red      | ....        |
| 2  | blue     | ....        |
| 3  | red      | ....        |
| 4  | red      | ....        |

查询应返回ID为1和3的元素.

The query should return the elements with the id 1 and 3.

期待阅读您的示例.预先感谢.

Looking forward to read your examples. Thanks in advance.

推荐答案

您需要定义一个全局二级索引,其中分区键为category,排序键为id.

You need to define an Global secondary index in which the partition key is category and the sort key is id.

一旦定义了该索引,就可以按以下方式查询它(抱歉,我使用的是JS表示法):

Once your have that index defined, you can query it as follows (I am using the JS notation, sorry):

{
  TableName: 'your_table_name',
  IndexName: 'your_index_name',
  KeyConditionExpression: 'category = :x and ID >= :y',
  ExpressionAttributeValues: {
    ':x': 'red',
    ':y': 1
  }
}

请注意,这是一个查询.在DynamoDB中,查询对项目的块"(aka:页面")进行处理.具体来说,在执行查询时,DDB会获取一个块,在该块中查找所有匹配项,然后将其返回.如果其他块中还有其他匹配项,则不会返回它们.但是,响应将为您提供下一个块的详细信息,以便您可以对下一个块发出后续查询.这些详细信息"封装在响应的LastEvaluatedKey字段中,应将其复制到后续请求的ExclusiveStartKey中.

Note that this is a query. In DynamoDB, queries work on "chunks" of items (aka: "pages"). Specifically, when executing a query, DDB takes a chunk, finds all matching items in that chunks and returns them. If there are other matching items in other chunks they will not be returned. However, the response will provide you with details of the next chunk so that you can issue a subsequent query on the next chunk. These "details" are encapsulated in the LastEvaluatedKey field of the response and they should be copied into the ExclusiveStartKey of the subsequent request.

您可以查看此指南查看使用LastEvaluatedKey的示例.查找以下行:

You can check this guide to see an example of using LastEvaluatedKey. Look for the following line:

while 'LastEvaluatedKey' in response:

重要!

尽管您只想获得两项,但您不想将Limit字段设置为2.将其设置为2意味着DynamoDB在查找与您的查询匹配的项目时将使用非常小的块(实际上,它将仅使用两个项目的块):这意味着您将需要进行多次重复的查询(通过使用LastEvaluatedkey/ExclusiveStartKey,直到您实际找到两个匹配的项目.这将大大减慢整个过程.对于大多数实际情况,最好的做法是根本不设置Limit字段,而仅使用其默认值.

Although you want to get just two items, you do not want to set the Limit field to 2. Setting it to 2 means that DynamoDB will use very small chunks when looking for items that match your query (in fact, it will use chunks of just two items): this means you will need to do numerous repeated queries (by using LastEvaluatedkey/ExclusiveStartKey as explained above) until you actually find two matching items. This will considerably slow down the entire process. For most practical scenarios, the best thing to do is not to set the Limit field at all, and just use its default value.

这篇关于dynamoDB:如何查询从X索引开始的N个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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