我正在尝试在dynamoDB上运行查询,以便在XField Index中为我提供基于xField的最新项目

DyanmoDB pojo

@EqualsAndHashCode(exclude = "rvn")
    @DynamoDBTable(tableName = "Test")
    public class Test {

    @DynamoDBHashKey(attributeName = "uniqueId")
    @DynamoDBAutoGeneratedKey
    private String uniqueId;

    @DynamoDBIndexHashKey(globalSecondaryIndexName = "xField-index", attributeName = "xField")
    private String xField;

    @DynamoDBAutoGeneratedTimestamp(strategy = DynamoDBAutoGenerateStrategy.CREATE)
    @DynamoDBAttribute(attributeName = "createdAt")
    private Date createdAt;

    @DynamoDBAutoGeneratedTimestamp(strategy = DynamoDBAutoGenerateStrategy.ALWAYS)
    @DynamoDBRangeKey(attributeName = "lastUpdatedAt")
    private Long lastUpdatedAt;

    @DynamoDBVersionAttribute(attributeName = "rvn")
    private Long rvn;

    }

表哈希键是使用@DynamoDBAutoGeneratedKey注释自动生成的 . 我在网上搜索并发现我可以使用withScanIndexForward,但是当我深入了解时,我会知道withScanIndexForward如何工作:

具有相同分区键值的项目按排序键按排序顺序存储 . 如果排序键数据类型为Number,则结果将按数字顺序存储 . 对于String类型,结果按UTF-8字节的顺序存储 . 对于Binary类型,DynamoDB将二进制数据的每个字节视为无符号 .

如果ScanIndexForward为true,DynamoDB将按照存储顺序返回结果(按排序键值) . 这是默认行为 . 如果ScanIndexForward为false,则DynamoDB按排序键值以相反顺序读取结果,然后将结果返回给客户端 .

就我而言,没有排序键 . 所以,当我运行下面提到的查询时,我得到随机数据 .

final Map<String, String> attributeNameMap = new HashMap<>();
        attributeNameMap.put("#t", "XField");
     final Map<String, AttributeValue> attributeValueMap = new HashMap<>();
        attributeValueMap.put(":v1", new AttributeValue().withS(value));

     DynamoDBQueryExpression<SkillOperationData> expression = new 
         DynamoDBQueryExpression<Test>()
                .withIndexName("XField-index")
                .withConsistentRead(false)
                .withKeyConditionExpression("#t = :v1")
                .withScanIndexForward(false)
                .withLimit(1);

有人可以帮我找到解决这个问题的方法 .