Gee*_*arn 19 java amazon-dynamodb
我知道这可能是一个非常愚蠢的问题,但我是DynamoDB的新手.
我的疑问是可以在DynamoDB中更新Range键的值.
假设我的表是"测试"
{
ID : PK/HK
Date : RK
Name : GSI
Add : LSI
}
Run Code Online (Sandbox Code Playgroud)
我想修改Date属性.
表中的初始值为:
{
ID = "344"
Date = "5656"
Name = "ABC"
}
Run Code Online (Sandbox Code Playgroud)
在下面运行此代码.我能够更改Name属性,即GSI.
Map<String,AttributeValue> item = new HashMap<String,AttributeValue>();
item.put("ID", new AttributeValue("344"));
item.put("Date", new AttributeValue("5656"));
Map<String,AttributeValueUpdate> item1 = new HashMap<String,AttributeValueUpdate>();
AttributeValueUpdate update = new AttributeValueUpdate().withValue(new AttributeValue("AMIT")).withAction("PUT");
item1.put("Name", update);
UpdateItemRequest updateItemreq = new UpdateItemRequest("Test",item,item1);
UpdateItemResult updateItemres = dynamoDBUSEast.updateItem(updateItemreq);
Run Code Online (Sandbox Code Playgroud)
但是,当我改变这一行
item1.put("Name", update);
Run Code Online (Sandbox Code Playgroud)
同
item1.put("Date", update);
Run Code Online (Sandbox Code Playgroud)
我收到一些错误
Exception in thread "main" com.amazonaws.AmazonServiceException: One or more parameter values were invalid: Cannot update attribute Date. This attribute is part of the key (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: HRRP24Q7C48AMD8ASAI992L6MBVV4KQNSO5AEMVJF66Q9ASUAAJG)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:820)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:439)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:245)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:2908)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.updateItem(AmazonDynamoDBClient.java:1256)
Run Code Online (Sandbox Code Playgroud)
那么是否可以更改范围键值?
mko*_*bit 14
不,就像异常消息所说,你Cannot update attribute Date. This attribute is part of the key.
您还可以在AttributeUpdates文档下看到:
要修改的属性的名称,要对每个属性执行的操作以及每个属性的新值.如果要更新属性,该属性是该表上任何索引的索引键属性,则属性类型必须与表描述的AttributesDefinition中定义的索引键类型匹配.您可以使用UpdateItem更新任何非键属性.
该文档声明您可以更新"作为该表上任何索引的索引键属性的属性"的任何属性,这意味着当您更新投影到索引上的属性时,即使它是该索引的一部分键,该索引也将更新以反映原始项目.
| 归档时间: |
|
| 查看次数: |
14447 次 |
| 最近记录: |