AWS DynamoDB Python - 无法识别boto3 Key()方法(查询)

sea*_*ean 5 python amazon-web-services amazon-dynamodb boto3 aws-lambda

我正在使用Lambda(Python)来查询我的DynamoDB数据库.我正在使用boto3库,我能够创建一个"等效"查询:

这个脚本有效:

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

def create_list(event, context):
    resource = boto3.resource('dynamodb')
    table = resource.Table('Table_Name')

    response = table.query(
        TableName='Table_Name',
        IndexName='Custom-Index-Name',
        KeyConditionExpression=Key('Number_Attribute').eq(0)
    )
    return response
Run Code Online (Sandbox Code Playgroud)

但是,当我将查询表达式更改为:

KeyConditionExpression=Key('Number_Attribute').gt(0)
Run Code Online (Sandbox Code Playgroud)

我收到错误:

"errorType": "ClientError",
  "errorMessage": "An error occurred (ValidationException) when calling the Query operation: Query key condition not supported"
Run Code Online (Sandbox Code Playgroud)

根据这[1]资源,"gt"是Key()的方法.有没有人知道这个库是否已经更新,或者除了"eq"之外还有哪些其他方法?

[1] http://boto3.readthedocs.io/en/latest/reference/customizations/dynamodb.html#ref-dynamodb-conditions

- - - - -编辑 - - - - -

我也尝试使用旧方法:

response = client.query(
        TableName = 'Table_Name',
        IndexName='Custom_Index',
        KeyConditions = {
            'Custom_Number_Attribute':{
                'ComparisonOperator':'EQ',
                'AttributeValueList': [{'N': '0'}]
            }
        }
    )
Run Code Online (Sandbox Code Playgroud)

这有效,但当我尝试:

response = client.query(
            TableName = 'Table_Name',
            IndexName='Custom_Index',
            KeyConditions = {
                'Custom_Number_Attribute':{
                    'ComparisonOperator':'GT',
                    'AttributeValueList': [{'N': '0'}]
                }
            }
        )
Run Code Online (Sandbox Code Playgroud)

...这是行不通的.

为什么EQ是在这些情况下工作的唯一方法?我不确定文档中缺少什么.

Guy*_*her 8

从我的想法来看:你的分区键是Number_Attribute,所以你做的gt时候不能做query(你可以做一个eq就是它.)

你可以在做一个gt或者between你的排序键时做一个query.它也被称为范围键,因为它"巧妙地"把彼此相邻的项目,它提供了这样的可能性gt,并between在有效query

现在,如果你想对between你的分区密钥进行操作,那么你必须使用scan如下所示:

Key('Number_Attribute').gt(0)
response = table.scan(
    FilterExpression=fe
)
Run Code Online (Sandbox Code Playgroud)

请记住以下有关扫描:

scan方法读取整个表中的每个项目,并返回表中的所有数据.您可以提供可选的filter_expression,以便仅返回符合条件的项目.但请注意,过滤器仅在扫描整个表格后应用.

换句话说,与查询相比,这是一个代价高昂的操作.您可以在此处的文档中查看示例.

希望有所帮助!