0

我在 DynamoDB 中创建了一个表,它同时具有主键(字符串)和排序键(数字)(课程-课程-id),我正在使用以下简单的 Lambda 函数来查询课程-课程-id 所在的表> 0:

import json
import boto3
from boto3.dynamodb.conditions import Key, Attr

#always start with the lambda_handler
def lambda_handler(event, context):

    # make the connection to dynamodb
    dynamodb = boto3.resource('dynamodb')

    # select the table
    table = dynamodb.Table("table-name")

    response = table.query(
    KeyConditionExpression=Key('course-lesson-id').gt(0)
    )
    items = response['Items']
    print(items)

据我了解,结果应该基于排序键以数字顺序返回,但我收到以下错误:

ClientError: An error occurred (ValidationException) when calling the 
Query operation: Query condition missed key schema element: course- 
lesson

course-lesson 是主分区键的名称。

关于可能的原因或修复的任何想法?

4

1 回答 1

2

您需要为Query操作提供主键。从查询文档

使用该参数为分区键KeyConditionExpression提供特定值。[...]您可以选择通过指定排序键值来缩小查询操作的范围[...]

您不能Query仅通过提供排序键来使用 a 。如果您只想基于course-lesson-id大于 的过滤进行查询0,则使用 a Scan- 但是,请注意Scans 在资源使用方面“更昂贵”,即对于大表,它们需要更长的时间来执行。

于 2020-02-24T07:30:13.617 回答