首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DynamoDB gt()查询

DynamoDB gt()查询
EN

Stack Overflow用户
提问于 2022-02-08 11:00:53
回答 1查看 326关注 0票数 0

我正在尝试使用一个gt()查询来处理DynamoDB

我的云彩形式如下-

代码语言:javascript
运行
复制
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) -

代码语言:javascript
运行
复制
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) -

代码语言:javascript
运行
复制
{'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}}

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

代码语言:javascript
运行
复制
An error occurred (ValidationException) when calling the Query operation: Query key condition not supported

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-08 14:39:21

my_datetime_string是索引的Partition Key (= Hash Key)。查询必须提供一个和一个Partition Key值.这意味着eqPartition Key条件下是一个有效的运算符,但gt不是。

如果表/索引有复合主键,查询可以将gt操作应用于Sort Key (= Range Key)条件,该条件将返回值的范围

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71032753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档