英文:
Android Java Not Updating DynamoDB Table Using Table.updateItem()?
问题
我的表格当前有这个包含7列的项目。user_id是主键。
我执行以下操作来获取当前行,并在该行上更新一个名为“heart_rate_info”的新列表列。
Document newMeasurement = new Document();
newMeasurement.put("time_ms", model.getTime_ms());
newMeasurement.put("session_id", model.getSession_id());
Primitive primitive_user_id = new Primitive(user_id);
Document currentDoc = userTable.getItem(new Primitive(user_id));
Document result;
if(model.getDataType() == MeasurementModel.SENSORS.HEART) {
newMeasurement.put("heart_rate_frequency", model.getRaw_measurement());
if(currentDoc.get("heart_rate_info") != null) {
DynamoDBList heart_rate_info = currentDoc.get("heart_rate_info").asDynamoDBList();
heart_rate_info.add(newMeasurement);
currentDoc.put("heart_rate_info", heart_rate_info);
result = userTable.updateItem(currentDoc, primitive_user_id,
new UpdateItemOperationConfig().withReturnValues(ReturnValue.UPDATED_NEW));
} else {
DynamoDBList heart_rate_info = new DynamoDBList();
heart_rate_info.add(newMeasurement);
currentDoc.put("heart_rate_info", heart_rate_info);
result = userTable.updateItem(currentDoc,
new UpdateItemOperationConfig().withReturnValues(ReturnValue.UPDATED_NEW));
}
}
当我以调试模式运行程序时,我可以看到它获取了正确的行,并使用正确的heart_rate_info数据更新了currentDoc。问题是当我运行
result = userTable.updateItem(currentDoc,
new UpdateItemOperationConfig().withReturnValues(ReturnValue.UPDATED_NEW));
这返回一个空结果。它不是一个null值,也不会抛出错误。只是返回一个空结果,表格没有得到更新。我不太确定出了什么问题。
这是我调用userTable.get时的currentDoc。
这是在调用userTable.updateItem之前的currentDoc。
userTable.putItem
userTable.getItem
两者都正常工作,为什么updateItem不起作用呢?有什么想法吗?
英文:
So my table currently has this item with 7 columns.user_id is the primary key.
I do the following to get the current row and update that row with a new List column called "heart_rate_info" added.
Document newMeasurement = new Document();
newMeasurement.put("time_ms", model.getTime_ms());
newMeasurement.put("session_id", model.getSession_id());
Primitive primitive_user_id = new Primitive(user_id);
Document currentDoc = userTable.getItem(new Primitive(user_id));
Document result;
if(model.getDataType()== MeasurementModel.SENSORS.HEART){
newMeasurement.put("heart_rate_frequency", model.getRaw_measurement());
if(currentDoc.get("heart_rate_info")!=null){
DynamoDBList heart_rate_info = currentDoc.get("heart_rate_info").asDynamoDBList();
heart_rate_info.add(newMeasurement);
currentDoc.put("heart_rate_info", heart_rate_info);
result = userTable.updateItem(currentDoc, primitive_user_id,
new UpdateItemOperationConfig().withReturnValues(ReturnValue.UPDATED_NEW));
}else{
DynamoDBList heart_rate_info = new DynamoDBList();
heart_rate_info.add(newMeasurement);
currentDoc.put("heart_rate_info", heart_rate_info);
result = userTable.updateItem(currentDoc,
new UpdateItemOperationConfig().withReturnValues(ReturnValue.UPDATED_NEW));
}
}
When I run the program in debug mode, I can see that it grabs the right row and updates currentDoc with the correct heart_rate_info data. The problem is when I run
result = userTable.updateItem(currentDoc,
new UpdateItemOperationConfig().withReturnValues(ReturnValue.UPDATED_NEW));
This returns an empty result. It's not a null value, it doesn't throw an error. Just returns an empty result and the table does not get updated. I'm not completely sure what's going on.
This is currentDoc when I call userTable.get
This is currentDoc right before userTable.updateItem is called.
userTable.putItem
userTable.getItem
Both work so why won't updateItem work. Any idea?
答案1
得分: 0
我并不是这个Java API的专家,但从https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPIItemCRUD.html#JavaDocumentAPIItemUpdate看来,updateItem
并不执行你认为它执行的操作:
如果你只想用新项目替换旧项目,请使用 putItem
操作,而不是 updateItem
。使用 updateItem
时,旧项目仅确定项目的 键,然后你应该使用 UpdateExpression
来说明要更新的内容及其方式。这个表达式不是一个完整的新项目 - 有关其语法,请参阅文档。
由于你没有提供任何更新表达式,项目根本没有被更新,因此 UPDATED_NEW
仅返回更新修改的属性 - 也就是说,没有什么东西被修改。
英文:
I'm not an expert on this Java API, but looking at https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPIItemCRUD.html#JavaDocumentAPIItemUpdate it seems that updateItem
does not do what you think it does:
If you want to just replace the old item with the new one, use the putItem
operation, not updateItem
. With updateItem
the old item just determines the key of the item, and then you are supposed to use an UpdateExpression
to say what gets updated and how. This expression isn't simply an entire new item - see the documentation for its syntax.
Because you didn't give any update expression, the item wasn't updated at all, so the UPDATED_NEW
returned only the attributes which the update modified - i.e., nothing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论