首页 文章

如何使用使用编组器的RangeKey查询DynamoDB GSI

提问于
浏览
2

我有一张 table ,因为我有一个二级索引 . 我的二级索引使用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?

2 回答

  • 1

    您可以自己使用marshaller来获取对象的String表示:

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

    然后你可以自己使用 YourObjectMarshaller.instance.marshall(obj) 并将其作为String withS传递 .

  • 0

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

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

相关问题