I have a table with the following KeySchema:
KeySchema: [
{AttributeName: USER_ID,KeyType: HASH},
{AttributeName: COMPLETED,KeyType: RANGE}],
The user ID is just a integer, but COMPLETED
is a timestamp of when the record was created.
I'm trying to retrieve a record with this code:
Item item = tbl.getItem("USER_ID", "4216634082");
But I get this error:
AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException...
Since the range key is a timestamp, I have no idea what it is until I've already found the record. How do I achieve this?
You are calling getItem
, which is a lookup of an item by its key - this requires both hash and range components if the key schema defines both.
If you want to get all the items that have a particular hash (ordered by range key), you need to use a query. For example:
Table table = dynamoDB.getTable("YourTable");
QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("USER_ID = :user_id")
.withValueMap(new ValueMap()
.withString(":user_id", "4216634082"))
ItemCollection<QueryOutcome> items = table.query(spec);
See http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html for concepts and http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html for detailed Java SDK documentation.
(Example lifted from the doc links)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With