如何使用使用封送器的范围密钥查询 DynamoDB GSI



我有一个表,因为我有一个二级索引。我的二级索引使用 DynamoDB 封送。

如何查询此 GSI 上的表?addRangeKeyCondition 仅支持 withS 和 withN 方法。如何使用我的对象查询它?这就是我查询 Range 键是否为字符串的方式:

DynamoDBQueryExpression<RequestPerOfferItem> queryExpr = new DynamoDBQueryExpression<>();
queryExpr.withHashKeyValues(item).withRangeKeyCondition( "KeyName",
              new Condition().withAttributeValueList(new AttributeValue().withS(val)).withComparisonOperator(
                      ComparisonOperator.EQ));

但我不能这样做,因为我的范围键使用编组器。如何使用此范围键查询我的 GSI?

您可以自己使用编组器来获取对象的字符串表示形式:

public static class YourObjectMarshaller implements DynamoDBMarshaller<YourObject>
{
   public static final YourObjectMarshaller instance = new YourObjectMarshaller();
...
}

然后你可以自己使用它作为YourObjectMarshaller.instance.marshall(obj),并将其作为字符串传递与S。

如果您使用的是 DynamoDBMapper,则可以使用 @DynamoDBMarshalling 注释并指定要用于对象的DynamoDBMarshaller

@DynamoDBMarshalling(YourObjectMarshaller.class)
public YourObject getYourObject() {
    ....
}

最新更新