DynamoDB:使用“withExclusiveStartKey"进行分页;在全球二级指数上 [英] DynamoDB : Pagination with "withExclusiveStartKey" on a Global Secondary Index

查看:18
本文介绍了DynamoDB:使用“withExclusiveStartKey"进行分页;在全球二级指数上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为product"的 DynamoDB 表,在userId"上有一个全局二级索引.主键在id"上.我正在尝试在userID"GSI 上使用withExclusiveStartKey"实现分页查询.但是,当我传递一个有效的 lastId 时,我得到以下异常:

I have DynamoDB table called "product" with a Global Secondary Index on "userId".Primary Key is on "id". I am trying to implement Querying with pagination using "withExclusiveStartKey" on "userID" GSI. However, I get following exception when I pass a valid lastId:

独占开始键的大小必须与表的键模式相同(服务:AmazonDynamoDBv2;状态代码:400;错误代码:验证异常;请求编号:822db97e-04a3-4c36-8c72-6008e2693679)

我在这里做错了什么?

public QueryResultPage<Product>  findPaged(String userId,int limit,String lastId) {
        DynamoDBMapper mapper = new DynamoDBMapper(dynamoDb);       
        Map<String, AttributeValue> vals = new HashMap<>();
        vals.put(":valUserId", new AttributeValue().withS(userId));
                DynamoDBQueryExpression<Product> queryExp = new         DynamoDBQueryExpression<Product>()
                .withKeyConditionExpression("userId = :valUserId")
                .withIndexName(ModelConsts.TBL_PRODUCT_GSI_USERID)
                .withExpressionAttributeValues(vals)
                .withScanIndexForward(false)
                .withConsistentRead(false)
                .withLimit(limit);  
           if (lastId != null) {//paging
            Map<String, AttributeValue> exclusiveStartKey = new HashMap<String, AttributeValue>();
                    exclusiveStartKey.put("id", new AttributeValue().withS(lastId));
               queryExp = queryExp.withExclusiveStartKey(exclusiveStartKey);
        }   
        QueryResultPage<Product> result = mapper.queryPage(Product.class, queryExp);
        return result;      
    }

推荐答案

GSI原表的所有key值都应该设置为start key.如果表有分区键和排序键,那么这两个键值都应该设置为起始键值.

All the key values of the original table of GSI should be set as start key. If the table has partition key and sort key, then both the key values should be set as start key values.

在下面的例子中:-

1) videos 表以 videoid 作为分区键,category 作为排序键

1) The videos table has videoid as partition key and category as sort key

2) GSI 定义为 category 作为分区键,videoid 作为排序键

2) The GSI is defined with category as partition key and videoid as sort key

以下代码通过 category 值查询 GSI,并设置了起始键(即分区键和排序键).

The below code queries the GSI by category value with start key set (i.e. both partition and sort key).

当我不填充分区或排序键时,我可以重现您的错误.

I can reproduce your error when I don't populate the partition or sort key.

示例代码:-

public QueryResultPage<VideoDynamoMappingAdapter> findVideosByCategoryUsingGSIAndMapperWithStartKey(
        String category) {
    DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDBClient);
    QueryResultPage<VideoDynamoMappingAdapter> queryResult = null;
    Map<String, AttributeValue> vals = new HashMap<>();
    vals.put(":val1", new AttributeValue().withS(category));
    DynamoDBQueryExpression<VideoDynamoMappingAdapter> queryExp = new DynamoDBQueryExpression<VideoDynamoMappingAdapter>()
            .withKeyConditionExpression("category = :val1").withIndexName("VideoCategoryGsi")
            .withExpressionAttributeValues(vals).withScanIndexForward(false).withConsistentRead(false).withLimit(1);

    Map<String, AttributeValue> startKey = new HashMap<>();

    startKey.put("videoid", new AttributeValue().withS("2"));
    startKey.put("category", new AttributeValue().withS("Thriller"));

    queryExp.setExclusiveStartKey(startKey);

    queryResult = dynamoDBMapper.queryPage(VideoDynamoMappingAdapter.class, queryExp);

    System.out.println("Result size ===>" + queryResult.getResults().size());
    System.out.println("Last evaluated key ===>" + queryResult.getLastEvaluatedKey());

    for (VideoDynamoMappingAdapter videoDynamoMappingAdapter : queryResult.getResults()) {
        System.out.println("Video data ===>" + videoDynamoMappingAdapter.toString());
    }

    return queryResult;

}

这篇关于DynamoDB:使用“withExclusiveStartKey"进行分页;在全球二级指数上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆