使用受限读取或吞吐量限制扫描 dynamoDB 表

Cru*_*ata 2 nosql amazon-dynamodb

有没有人有一个示例 java 代码在 dynamoDB 表上执行扫描操作,其中扫描操作仅使用吞吐量限制的特定百分比?提前致谢。

Dav*_*cek 5

昨天,我们在AWS Java 开发人员博客上发表了一篇关于如何在 Amazon DynamoDB 中进行速率限制扫描的博文。我不确定您使用的是哪种编程语言,但如果您使用的是 Java,这种使用 GoogleGuava RateLimiter类的方法可能适合您。但格雷格之前的回复也是正确的。如果您使用 Amazon Elastic Map Reduce,DynamoDB 插件支持可配置的读取和写入吞吐量百分比,以在扫描您的表时限制自身。DynamoDB 的Amazon Redshift 集成也有此设置。

以下是博客文章中的一个片段,展示了如何使用 RateLimiter 和适用于 JavaAWS 开发工具包执行分页扫描,该扫描将自身限制为每秒消耗 25 个读取容量单位:

// Initialize the rate limiter to allow 25 read capacity units / sec
RateLimiter rateLimiter = RateLimiter.create(25.0);

// Track how much throughput we consume on each page
int permitsToConsume = 1;

// Initialize the pagination token
Map<String, AttributeValue> exclusiveStartKey = null;

do {
    // Let the rate limiter wait until our desired throughput "recharges"
    rateLimiter.acquire(permitsToConsume);

    // Do the scan
    ScanRequest scan = new ScanRequest()
        .withTableName("ProductCatalog")
        .withLimit(100)
        .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
        .withExclusiveStartKey(exclusiveStartKey);
    ScanResult result = dynamodb.scan(scan);
    exclusiveStartKey = result.getLastEvaluatedKey();

    // Account for the rest of the throughput we consumed,
    // now that we know how much that scan request cost
    double consumedCapacity = result.getConsumedCapacity().getCapacityUnits();
    permitsToConsume = (int)(consumedCapacity - 1.0);
    if(permitsToConsume <= 0) {
        permitsToConsume = 1;
    }

    // Process results here
    processYourResults(result);

} while (exclusiveStartKey  != null);
Run Code Online (Sandbox Code Playgroud)

  • 为什么 -1.0 在这一行?permitToConsume = (int)(consumedCapacity - 1.0); (3认同)