【问题标题】:Update Dynamo DB column based on GSI基于 GSI 更新 Dynamo DB 列
【发布时间】:2020-02-04 18:03:20
【问题描述】:

我创建了一个包含 4 列的表 TEST:

column1 : 范围键

column2:排序键

第 3 列:GSI

column4:普通属性

现在我想根据 GSI 值更新 column4 的值。我尝试使用以下代码,但它仅在我同时传递范围和排序键时才有效。在我更新时的用例中,我只有 GSI 的值而不是范围/排序键。

Map<String, AttributeValue> key = new HashMap<>();
key.put(“column1”, new AttributeValue().withS(column1Value));
key.put(“column2”, new AttributeValue().withS(column2Value));
Map<String, AttributeValue> attributeValues = new HashMap<>();

attributeValues.put(“column4”, new AttributeValue().withS(column4Value));
attributeValues.put(“column3”, new AttributeValue().withS(column3Value));

UpdateItemRequest updateItemRequest = new UpdateItemRequest()
                .withTableName(emailsTableName)
                .withKey(key)
                .withUpdateExpression(“set column4 = :column4”)
                .withConditionExpression(“column3 = :column3”)
                .withExpressionAttributeValues(attributeValues);
UpdateItemResult updateItemResult = dynamoDBClient.updateItem(updateItemRequest);

这是否可以仅基于 GSI 更新 Dynamo DB 的列?

【问题讨论】:

  • column3 值与表上的实际全局二级索引之间是否存在联系?为什么要存储 GSI?
  • @jarmod 列名不是 GSI 。列名仅是 column4,我们已经在此列上创建了 GSI。
  • 要更新一个项目,你需要它的主键。如果您有一个二级索引可以帮助您找到感兴趣的项目,那么您可以这样做,检索主键,然后执行更新。
  • @jarmod 所以你说 GSI 只用于定位。我们不能根据它们执行更新吗?
  • 要发布更新,您需要一个主键。如果您使用 GSI 查询,这将可用,因为基表的主键属性始终投影到索引中。

标签: java amazon-web-services amazon-dynamodb amazon-dynamodb-index


【解决方案1】:

GSI 用于查询数据,因此要执行此操作,您需要先使用 GSI 查询数据,然后使用其中的结果通过响应中的键了解要更新的记录。请记住,对于 GSI,它可能是记录,复数。不保证唯一性。

【讨论】:

  • 感谢您的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
  • 2016-02-15
  • 2017-02-18
相关资源
最近更新 更多