gpt4 book ai didi

java - DynamoDB 排序并选择前 20 条记录

转载 作者:行者123 更新时间:2023-11-30 07:51:20 24 4
gpt4 key购买 nike

我想获取像前 10 名或前 20 名这样的记录,基本上是一些可配置的数字,这些记录在应用一些过滤器后根据存储为 ISO-8601 格式 uuuu-MM-dd 的日期字段进行排序。

@DynamoDBTable(tableName = "Data")
@NoArgsConstructor
@ToString
public class Data {

private LocalDate date;
@Setter
private Long number;
@Setter
private STATUS status;

@DynamoDBHashKey
@DynamoDBTypeConverted(converter = LocalDateConverter.class)
public LocalDate getDate() {
return date;
}

@DynamoDBTypeConverted(converter = LocalDateConverter.class)
public void setDate(LocalDate date) {
this.date = date;
}

@DynamoDBIndexHashKey(globalSecondaryIndexName = "idx_Data_number")
public Long getCompetitionId() {
return number;
}

@DynamoDBTypeConvertedEnum
@DynamoDBIndexHashKey(globalSecondaryIndexName = "idx_Data_status")
public STATUS getStatus() {
return status;
}
}

在根据数字和多个状态进行过滤后,我需要根据日期字段排序 20 条记录。我使用 ScanRequest 来基本上过滤记录。现在我想限制结果并根据日期获取热门记录。

@AllArgsConstructor
public class DataRepositoryImpl implements DataRepository {
private final DynamoDBMapper dynamoDBMapper;
private final AmazonDynamoDB amazonDynamoDB;

@Override
public List<Data> findAll(Long number, List<String> status) {
Condition numberCondition = new Condition()
.withComparisonOperator(ComparisonOperator.EQ)
.withAttributeValueList(new AttributeValue().withN(String.valueOf(number)));

List<AttributeValue> attributeValues = newArrayList();
status.forEach(s -> attributeValues.add(new AttributeValue().withS(s)));
Condition statusCondition = new Condition()
.withComparisonOperator(ComparisonOperator.IN)
.withAttributeValueList(attributeValues);

Map<String, Condition> conditions = newHashMap();
conditions.put("number", numberCondition);
conditions.put("status", statusCondition);

ScanRequest scanRequest = new ScanRequest()
.withTableName("Data")
.withScanFilter(conditions)
.withConditionalOperator(ConditionalOperator.AND);

ScanResult scanResult = amazonDynamoDB.scan(scanRequest);
return dynamoDBMapper.marshallIntoObjects(Data.class, scanResult.getItems());
}
}

最佳答案

DynamoDB 仅按SORT KEY 属性对数据进行排序。

DynamoDB 没有按除排序键之外的任何属性(即任何非键属性或分区键属性)对数据进行排序的功能。

Partition key and sort key – Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key. DynamoDB uses the partition key value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. All items with the same partition key are stored together, in sorted order by sort key value.

DynamoDB 将数据存储在多个分区中。与 RDBMS 一样,它也没有任何 ORDER BY 子句。但是,它确实有 ScanIndexForward 选项,该选项仅适用于 Sort 键属性。

ScanIndexForward — (Boolean) Specifies the order for index traversal: If true (default), the traversal is performed in ascending order; if false, the traversal is performed in descending order.

关于java - DynamoDB 排序并选择前 20 条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47305768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com