我正在使用適用于 Scala 的 AWS Dynamo DB 庫 - com.amazonaws.services.dynamodbv2。
早些時候,我有一個帶有主鍵的表,我使用 GetItem 從中獲取特定專案,如下所示:
val item = Try(Option(ddb.getItem(new GetItemRequest().withTableName(table).withKey(Collections.singletonMap(keyField, new AttributeValue(key)))).getItem).map(_.get(valField).getS))
但是現在我需要開始在頂部使用新的排序鍵(創建日期的時間戳)。
這樣我就可以有多個相同的主鍵和不同的時間戳排序鍵。這意味著我將能夠獲得最接近我當前排序時間戳屬性的專案。
我想我需要KeyConditionExpression輸入時間戳大于或等于新排序鍵的位置,并且我看到一個屬性ScanIndexForward可以設定為 true 并結合 limit = 1 所以我只會得到一個專案,它將是最接近的(? )
這應該讓我得到我希望的專案,但我不太確定如何在 Scala 和 AWS 庫中解決這個問題。
uj5u.com熱心網友回復:
如果有人感興趣,這對我有用-
val queryRequest = new QueryRequest().withTableName(table)
.withKeyConditions(createKeyConditionsMap(keyField, keyValue, sortField, sortValue))
.withScanIndexForward(false)
.withLimit(1)
val x = ddb.query(queryRequest).getItems()
def createKeyConditionsMap(keyField: String, keyValue: String, sortField: String, sortValue: String) = {
Map(keyField -> new Condition().withComparisonOperator("EQ").withAttributeValueList(new AttributeValue(keyValue)),
sortField -> new Condition().withComparisonOperator("LE").withAttributeValueList(new AttributeValue().withN(sortValue))).asJava
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/401916.html
上一篇:Akka服務器運行后立即停止
