[英]AWS DynamoDB Python - boto3 Key() methods not recognized (Query)
I am using Lambda (Python) to query my DynamoDB database. 我正在使用Lambda(Python)来查询我的DynamoDB数据库。 I am using the boto3 library, and I was able to make an "equivalent" query:
我正在使用boto3库,我能够创建一个“等效”查询:
This script works: 这个脚本有效:
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
However, when I change the query expression to this: 但是,当我将查询表达式更改为:
KeyConditionExpression=Key('Number_Attribute').gt(0)
I get the error: 我收到错误:
"errorType": "ClientError",
"errorMessage": "An error occurred (ValidationException) when calling the Query operation: Query key condition not supported"
According to this [1] resource, "gt" is a method of Key(). 根据这[1]资源,“gt”是Key()的方法。 Does anyone know if this library has been updated, or what other methods are available other than "eq"?
有没有人知道这个库是否已经更新,或者除了“eq”之外还有哪些其他方法?
[1] http://boto3.readthedocs.io/en/latest/reference/customizations/dynamodb.html#ref-dynamodb-conditions [1] http://boto3.readthedocs.io/en/latest/reference/customizations/dynamodb.html#ref-dynamodb-conditions
---------EDIT---------- - - - - -编辑 - - - - -
I also just tried the old method using: 我也尝试使用旧方法:
response = client.query(
TableName = 'Table_Name',
IndexName='Custom_Index',
KeyConditions = {
'Custom_Number_Attribute':{
'ComparisonOperator':'EQ',
'AttributeValueList': [{'N': '0'}]
}
}
)
This worked, but when I try: 这有效,但当我尝试:
response = client.query(
TableName = 'Table_Name',
IndexName='Custom_Index',
KeyConditions = {
'Custom_Number_Attribute':{
'ComparisonOperator':'GT',
'AttributeValueList': [{'N': '0'}]
}
}
)
...it does not work. ...这是行不通的。
Why would EQ be the only method working in these cases? 为什么EQ是在这些情况下工作的唯一方法? I'm not sure what I'm missing in the documentation.
我不确定文档中缺少什么。
From what I think: Your Partition Key is Number_Attribute, and so you cannot do a gt
when doing a query
(you can do an eq
and that is it.) 根据我的想法:您的分区键是Number_Attribute,因此在执行
query
时您无法执行gt
(您可以执行eq
,就是这样。)
You can do a gt
or between
for your Sort Key when doing a query
. 在进行
query
时,您可以为排序键执行gt
或between
的query
。 It is also called Range key, and because it "smartly" puts the items next to each other, it offers the possibility of doing gt
and between
efficiently in a query
它也被称为Range键,并且因为它“巧妙地”将项目彼此相邻,它提供了在
query
有效地执行gt
和between
的可能性
Now, if you want to do a between
to your partition Key, then you will have to use scan
like the below: 现在,如果你想
between
你的分区键between
进行操作,那么你将不得不使用如下所示的scan
:
Key('Number_Attribute').gt(0)
response = table.scan(
FilterExpression=fe
)
Keep in mind of the following concerning scan: 请记住以下有关扫描:
The scan method reads every item in the entire table, and returns all of the data in the table.
scan方法读取整个表中的每个项目,并返回表中的所有数据。 You can provide an optional filter_expression, so that only the items matching your criteria are returned.
您可以提供可选的filter_expression,以便仅返回符合条件的项目。 However, note that the filter is only applied after the entire table has been scanned.
但请注意,过滤器仅在扫描整个表格后应用。
So in other words, it's a bit of a costly operation comparing to query. 换句话说,与查询相比,这是一个代价高昂的操作。 You can see an example in the documentation here .
您可以在此处的文档中查看示例。
Hope that helps! 希望有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.