'Exclusive Start Key causing issue with local secondary index in DynamoDbMapper

I have a table in dynamo db. Below is the key for this table.

partitionKey - campaignId sortKey - email

I have created a local secondary index for this table.Below is the key for local secondary index

partitionKey - campaignId sortKey - subStatus

As per the dynamo db you should include primary keys of the table and the index (as key), with last evaluated values (as attribute value) when you setting ExclusiveStartKey.

Below is the code snippet of code.

public QueryResultPage<Prospect> getPaginatedProspectsByCampaignIdAndSubStatus(String campaignId, ProspectSubStatus subStatus, Integer limit, Map<String, AttributeValue> lastEvaluatedKey, String email) {
        Prospect prospect = new Prospect();
        prospect.setCampaignId(campaignId);
        
        Map<String, AttributeValue> exclusiveStartKey = new HashMap<String, AttributeValue>();
        if(!lastEvaluatedKey.isEmpty()) {
            exclusiveStartKey.put("campaingId", lastEvaluatedKey.get("campaingId"));
            exclusiveStartKey.put("subStatus", lastEvaluatedKey.get("subStatus"));
            exclusiveStartKey.put("email", new AttributeValue().withS(email));
        }

        Condition rangeKeyCondition = new Condition()
                .withComparisonOperator(ComparisonOperator.EQ)
                .withAttributeValueList(new AttributeValue().withS(subStatus.toString()));
        DynamoDBQueryExpression queryExpression = null;
        if(exclusiveStartKey.isEmpty()) {
            queryExpression = new DynamoDBQueryExpression<Prospect>()
                    .withHashKeyValues(prospect)
                    .withIndexName("subStatus-index")
                    .withRangeKeyCondition("subStatus", rangeKeyCondition)
                    .withConsistentRead(false).withLimit(limit);
        } else {
             queryExpression = new DynamoDBQueryExpression<Prospect>()
                    .withHashKeyValues(prospect)
                    .withIndexName("subStatus-index")
                    .withExclusiveStartKey(exclusiveStartKey)
                    .withRangeKeyCondition("subStatus", rangeKeyCondition)
                    .withConsistentRead(false).withLimit(limit);
        }
        
        QueryResultPage<Prospect> prospects = mapper.queryPage(Prospect.class, queryExpression);
        return prospects;
    }

But when run it, I am getting below error. Can someone please help me out to understand what I am doing wrong and how can I achieve the pagination in it. enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source