簡體   English   中英

使用適用於Java 2.x的AWS DynamoDB SDK掃描表

[英]Scan a table with AWS DynamoDB SDK for Java 2.x

我在Amazon DynamoDB中有一個表( users ),其中包含以下項:

{
   id: "ef1e44bc-03ad-11e9-8eb2-f2801f1b9fd1", // UUID, HASH key
   version: "3.1",                             // Version string, conforming to semver standards
   emailsEnabled: true,                        // Boolean flag
   capabilities: {                             // Big nested object, not important for the question
      …
   }
}

我想進行臨時掃描,以了解有多少使用3.1版的用戶啟用了電子郵件。 我在此表中沒有任何索引,但是可以進行掃描。

如何使用適用於Java 2.x的AWS開發工具包做到這一點?

您必須使用過濾器表達式來限制您的應用處理的數據量。

您還可以使用ProjectionExpressions擺脫掃描結果中其他不重要的屬性。

這是代碼:

DynamoDbClient client = DynamoDbClient.builder().build();
ScanRequest request =
    ScanRequest
        .builder()
        .tableName("users")
        .filterExpression("version = :version")
        .expressionAttributeValues(
            Map.of(":version", AttributeValue.builder().s("3.1").build()) // Using Java 9+ Map.of
        )
        .projectionExpression("id, version, emailsEnabled")
        .build();
ScanIterable response = client.scanPaginator(request);

for (ScanResponse page : response) {
    for (Map<String, AttributeValue> item : page.items()) {
        // Consume the item
        System.out.println(item);

        if (item.get("emailsEnabled").bool()) {
            // Update counters
        }
    }
}

請注意,在掃描完成之后但返回結果之前將應用過濾器表達式。 因此,無論是否存在過濾器表達式,掃描都將消耗相同數量的讀取容量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM