dynamodbmapper在使用gsi时不会返回我的所有数据

cuxqih21  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(381)

我实际上使用dynamodbmapper来查询我的dynamo表。为此,我创建了两个类,Map器使用它们来转换表返回的查询。见下文
表名.java

@DynamoDBTable(tableName = "customer_table")
public class CustomerTable {

    @DynamoDBHashKey(attributeName = "customer_id")
    String customerId;

    @DynamoDBIndexHashKey(attributeName = "customer_id_bis", globalSecondaryIndexName = "customer_id_bis-index")
    String customerIdBis;

    @DynamoDBIndexHashKey(attributeName = "customer_id_tier", globalSecondaryIndexName = "customer_id_tier-index")
    String customerIdTier;

    @DynamoDBAttribute(attributeName = "salutations")
    List<Salutations> salutations;
}

问候语.java

@DynamoDBDocument
public class Salutations {

    @DynamoDBAttribute(attributeName = "salutations_id")
    private String salutationsId;

    @DynamoDBAttribute(attributeName = "rank")
    private Integer rank;

    @DynamoDBAttribute(attributeName = "score")
    private String score;

    @DynamoDBAttribute(attributeName = "typos")
    private List<String> typos;
}

然后我在hashkey上查询我的表

Map<String, AttributeValue> keyToGet = Map.of(":column", new AttributeValue(value));
DynamoDBQueryExpression<CustomerTable> queryExpression = new DynamoDBQueryExpression<CustomerTable>()
                .withKeyConditionExpression("customer_id = :column")
                .withExpressionAttributeValues(keyToGet);
mapper.query(CustomerTable.class, queryExpression);

对于一个gsi我做这个

Map<String, AttributeValue> keyToGet = Map.of(":column", new AttributeValue(value));
DynamoDBQueryExpression<CustomerTable> queryExpression = new DynamoDBQueryExpression<CustomerTable>()
                .withIndexName("customer_id_bis-index")
                .withKeyConditionExpression("customer_id_bis = :column")
                .withConsistentRead(false)
                .withExpressionAttributeValues(keyToGet);
mapper.query(CustomerTable.class, queryExpression);

一切工作正常,但我得到了两个名为typos字段的查询之间的差异。对于customer\u id,我获得如下完整元素

{
    "customer_id":"jean",
    "customer_id_bis":"paul",
    "customer_id_tier":"sartre",
    "salutations": [{
        "salutations_id": "12345",
        "rank": 1,
        "score": "0.6796357154846191",
        "typos": [        
            "185",
            "198",
            "199"
        ]
    }]
}

但对于同一个用户,但使用他的gsi,我得到如下结果

{
    "customer_id":"jean",
    "customer_id_bis":"paul",
    "customer_id_tier":"sartre",
    "salutations": [{
        "salutations_id": "12345",
        "rank": 1,
        "score": "0.6796357154846191",
        "typos": []
    }]
}

我不明白为什么我对同一个用户得到不同的结果。

tcbh2hod

tcbh2hod1#

似乎你的dynamodb gsi是用 KEYS_ONLY .
您必须从表中投影您希望在gsi中具有的任何附加属性
或者,由于使用gsi的查询返回记录的主键,因此可以对表使用getitem()。

相关问题