Get all items using only the Partition key from Dynamodb

Viewed 1565

How to get all the items matching only the partition key (table has sort key) using Amazon DynamoDB module. I'm using the GetItemRequest to query without the sort key.

GetItemRequest getItemRequest = getRequest(externalId);
IDMapping.from(dynamoDB.getItem(getItemRequest).item());

I get the following error when i query only with partition key with GetItemRequest.

Caused by: software.amazon.awssdk.services.dynamodb.model.DynamoDbException: The provided key element does not match the schema (Service: DynamoDb, Status Code: 400, Request ID: 6C9BTJCJOTS7I5FI1EBJLIQK3RVV4KQNSO5AEMVJF66Q9ASUAAJG, Extended Request ID: null)

I'm using the following dependency in my quarkus application.

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>dynamodb</artifactId>
</dependency>

This query is possible using aws cli, aws console and also with the older java aws-sdk.

are there any ways to overcome this?

Thanks in Advance

1 Answers

The GetItem request requires the full key (partition and sort). You can do what you want with a Query request.

    HashMap<String, String> nameMap = new HashMap<String, String>();
    nameMap.put("#yr", "year");

    HashMap<String, Object> valueMap = new HashMap<String, Object>();
    valueMap.put(":yyyy", 1985);

    QuerySpec querySpec = new QuerySpec().withKeyConditionExpression("#yr = :yyyy").withNameMap(nameMap)
        .withValueMap(valueMap);

    items = table.query(querySpec);

This example is for a table with a composite key:

  • year – The partition key. The attribute type is number.
  • title – The sort key. The attribute type is string.
Related