我正在使用 Dynamodb Item - getItemAPI 從 DynamoDB 表中獲取記錄。但它回傳Item物件,我想從Item物件中檢索特定的屬性值。我們如何在 Java 中做到這一點?我找不到參考資料。
Table table = dynamoDB.getTable(tableName);
Item item = table.getItem(hashKeyFieldName, hashKeyFieldValue);
該專案包含以下欄位:
HashKey, TimeStamp, NumRetries
我想從item上面獲取特定的 NumRetries 值。這是可能的嗎?像int numRetries = item.get("NumRetries");什么?
uj5u.com熱心網友回復:
您應該可以使用Projection Expression做到這一點:
GetItemSpec spec = new GetItemSpec().withPrimaryKey("primaryKey", primaryKey)
.withProjectionExpression("HashKey, TimeStamp, NumRetries");
Item outcome = table.getItem(spec);
可能需要一張姓名地圖。
uj5u.com熱心網友回復:
您可以使用投影運算式從專案中獲取某些屬性,但請記住,使用投影運算式不會減少用于檢索物件的 RCU 的使用和成本。
代碼示例,
GetItemSpec spec = new GetItemSpec()
.withPrimaryKey("YourPrimaryKey", value)
.withProjectionExpression("NumRetries");
Item item = table.getItem(spec);
System.out.println(item.toJSONPretty());
可以在此處找到更多代碼示例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/388079.html
