【问题标题】:Using query IndexName, GSI in DynamoDB and not available begins_with in python在 DynamoDB 中使用查询 IndexName、GSI 而在 python 中不可用 begin_with
【发布时间】:2021-02-09 15:12:20
【问题描述】:

我正在关注文档,发现“begins_with”可用于 python 3.7,但如果我想使用 GSI“createdAt-index”查询表,则会收到下一个错误。

所以这是不起作用的代码:


def query_pharmaorders(date):
    table = dynamodb.Table('users')
    response = table.query(
        IndexName="createdAt-index",
        KeyConditionExpression=Key('createdAt').begins_with("2020"),
    )
    return (response['Items'])

这是错误:


Response
{
  "errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 19)",
  "errorType": "Runtime.UserCodeSyntaxError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\" Line 19\n    \t\tKeyConditionExpression=Key('createdAt').begins_with(\"2020\")\n"
  ]
}

我的分区键是在哪里创建的。

有什么想法吗?

【问题讨论】:

  • 你到底遇到了什么错误?

标签: python amazon-web-services amazon-dynamodb


【解决方案1】:

您没有说出错误是什么,但对可能出现的错误有一些想法。

根据docs for the query operation

请注意,如果您使用 IndexName 参数,您还必须提供 TableName。

使用query操作时,需要进行分区键相等性测试,必须按以下格式指定:

partitionKeyName = :partitionkeyval

虽然您可以在排序键上使用begins_with 条件,但不能对分区键执行此操作。使用query 操作时,您需要指定exact 分区键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多