Retrieve all items from DynamoDB using query?
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.
I'm on groovy but it's gonna drop you a hint. Error :
{'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 search query but using key condition query. You have to add separate filterCondition to perform your query. My code
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)
.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
Here is a recursive implementation of the boto3 scan:
import boto3dynamo = 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