【问题标题】:Query More Than 100 DynamoDB Items that match a Partition Key using Java SDK 2?使用 Java SDK 2 查询超过 100 个与分区键匹配的 DynamoDB 项目?
【发布时间】:2021-05-17 22:10:06
【问题描述】:

如何使用 aws java sdk 2 查询 dynamodb 表中匹配特定分区键的 100 多个项目?

如果需要使用匹配哈希键exceeds 100 then the batchGetItemPaginator method的行数。这个方法接受一个BatchGetItemRequest,它需要一个KeysAndAttributes

当我只使用哈希键而不是排序键构造 KeysAndAttributes 时,我得到以下异常

DynamoDbException:提供的关键元素与架构不匹配

但是,如果我在 KeysAndAttributes 中提供了一个排序键,那么 batchGetItemPaginator 方法就可以正常工作。

那么如何使用主键(无排序键)构造 BatchGetItemRequest?

注意:我的问题与this question 不重复。如果行数超过 100,则其他问题不涉及所需的分页。

提前感谢您的考虑和回复。

【问题讨论】:

    标签: java amazon-web-services aws-java-sdk-2.x aws-java-sdk-dynamodb


    【解决方案1】:

    主键>>分区+HashKey

    GetItem 用于通过主键获取一项。

    BatchGetItem 用于使用多个主键从多个表中获取多个项目。所以,我们需要传递 Partition 和 HashKey 的列表

    我们永远不能使用部分密钥(即分区密钥)执行 getItem 或 batchGetItem。

    要仅通过分区键获取项目,我们必须使用query api,它最多返回 1 MB 的数据。要获取具有特定分区键的所有记录,我们可以通过exclusiveStartKey多次调用查询api来进行分页

    .exclusiveStartKey(exclusiveStartKey) 帮助我们在特定记录之后开始。

    .limit(10) 通常设置为不超过 1 MB 的总大小的合理数字。如果没有项目不是问题,我们可以排除它。

    response.lastEvaluatedKey() 从响应中捕获此内容以用于下一个查询。

    public static Map<String, AttributeValue> queryTable(DynamoDbClient ddb, String tableName, String partitionKeyName,
            String partitionKeyVal, Map<String, AttributeValue> exclusiveStartKey) {
    
        Map<String, AttributeValue> lastEvaluatedKey = null;
    
        HashMap<String, AttributeValue> attrValues = new HashMap<String, AttributeValue>();
        attrValues.put(":" + partitionKeyName, AttributeValue.builder().s(partitionKeyVal).build());
        QueryRequest queryReq = QueryRequest.builder().tableName(tableName).exclusiveStartKey(exclusiveStartKey)
                .keyConditionExpression(partitionKeyName + " = :" + partitionKeyName)
                .expressionAttributeValues(attrValues).limit(10).build();
    
        try {
            QueryResponse response = ddb.query(queryReq);
            lastEvaluatedKey = response.lastEvaluatedKey();
        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return lastEvaluatedKey;
    }
    

    pk 作为值1 的分区键对表test 进行样本测试

    public static void main(String[] args) {
    
        Region region = Region.US_EAST_1;
        DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    
        Map<String, AttributeValue> lastEvaluatedKey = null;
        // Loop until we don't get lastEvaluatedKey 
        do {
            System.out.println("---------DynamoDb Query with Exclusive Start Key------------" + lastEvaluatedKey);
            lastEvaluatedKey = queryTable(ddb, "test", "pk", "1", lastEvaluatedKey);
        } while (lastEvaluatedKey != null && !lastEvaluatedKey.isEmpty());
    
        ddb.close();
    }
    

    【讨论】:

    • 感谢您的回复! queryBatchGetItem 之间的澄清非常有帮助。但是,查询文档指出响应最多为 1 MB。您能否更新您的示例以包括分页?
    • @RamónJRomeroyVigil 使用分页查询示例更新了答案
    猜你喜欢
    • 2022-09-28
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2019-09-05
    • 2018-10-31
    • 2021-05-30
    相关资源
    最近更新 更多