【问题标题】:DynamoDB find all items after dateDynamoDB 查找日期之后的所有项目
【发布时间】:2020-02-19 10:29:22
【问题描述】:

我有一个存储链接数据的 DynamoDB 表(urldatecategorytags 等)。

我需要能够 -

  • 通过url 查找项目(检查url 不存在)
  • 查找在给定date之后存储的所有项目

基于以上,我在date上设置了url作为主哈希键和二级索引的架构,如下-

AWSTemplateFormatVersion: '2010-09-09'
Parameters:
  TableName:
    Type: String
    Default: "my_links"
  HashAttr:
    Type: String
    Default: url
  IndexAttr:
    Type: String
    Default: date
  ReadCapacity:
    Type: Number
    Default: 5
  WriteCapacity:
    Type: Number
    Default: 5
Resources:
  Table:
    Properties:
      KeySchema:
        - AttributeName: !Ref HashAttr
          KeyType: HASH
      AttributeDefinitions:
        - AttributeName: !Ref HashAttr
          AttributeType: S
        - AttributeName: !Ref IndexAttr
          AttributeType: S
      GlobalSecondaryIndexes:
        - IndexName: !Ref IndexAttr
          KeySchema:
            - AttributeName: !Ref IndexAttr
              KeyType: HASH
          Projection:
            ProjectionType: ALL
          ProvisionedThroughput:
            ReadCapacityUnits: !Ref ReadCapacity
            WriteCapacityUnits: !Ref WriteCapacity
      ProvisionedThroughput:
        ReadCapacityUnits: !Ref ReadCapacity
        WriteCapacityUnits: !Ref WriteCapacity
      TableName: !Ref TableName
    Type: AWS::DynamoDB::Table

我可以按如下方式通过date 查询表,但只能使用eq 条件-

ddb=boto3.resource("dynamodb")
table=ddb.Table("my_links")
from boto3.dynamodb.conditions import Key
queryexp=Key('date').eq("2020-02-19")
for item in table.query(IndexName="date",
                        KeyConditionExpression=queryexp)["Items"]:
    print (item)    

如果我使用 gte 代替 eq 条件,我会得到以下 -

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Query key condition not supported

但是,我可以使用扫描和gte 条件查询表 -

filterexp=Key('date').gte("2020-02-19")
for item in table.scan(FilterExpression=filterexp)["Items"]:
    print (item)

但是我猜我不再需要二级索引,而且随着表变大,这将变得非常昂贵:-/

所以如果可能的话,我宁愿坚持使用二级索引和查询(我在想这个吗?),但是我需要对架构做些什么才能在 之后获取所有项目 em> 约会?

【问题讨论】:

    标签: amazon-dynamodb


    【解决方案1】:

    您不能使用 GTE。 查询支持 EQ |乐 | LT |通用电气 |燃气轮机 | BEGINS_WITH |之间

    查看这篇文章https://medium.com/cloud-native-the-gathering/querying-dynamodb-by-date-range-899b751a6ef2

    【讨论】:

    • queryexp=DDBKey('date').between(low_value=args["startdate"], high_value=args["enddate"]) 仍然失败,Query key condition not supported
    • 我错过了这个:创建全局二级索引而不是二级索引
    • 我不是已经创建了全局二级索引了吗?索引定义在 CF yaml 中的GlobalSecondaryIndexes 下指定
    【解决方案2】:

    所以这很有用 -

    https://stackoverflow.com/a/38790120/124179

    还有这个 -

    How to query DynamoDB by date (range key), with no obvious hash key?

    最后的答案是对数据进行非规范化,用week 字段替换date 字段(和相关的二级索引),然后使用eq 查询条件搜索一个或多个特定周并加入结果(我只需要几周的数据)

    显然可以将week 替换为month 以增加范围但降低粒度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      相关资源
      最近更新 更多