AWS DynamoDB Python - boto3 Key() 方法无法识别(查询)
AWS DynamoDB Python - boto3 Key() methods not recognized (Query)
我正在使用 Lambda (Python) 查询我的 DynamoDB 数据库。我正在使用 boto3 库,并且能够进行 "equivalent" 查询:
此脚本有效:
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
但是,当我将查询表达式更改为:
KeyConditionExpression=Key('Number_Attribute').gt(0)
我收到错误:
"errorType": "ClientError",
"errorMessage": "An error occurred (ValidationException) when calling the Query operation: Query key condition not supported"
根据此 [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'}]
}
}
)
这有效,但当我尝试时:
response = client.query(
TableName = 'Table_Name',
IndexName='Custom_Index',
KeyConditions = {
'Custom_Number_Attribute':{
'ComparisonOperator':'GT',
'AttributeValueList': [{'N': '0'}]
}
}
)
...它不起作用。
为什么 EQ 是在这些情况下唯一有效的方法?我不确定我在文档中遗漏了什么。
据我所知:
您的分区键是 Number_Attribute,因此您在执行 query
时不能执行 gt
(您可以执行 eq
,仅此而已。)
您可以在执行 query
时为排序键执行 gt
或 between
。它也称为范围键,因为它 "smartly" 将项目并排放置,它提供了在 query
[= 中高效地执行 gt
和 between
的可能性24=]
现在,如果您想对您的分区键执行 between
,那么您将必须使用 scan
,如下所示:
Key('Number_Attribute').gt(0)
response = table.scan(
FilterExpression=fe
)
请记住以下有关扫描的事项:
The scan method reads every item in the entire table, and returns all of the data in the table. You can provide an optional filter_expression, so that only the items matching your criteria are returned. However, note that the filter is only applied after the entire table has been scanned.
所以换句话说,与查询相比,这是一个有点昂贵的操作。您可以在文档 here.
中查看示例
希望对您有所帮助!
我正在使用 Lambda (Python) 查询我的 DynamoDB 数据库。我正在使用 boto3 库,并且能够进行 "equivalent" 查询:
此脚本有效:
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
但是,当我将查询表达式更改为:
KeyConditionExpression=Key('Number_Attribute').gt(0)
我收到错误:
"errorType": "ClientError",
"errorMessage": "An error occurred (ValidationException) when calling the Query operation: Query key condition not supported"
根据此 [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'}]
}
}
)
这有效,但当我尝试时:
response = client.query(
TableName = 'Table_Name',
IndexName='Custom_Index',
KeyConditions = {
'Custom_Number_Attribute':{
'ComparisonOperator':'GT',
'AttributeValueList': [{'N': '0'}]
}
}
)
...它不起作用。
为什么 EQ 是在这些情况下唯一有效的方法?我不确定我在文档中遗漏了什么。
据我所知:
您的分区键是 Number_Attribute,因此您在执行 query
时不能执行 gt
(您可以执行 eq
,仅此而已。)
您可以在执行 query
时为排序键执行 gt
或 between
。它也称为范围键,因为它 "smartly" 将项目并排放置,它提供了在 query
[= 中高效地执行 gt
和 between
的可能性24=]
现在,如果您想对您的分区键执行 between
,那么您将必须使用 scan
,如下所示:
Key('Number_Attribute').gt(0)
response = table.scan(
FilterExpression=fe
)
请记住以下有关扫描的事项:
The scan method reads every item in the entire table, and returns all of the data in the table. You can provide an optional filter_expression, so that only the items matching your criteria are returned. However, note that the filter is only applied after the entire table has been scanned.
所以换句话说,与查询相比,这是一个有点昂贵的操作。您可以在文档 here.
中查看示例希望对您有所帮助!