简体   繁体   English

使用 GSI 时,DynamoDBmapper 不返回我的所有数据

[英]DynamoDBmapper doesn't return all my data when using GSI

I'm actually using DynamoDBmapper to query my dynamo table.我实际上是在使用 DynamoDBmapper 来查询我的发电机表。 To do so i created two classes that are used by the mapper to convert the queries returns by the table.为此,我创建了两个类,映射器使用它们来转换表返回的查询。 See below见下文

TableName.java表名.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;
}

Salutations.java称呼。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;
}

Then i'm querying my table as follow on my HashKey然后我在我的 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);

and for a GSI i do this对于 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);

Everything is working fine but i got differences between the two queries for the field named typos.一切正常,但我对名为拼写错误的字段的两个查询之间存在差异。 For customer_id i obtain the complete element as follow对于customer_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"
        ]
    }]
}

but for the same user but using his GSI i got as follow但对于同一个用户但使用他的 GSI 我得到如下

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

I dont understand why i obtain different results for a same user.我不明白为什么我会为同一个用户获得不同的结果。

Seems like your DynamoDB GSI is defined with KEYS_ONLY .好像您的 DynamoDB GSI 是用KEYS_ONLY定义的。

You have to project any additional attributes you'd like to have in GSI from the table您必须从表中投影您希望在 GSI 中具有的任何其他属性

Optionally, since the query using the GSI returns the PK of the record, you can use GetItem() on the table.或者,由于使用 GSI 的查询返回记录的 PK,因此您可以在表上使用 GetItem()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM