如何使用 Scala 在 Dynamo DB 中查询最接近时间戳的项目

How to query the closest item to timestamp in Dynamo DB with Scala

我正在为 Scala 使用 AWS Dynamo DB 库 - com.amazonaws.services.dynamodbv2.

之前我有一个带主键的 table,我使用 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 与 限制 = 1 所以我只会得到一件,而且是最近的(?)

这应该能让我得到我想要的东西,但我不太确定如何在 Scala 和 AWS 库中处理这个问题。

如果有人感兴趣, 这就是对我有用的 -

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
  }