AmazonDynamoDBException in Java

0

Hello,

I'm using the below java code to get from DynamoDB all the items for a Partition Key. Table has a Partition Key (PK) and a Sort Key (SK) that are Strings.

TableKeysAndAttributes tableKeysAndAttributes = new TableKeysAndAttributes(tableDetails.getTableName())
    	.withHashOnlyKeys(tableDetails.getTablePKName(), tableDetails.getTablePKValue())
    	.withProjectionExpression("document");
    	
BatchGetItemOutcome outcome = dynamoClient.batchGetItem(tableKeysAndAttributes);

I got the below error, as in the case when I should send also the SK to the database, while the API supports only PK:

com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID:  
 LGT808CBEEL6CEN8EMATK07R0JVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null): java.lang.RuntimeException java.lang.RuntimeException: com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID:  
 LGT808CBEEL6CEN8EMATK07R0JVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null) at  
 com.amazonaws.lambda.mihai.translate.handler.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:79) at  
 com.amazonaws.lambda.mihai.translate.handler.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:26) Caused by:  
 com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID:  
 LGT808CBEEL6CEN8EMATK07R0JVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null) at  
 com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1879) at  
 com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleServiceErrorResponse(AmazonHttpClient.java:1418) at  
 com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1387) at  
 com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1157) at  
 com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:814) at  
 com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:781) at  
 com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:755) at  
 com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:715) at  
 com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:697) at  
 com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:561) at  
 com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:541) at  
 com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:6999) at  
 com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:6966) at  
 com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.executeBatchGetItem(AmazonDynamoDBClient.java:607) at  
 com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.batchGetItem(AmazonDynamoDBClient.java:571) at  
 com.amazonaws.services.dynamodbv2.document.internal.BatchGetItemImpl.doBatchGetItem(BatchGetItemImpl.java:98) at  
 com.amazonaws.services.dynamodbv2.document.internal.BatchGetItemImpl.batchGetItem(BatchGetItemImpl.java:57) at  
 com.amazonaws.services.dynamodbv2.document.DynamoDB.batchGetItem(DynamoDB.java:153) at  
 com.amazonaws.lambda.mihai.servicecommons.service.DynamoAbstractService.getPartitionKeyDocuments(DynamoAbstractService.java:229) at  

Could you please specify why do I have the error in this case ?

Thank you,
Mihai ADAM

asked a month ago114 views
2 Answers
2

The API which you need to use is Query which allows you to get all items related to a single partition key.

You must provide the name of the partition key attribute and a single value for that attribute. Query returns all items with that partition key value. Optionally, you can provide a sort key attribute and use a comparison operator to refine the search results.

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.US_WEST_2).build();
DynamoDB dynamoDB = new DynamoDB(client);

Table table = dynamoDB.getTable("Reply");

QuerySpec spec = new QuerySpec()
    .withKeyConditionExpression("Id = :v_id")
    .withValueMap(new ValueMap()
        .withString(":v_id", "Amazon DynamoDB#DynamoDB Thread 1"));

ItemCollection<QueryOutcome> items = table.query(spec);

Iterator<Item> iterator = items.iterator();
Item item = null;
while (iterator.hasNext()) {
    item = iterator.next();
    System.out.println(item.toJSONPretty());
}

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html

profile pictureAWS
EXPERT
answered a month ago
0
profile picture
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
  • In the described case (with PK and SK) is it possible to get all records for one PK, using the standard interface, and without using a Query ?
    Which is the best solution to get all records for a PK ?

    Thank you

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions