Android Java 不更新 DynamoDB Table 使用 Table.updateItem()?
Android Java Not Updating DynamoDB Table Using Table.updateItem()?
所以我的 table 目前有这个项目有 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));
}
}
当我 运行 程序处于调试模式时,我可以看到它抓取了正确的行并用正确的 [= 更新了 currentDoc 53=] 数据。问题是当我 运行
result = userTable.updateItem(currentDoc,
new UpdateItemOperationConfig().withReturnValues(ReturnValue.UPDATED_NEW));
这 returns 一个空结果。它不是空值,不会引发错误。只是 returns 一个空结果并且 table 没有得到更新。我不太确定发生了什么。
当我调用 userTable.get
时,这是 currentDoc
这是调用 userTable.updateItem 之前的 currentDoc。
userTable.putItem
userTable.getItem
两者都有效,为什么 updateItem 不起作用。有什么想法吗?
我不是这方面的专家 Java API,但看着 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPIItemCRUD.html#JavaDocumentAPIItemUpdate 似乎 updateItem
没有按照您的想法去做:
如果您只想用新项目替换旧项目,请使用 putItem
操作,而不是 updateItem
。使用 updateItem
,旧项目仅确定项目的 key,然后您应该使用 UpdateExpression
来说明更新什么以及如何更新。此表达式不仅仅是一个全新的项目 - 请参阅文档了解其语法。
因为您没有给出任何更新表达式,所以项目根本没有更新,所以 UPDATED_NEW
只返回更新修改的属性 - 即什么都没有。
所以我的 table 目前有这个项目有 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));
}
}
当我 运行 程序处于调试模式时,我可以看到它抓取了正确的行并用正确的 [= 更新了 currentDoc 53=] 数据。问题是当我 运行
result = userTable.updateItem(currentDoc,
new UpdateItemOperationConfig().withReturnValues(ReturnValue.UPDATED_NEW));
这 returns 一个空结果。它不是空值,不会引发错误。只是 returns 一个空结果并且 table 没有得到更新。我不太确定发生了什么。
当我调用 userTable.get
时,这是 currentDoc这是调用 userTable.updateItem 之前的 currentDoc。
userTable.putItem
userTable.getItem
两者都有效,为什么 updateItem 不起作用。有什么想法吗?
我不是这方面的专家 Java API,但看着 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaDocumentAPIItemCRUD.html#JavaDocumentAPIItemUpdate 似乎 updateItem
没有按照您的想法去做:
如果您只想用新项目替换旧项目,请使用 putItem
操作,而不是 updateItem
。使用 updateItem
,旧项目仅确定项目的 key,然后您应该使用 UpdateExpression
来说明更新什么以及如何更新。此表达式不仅仅是一个全新的项目 - 请参阅文档了解其语法。
因为您没有给出任何更新表达式,所以项目根本没有更新,所以 UPDATED_NEW
只返回更新修改的属性 - 即什么都没有。