我知道这可能是一个非常愚蠢的问题,但我是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: …Run Code Online (Sandbox Code Playgroud) 我正在尝试插入不存在的项目并将其计数器设置为 1 或在它存在时将计数器增加 1 但它似乎不起作用...
这是我到目前为止所做的:
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey("Id", Id)
.withReturnValues(ReturnValue.ALL_NEW)
.withUpdateExpression("set #c = if_not_exists(#c = :val, #c + :val)")
.withNameMap(new NameMap()
.with("#c", "counter"))
.withValueMap(new ValueMap()
.withNumber(":val", 1));
Run Code Online (Sandbox Code Playgroud)
当我将更新表达式更改为set #c = #c + :val它时,它会更新现有项目,但如果它不存在,则不会插入新项目。
set #c = :val 似乎对存在和不存在都有效,但这不是我需要的。