In my code I use this class as Java entity for DynamoDB table:
@DynamoDbBean
@Data
public class RecordDynamoDB {
private String id;
private String customerIdHash;
private OffsetDateTime expirationTime;
@DynamoDbPartitionKey
public String getId() {
return id;
}
@DynamoDbSecondarySortKey(indexNames = {"expirationTime-index"})
@DynamoDbConvertedBy(DynamoDBTimestampConverter.class)
public OffsetDateTime getExpirationTime() {
return expirationTime;
}
@DynamoDbSecondaryPartitionKey(indexNames = "customerIdHash-index")
public String getCustomerIdHash() {
return customerIdHash;
}
}
This code allows we to look for the items with desired status and expiration time:
var attributeValueMap = Map.of(
":status", AttributeValue.fromS(Status.EXPIRED.getValue()),
":expirationTime", DATE_TIME_CONVERTER.transformFrom(now)
);
var scanRequest = ScanRequest.builder()
.tableName(CONSENTS_TABLE_NAME)
.filterExpression("expirationTime < :expirationTime AND grant.#status = :status")
.expressionAttributeValues(attributeValueMap)
.expressionAttributeNames(Map.of("#status", "status"))
.build();
return dynamoDbClient.scan(scanRequest)
.items()
.stream()
.map(recordTableSchema::mapToItem)
.toList();
This code works. Now I want to use index in order to load data faster. I create GSI with customerIdHash
used as partition key and expirationTime
as sort key. Then I try this code:
var attributeValueMap = Map.of(
":status", AttributeValue.fromS(ConsentGrantStatus.EXPIRED.getValue()),
":expirationTime", DATE_TIME_CONVERTER.transformFrom(now)
);
DynamoDbIndex<ConsentRecordDynamoDB> index = table(CONSENTS_TABLE_NAME, ConsentRecordDynamoDB.class).index(CONSENTS_INDEX_NAME);
var expression = Expression.builder()
.expression("expirationTime < :expirationTime AND grant.#status = :status")
.expressionNames(Map.of("#status", "status"))
.expressionValues(attributeValueMap)
.build();
return index.scan(builder -> builder.filterExpression(expression))
.stream()
.map(Page::items)
.flatMap(List::stream)
.toList();
This code employing index.scan()
doesn’t return any record. What am I doing wrong here?