Python 使用查询从 DynamoDB 中检索所有项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21515765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve all items from DynamoDB using query?
提问by dlaser
I am trying to retrieve all items in a dynamodb table using a query. Below is my code:
我正在尝试使用查询检索 dynamodb 表中的所有项目。下面是我的代码:
import boto.dynamodb2
from boto.dynamodb2.table import Table
from time import sleep
c = boto.dynamodb2.connect_to_region(aws_access_key_id="XXX",aws_secret_access_key="XXX",region_name="us-west-2")
tab = Table("rip.irc",connection=c)
x = tab.query()
for i in x:
print i
sleep(1)
However, I recieve the following error:
但是,我收到以下错误:
ValidationException: ValidationException: 400 Bad Request
{'message': 'Conditions can be of length 1 or 2 only', '__type': 'com.amazon.coral.validate#ValidationException'}
The code I have is pretty straightforward and out of the boto dynamodb2 docs, so I am not sure why I am getting the above error. Any insights would be appreciated (new to this and a bit lost). Thanks
我拥有的代码非常简单,并且不在 boto dynamodb2 文档中,所以我不确定为什么会出现上述错误。任何见解将不胜感激(对此很陌生,有点迷失)。谢谢
EDIT: I have both an hash key and a range key. I am able to query by specific hash keys. For example,
编辑:我有一个散列键和一个范围键。我可以通过特定的哈希键进行查询。例如,
x = tab.query(hash__eq="2014-01-20 05:06:29")
How can I retrieve all items though?
我怎样才能检索所有项目?
回答by dlaser
Ahh ok, figured it out. If anyone needs:
啊好吧,想通了。如果有人需要:
You can't use the query method on a table without specifying a specific hash key. The method to use instead is scan. So if I replace:
您不能在不指定特定哈希键的情况下对表使用查询方法。使用的方法是扫描。所以如果我更换:
x = tab.query()
with
和
x = tab.scan()
I get all the items in my table.
我拿到了桌子上的所有物品。
回答by Vadim
I'm on groovy but it's gonna drop you a hint. Error :
我在 groovy 但它会给你一个提示。错误 :
{'message': 'Conditions can be of length 1 or 2 only'}
is telling you that your key condition can be length 1 -> hashKey only, or length 2 -> hashKey + rangeKey. All what's in a query on a top of keys will provoke this error. The reason of this error is: you are trying to run searchquery but using key conditionquery. You have to add separate filterConditionto perform your query. My code
告诉你你的关键条件可以是 length 1 -> hashKey only,或 length 2 -> hashKey + rangeKey。键顶部的查询中的所有内容都会引发此错误。此错误的原因是:您正在尝试运行搜索查询,但使用了关键条件查询。您必须添加单独的filterCondition才能执行查询。我的代码
String keyQuery = " hashKey = :hashKey and rangeKey between :start and :end "
queryRequest.setKeyConditionExpression(keyQuery)// define key query
String filterExpression = " yourParam = :yourParam "
queryRequest.setFilterExpression(filterExpression)// define filter expression
queryRequest.setExpressionAttributeValues(expressionAttributeValues)
queryRequest.setSelect('ALL_ATTRIBUTES')
QueryResult queryResult = client.query(queryRequest)
回答by Adithya
This is how I do a query if someone still needs a solution:
如果有人仍然需要解决方案,我就是这样做的:
def method_name(a, b)
results = self.query(
key_condition_expression: '#T = :t',
filter_expression: 'contains(#S, :s)',
expression_attribute_names: {
'#T' => 'your_table_field_name',
'#S' => 'your_table_field_name'
},
expression_attribute_values: {
':t' => a,
':s' => b
}
)
results
end
回答by Dylan w
I ran into this error when I was misusing KeyConditionExpressioninstead of FilterExpressionwhen querying a dynamodb table.
当我滥用KeyConditionExpression而不是FilterExpression查询 dynamodb 表时,我遇到了这个错误。
KeyConditionExpressionshould only be used with partition key or sort key values.
FilterExpressionshould be used when you want filter your results even more.
KeyConditionExpression应该只与分区键或排序键值一起使用。
FilterExpression当您想进一步过滤结果时,应该使用它。
However do note, using FilterExpressionuses the same reads as it would without, because it performs the query based on the keyConditionExpression. It then removes items from the results based on your FilterExpression.
但是请注意,使用FilterExpression与不使用时使用相同的读取,因为它基于keyConditionExpression. 然后它会根据您的FilterExpression.
SourceWorking with Queries
回答by Eph Harris
.scan() does not automatically return all elements of a table due to pagination of the table. There is a 1Mb max response limit Dynamodb Max response limit
由于表格的分页,.scan() 不会自动返回表格的所有元素。有 1Mb 最大响应限制Dynamodb 最大响应限制
Here is a recursive implementation of the boto3 scan:
这是 boto3 扫描的递归实现:
import boto3
dynamo = boto3.resource('dynamodb')
def scanRecursive(tableName, **kwargs):
"""
NOTE: Anytime you are filtering by a specific equivalency attribute such as id, name
or date equal to ... etc., you should consider using a query not scan
kwargs are any parameters you want to pass to the scan operation
"""
dbTable = dynamo.Table(tableName)
response = dbTable.scan(**kwargs)
if kwargs.get('Select')=="COUNT":
return response.get('Count')
data = response.get('Items')
while 'LastEvaluatedKey' in response:
response = kwargs.get('table').scan(ExclusiveStartKey=response['LastEvaluatedKey'], **kwargs)
data.extend(response['Items'])
return data

