【问题标题】:DynamoDB gt() query on GSIGSI 上的 DynamoDB gt() 查询
【发布时间】:2022-02-08 22:41:54
【问题描述】:

我正在尝试使用 DynamoDB GSI 获取 gt() 查询

我的Cloudformation如下-

Resources
  MyTable:
    Properties:
      AttributeDefinitions:
      - AttributeName: pk
        AttributeType: S
      - AttributeName: sk
        AttributeType: S
      - AttributeName: my_datetime_string
        AttributeType: S
      BillingMode: PAY_PER_REQUEST
      GlobalSecondaryIndexes:
      - IndexName: my_datetime_string-index
        KeySchema:
        - AttributeName: my_datetime_string
          KeyType: HASH
        Projection:
          ProjectionType: ALL
      KeySchema:
      - AttributeName: pk
        KeyType: HASH
      - AttributeName: sk
        KeyType: RANGE
      TableName:
        Fn::Sub: my-table-${AWS::StackName}-${AWS::Region}
    Type: AWS::DynamoDB::Table

我的查询代码如下(python)-

tablename="whatever"
import boto3
table=boto3.resource("dynamodb").Table(tablename)
from datetime import datetime
timestamp="%s 00:00:00" % datetime.today().strftime("%Y-%m-%d")
from boto3.dynamodb.conditions import Key
expr=Key("my_datetime_string").gt(timestamp)
print (table.query(IndexName="my_datetime_string-index",        
                   KeyConditionExpression=expr))

我在表中有一些数据,可以看到它有一个字符串类型 (my_datetime_string) -

{'Item': {'my_datetime_string': '2022-01-27 14:30:00', 'sk': 'HEAD', 'pk': 'EVENT#1293000'}, 'ResponseMetadata': {'RequestId': 'XXX', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Tue, 08 Feb 2022 10:54:15 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '522', 'connection': 'keep-alive', 'x-amzn-requestid': 'XXX', 'x-amz-crc32': '2080419831'}, 'RetryAttempts': 0}}

但我从查询中得到的只是 -

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

我在这里缺少什么? GSI 索引肯定支持gt() 查询String 类型吗? (或者可能不是??)

【问题讨论】:

  • Ps 代码在 eq() 查询中运行良好

标签: boto3 dynamodb-queries amazon-dynamodb-index amazon-dynamodb


【解决方案1】:

my_datetime_string 是您索引的Partition Key (= Hash Key)。 Query 必须提供唯一的 Partition Key 值。这意味着eq 是Partition Key 条件下的有效运算符,但gt 不是。

如果您的表/索引具有compound primary key,则查询可以将gt 操作应用于Sort Key (= Range Key) 条件,这将返回一个range 值。

【讨论】:

  • 超级清晰-谢谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多