简体   繁体   English

Java AWS DynamoDB 如何增加数字

[英]Java AWS DynamoDB how to increment number

I'm trying to add 1 to a number, and then get the new number back.我正在尝试将 1 加到一个数字上,然后取回新的数字。

I can't get my UpdateItemSpec right.我无法正确获取我的 UpdateItemSpec。 Please help.请帮忙。 Every example out there seems to show something different and none of it is working.那里的每个示例似乎都显示了一些不同的东西,但都没有奏效。

Here is my code:这是我的代码:

AmazonDynamoDBClient dbClient = new AmazonDynamoDBClient(
            new BasicAWSCredentials("SECRET", "SECRET")
);
dbClient.setRegion(Region.getRegion(Regions.fromName("us-west-1")));
DynamoDB dynamoDB = new DynamoDB(dbClient);
Table table = dynamoDB.getTable("NumTable");

GetItemSpec spec = new GetItemSpec()
  .withPrimaryKey("PKey","OrderNumber");

Item item = table.getItem(spec);
logger.info(item.toJSONPretty());

UpdateItemSpec updateItemSpec = new UpdateItemSpec()
            .withPrimaryKey("Pkey",
                    "OrderNumber")
            .withReturnValues("UPDATED_NEW")
            .withUpdateExpression("ADD #k :incr")
            .withNameMap(new NameMap().with("#k", "NumVal"))
            .withValueMap(
                    new ValueMap()
                            .withNumber(":incr", 1));
                        //.withString(":incr", "{N:\"1\"}"));
                        //I've tried a million other ways too!


UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
logger.info(outcome.getItem().toJSONPretty());

Console shows the first get part working:控制台显示第一个 get 部分正在工作:

Sat Nov 09 00:46:07 UTC - 2019-11-09 00:46:07 f1475303-7585-4804-8a42-2e0a9b16b1dc INFO Commission:88 - {
Sat Nov 09 00:46:07 UTC - "NumVal" : 200000,
Sat Nov 09 00:46:07 UTC - "PKey" : "OrderNumber"
Sat Nov 09 00:46:07 UTC - }

But the update part gives this error (among others):但是更新部分给出了这个错误(等等):

Sat Nov 09 00:46:08 UTC - The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 8TPDT2EVMC0G0GF3IFK7SU6777VV4KQNSO5AEMVJF66Q9ASUAAJG): com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 8TPDT2EVMC0G0GF3IFK7SU6777VV4KQNSO5AEMVJF66Q9ASUAAJG) at [........]

I really feel like the key element does match the schema:'(我真的觉得关键元素确实与架构匹配:'(

Here is a picture from my AWS console:这是来自我的 AWS 控制台的图片:

在此处输入图像描述

Your implementation looks fine to me.你的实现对我来说很好。 The error is because of typo error in your UpdateItemSpec code.该错误是因为 UpdateItemSpec 代码中的拼写错误。

UpdateItemSpec updateItemSpec = new UpdateItemSpec()
            .withPrimaryKey("Pkey",
                    "OrderNumber")

The typo is "Pkey".错字是“Pkey”。 It should be "PKey", which is why it works in GetItemSpec code.它应该是“PKey”,这就是它在 GetItemSpec 代码中起作用的原因。

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

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