使用 Java 更新 DynamoDB 中的项目
Update item in DynamoDB using Java
我想更新特定项目(行中只有一个数据),
如何更新 DynamoDB 中的项目?
login_id 是我的主键。我传递 login_id 和其他布尔值。
我想根据那个登录 ID 设置布尔值 true。
我该怎么做?
我试过这段代码。
LinkedHashMap inputHashMap = (LinkedHashMap) input;
login_id = (String) inputHashMap.get("login_id");
isUserVerified = (Boolean) inputHashMap.get("isUserVerified");
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient());
Table tableUserDetails = dynamoDB.getTable(USER_DETAILS_TABLE);
Table tableOtpStatus = dynamoDB.getTable(OTP_DETAILS_TABLE);
UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("login_id", login_id);
try{
UpdateItemOutcome outcome = tableUserDetails.updateItem(updateItemSpec);
System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty());
}catch(Exception e){
System.err.println(e.getMessage());
}
在执行上面的代码时,我遇到了以下异常。
The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException;
您似乎缺少更新表达式。假设 isUserVerified 是主键的一部分,它应该是这样的:
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey("login_id", login_id)
.withUpdateExpression("set isUserVerified = :val")
.withValueMap(new ValueMap()
.withBoolean(":val", true));
如果 isUserVerified 不是密钥的一部分,则只需将其从 .withPrimaryKey()
语句中删除即可。
您可以找到更多 here。
我想更新特定项目(行中只有一个数据),
如何更新 DynamoDB 中的项目?
login_id 是我的主键。我传递 login_id 和其他布尔值。 我想根据那个登录 ID 设置布尔值 true。
我该怎么做?
我试过这段代码。
LinkedHashMap inputHashMap = (LinkedHashMap) input;
login_id = (String) inputHashMap.get("login_id");
isUserVerified = (Boolean) inputHashMap.get("isUserVerified");
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient());
Table tableUserDetails = dynamoDB.getTable(USER_DETAILS_TABLE);
Table tableOtpStatus = dynamoDB.getTable(OTP_DETAILS_TABLE);
UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("login_id", login_id);
try{
UpdateItemOutcome outcome = tableUserDetails.updateItem(updateItemSpec);
System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty());
}catch(Exception e){
System.err.println(e.getMessage());
}
在执行上面的代码时,我遇到了以下异常。
The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException;
您似乎缺少更新表达式。假设 isUserVerified 是主键的一部分,它应该是这样的:
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey("login_id", login_id)
.withUpdateExpression("set isUserVerified = :val")
.withValueMap(new ValueMap()
.withBoolean(":val", true));
如果 isUserVerified 不是密钥的一部分,则只需将其从 .withPrimaryKey()
语句中删除即可。
您可以找到更多 here。