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

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

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

  1. @DynamoDBTable(tableName = "customer_table")
  2. public class CustomerTable {
  3. @DynamoDBHashKey(attributeName = "customer_id")
  4. String customerId;
  5. @DynamoDBIndexHashKey(attributeName = "customer_id_bis", globalSecondaryIndexName = "customer_id_bis-index")
  6. String customerIdBis;
  7. @DynamoDBIndexHashKey(attributeName = "customer_id_tier", globalSecondaryIndexName = "customer_id_tier-index")
  8. String customerIdTier;
  9. @DynamoDBAttribute(attributeName = "salutations")
  10. List<Salutations> salutations;
  11. }

问候语.java

  1. @DynamoDBDocument
  2. public class Salutations {
  3. @DynamoDBAttribute(attributeName = "salutations_id")
  4. private String salutationsId;
  5. @DynamoDBAttribute(attributeName = "rank")
  6. private Integer rank;
  7. @DynamoDBAttribute(attributeName = "score")
  8. private String score;
  9. @DynamoDBAttribute(attributeName = "typos")
  10. private List<String> typos;
  11. }

然后我在hashkey上查询我的表

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

对于一个gsi我做这个

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

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

  1. {
  2. "customer_id":"jean",
  3. "customer_id_bis":"paul",
  4. "customer_id_tier":"sartre",
  5. "salutations": [{
  6. "salutations_id": "12345",
  7. "rank": 1,
  8. "score": "0.6796357154846191",
  9. "typos": [
  10. "185",
  11. "198",
  12. "199"
  13. ]
  14. }]
  15. }

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

  1. {
  2. "customer_id":"jean",
  3. "customer_id_bis":"paul",
  4. "customer_id_tier":"sartre",
  5. "salutations": [{
  6. "salutations_id": "12345",
  7. "rank": 1,
  8. "score": "0.6796357154846191",
  9. "typos": []
  10. }]
  11. }

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

tcbh2hod

tcbh2hod1#

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

相关问题