software.amazon.awssdk.services.dynamodb.model.DynamoDbException: Query key condition not supported (Service: DynamoDb, Status Code: 400

0

I am trying to query the DynamoDB using the code:

Flux.from(tablePrimary.query(QueryEnhancedRequest.builder() .queryConditional( QueryConditional.sortBeginsWith(k -> k.sortValue(entityId).partitionValue(notificationId))) .build()) .items() )

and my DB structure looks like We have defined sort key and partition key

but getting the same error.

susmita
asked 23 days ago471 views
1 Answer
2

To resolve the DynamoDbException: Query key condition not supported error in your DynamoDB query, ensure the partition key and sort key are used correctly in the QueryConditional.sortBeginsWith method.

Correct Query Code:

Flux.from(tablePrimary.query(
    QueryEnhancedRequest.builder()
        .queryConditional(QueryConditional.sortBeginsWith(
            k -> k.partitionValue(notificationId)
                  .sortValue(entityId)
        ))
        .build()
    )
    .items()
)

Key Points: 1.Partition Key First: Specify partitionValue(notificationId) before sortValue(entityId).

2.Correct Method Usage: Ensure the lambda correctly sets both partition and sort values.

Example with Error Handling:

Flux.from(tablePrimary.query(
    QueryEnhancedRequest.builder()
        .queryConditional(QueryConditional.sortBeginsWith(
            k -> k.partitionValue(notificationId)
                  .sortValue(entityId)
        ))
        .build()
    )
    .items()
)
.onErrorResume(e -> {
    // Log the error
    System.err.println("Error querying DynamoDB: " + e.getMessage());
    return Flux.empty();
})
.subscribe(item -> {
    // Process the item
    System.out.println(item);
});

Table Structure: Verify that your DynamoDB table has the partition key notificationId and the sort key entityId defined correctly.

By following this structure, your query should work without the Query key condition not supported error. i hope this will helps you, thank you

profile picture
answered 23 days ago
  • I tried with partitionKey first and next the sortKey but that still doesn't solve the issue.

  • here the sort value that I passed to the method contains just the beginning few letters of the actual sort value.

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