使用过滤器表达式和行限制从 dynamodb 查询返回恒定数量的行

问题描述 投票:0回答:1

我有一个看起来像这样的 dynamodb 表

resource = boto3.resource("dynamodb")
resource.create_table(TableName="mytable", KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"},
                                              {"AttributeName": "sk", "KeyType": "RANGE"}],
                                                  AttributeDefinitions=[
                                                      {"AttributeName": "pk", "AttributeType": "S"},
                                                      {"AttributeName": "sk", "AttributeType": "S"},
                                                      {"AttributeName": "type", "AttributeType": "S"},
                                                      {"AttributeName": "brand", "AttributeType": "S"},
                                                      {"AttributeName": "creationDate", "AttributeType": "S"},
                                                  ],
                                                  ProvisionedThroughput={
                                                      "ReadCapacityUnits": 10,
                                                      "WriteCapacityUnits": 10,
                                                  },
                                                  GlobalSecondaryIndexes=[
                                                      {
                                                          "IndexName": "GSI1",
                                                          "KeySchema": [
                                                              {"AttributeName": "type", "KeyType": "HASH"},
                                                              {"AttributeName": "brand", "KeyType": "RANGE"},
                                                          ],
                                                          "Projection": {
                                                              "ProjectionType": "ALL"
                                                          },
                                                          "ProvisionedThroughput": {
                                                              "ReadCapacityUnits": 5,
                                                              "WriteCapacityUnits": 5,
                                                          },
                                                      },
                                                      {
                                                          "IndexName": "GSI2",
                                                          "KeySchema": [
                                                              {"AttributeName": "type", "KeyType": "HASH"},
                                                              {"AttributeName": "creationDate", "KeyType": "RANGE"},
                                                          ],
                                                          "Projection": {
                                                              "ProjectionType": "ALL"
                                                          },
                                                          "ProvisionedThroughput": {
                                                              "ReadCapacityUnits": 5,
                                                              "WriteCapacityUnits": 5,
                                                          },
                                                      }
                                                  ]
                                                  )

我想创建一个查询来获取

type
的所有条目 - 假设输入“ABC”。然而,该查询也应该只允许某些品牌。假设有三个

  • 阿迪达斯
  • 美洲狮
  • 耐克

并且该用户只能访问其中两个

["Addidas", "Nike"]
。查询应使用恒定的页面大小(假设 10 个条目)进行分页并返回
LastEvaluatedKey
。返回的数据需要通过一个名为
creationDate
的字段进行排序。

为了解决这个问题,我创建了以下查询:

response = resource.query(IndexName="GSI2", KeyConditions={
                                                  "type": {
                                                      "AttributeValueList": ["ABC"],
                                                      "ComparisonOperator": "EQ"
                                                  }
                                              },
                                              FilterExpression=Attr("brand").is_in(brands),
                                              Limit=no_of_items,
                                              ScanIndexForward=False
                                              )

只要所有品牌都在数组中,它就会按预期工作。如果不是,则几乎总是会产生少于十个条目,因为限制是在 FilterExpression 之前应用的。 有没有什么方法可以对此进行建模,以便在不需要所有品牌的情况下使分页正常工作?

amazon-dynamodb boto3 dynamodb-queries amazon-dynamodb-index
1个回答
0
投票

不幸的是,没有办法根据过滤后的结果数量进行限制。您需要估计过滤器产量(可能匹配的百分比)以计算要使用的预过滤器限制。可能估计有点高了。如果还不够,请进行第二次获取。

© www.soinside.com 2019 - 2024. All rights reserved.