Код: Выделить всё
@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;
}
}
Код: Выделить всё
var attributeValueMap = Map.of(
":status", AttributeValue.fromS(Status.EXPIRED.getValue()),
":expirationTime", DATE_TIME_CONVERTER.transformFrom(now)
);
var scanRequest = ScanRequest.builder()
.tableName(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();
Теперь я хочу использовать индекс, чтобы быстрее загружать данные. Я создаю GSI с именем expirationTime-index с customerIdHash в качестве ключа раздела и expirationTime в качестве ключа сортировки. Затем я переписываю свой код:
Код: Выделить всё
var attributeValueMap = Map.of(
":status", AttributeValue.fromS(ConsentGrantStatus.EXPIRED.getValue()),
":expirationTime", DATE_TIME_CONVERTER.transformFrom(now)
);
DynamoDbIndex index = table(TABLE_NAME, ConsentRecordDynamoDB.class).index(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();
Подробнее здесь: https://stackoverflow.com/questions/787 ... oesnt-work
Мобильная версия