When I try to do an empty query:
dynamodb = boto3.resource("dynamodb", endpoint_url="http://localhost:7000")
table = dynamodb.Table(table_name)
response = table.query(
)
I get an error stating that the KeyConditionExpression parameter needs to be passed:
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.
However in the Amazon Docs it says the KeyConditionExpression is not a required parameter.
I'm trying to query my entire table for all the rows and running a scan is not feasible in my scenario since I need the response to be automatically sorted. Any suggestions?
That error is being thrown from DynamoDB. So for query, you always have to provide some expression to query with. KeyConditionExpression is what boto3 uses and KeyCondition is a legacy parameter and is maintained for backward compatibility. But in the end, you have to provided either of them.
I could not find any interface for sorting without specifying some query. Would it be possible to create a query that would return all of the items in your table and that way it sorts all of your items as well?
According to the docs it says neither are required parameters so I'm not sure as to why it's making me provide one of them.
My table is based on a Partition Key which contains the item's category, and then a Sort Key containing the item's unique ID. I attempted to make a query with KeyConditionExpression=Key('id').ge(0) which should theoretically return all the items back since they all have ID's greater than 0, however then I get a Key Schema error. It seems like KeyConditionExpression only works for the Partition Key?
The docs cannot mark either of them as a required in the docs because if a parameter is marked as required it will be a hard fail if the parameter is not even part of the request (it does not matter what other parameters were provided). So for example:
KeyCondition is marked as required, then users would not be able to just only provide KeyConditionExpression (which is recommend) because KeyCondition is not in the request.KeyConditionExpression was marked as required, that would break users previously using only KeyCondition because they now have to provide KeyConditionExpression instead.Yeah so if you have a partition key and a sort key, you would have to provide the partition key in the query as that is required when querying. You can also provide a sort key as well though to narrow down the results returned back, and this has more comparisons available to it than the partition key's equality comparison. Here is more specifics in the docs about what you must provide for queries: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html#QueryAndScan.Query.
To get this working, I think you will need to rely on querying over each category as the partition key can only rely on the eq() comparison. Let us know if you have anymore questions.
Wow I don't think any amount rereading it would have allowed me to interpret it like that, so many thanks for the explanation.
As for the comparison rules, that I definitely overlooked! Now I see the fine print:
You must specify the partition key name and value as an equality condition.
Which explains why I couldn't just do KeyConditionExpression=Key('category').between('a', 'z') as it throws a ValidationException.
Yep no problem. Glad to help.
Note ......".scan(params,function(err,data)" not query
Most helpful comment
Wow I don't think any amount rereading it would have allowed me to interpret it like that, so many thanks for the explanation.
As for the comparison rules, that I definitely overlooked! Now I see the fine print:
You must specify the partition key name and value as an equality condition.Which explains why I couldn't just do
KeyConditionExpression=Key('category').between('a', 'z')as it throws a ValidationException.