如何使用适用于Java的AWS开发工具包v2扫描dynamodb?

时间:2020-01-22 03:18:03

标签: amazon-web-services amazon-dynamodb aws-sdk-java

如何使用aws new sdk v2 for Java执行扫描?那里的文档很少。

1 个答案:

答案 0 :(得分:0)

因为v2 sdk是非常新的,所以文档是scan-t的,所以我不得不深入研究API,并真正地研究如何使用v2 sdk进行简单的扫描+分页。这是为我工作的代码示例。调味。

int count = 0;
DynamoDbClient ddb = DynamoDbClient.create();
ScanRequest request = ScanRequest.builder().tableName("Artists").limit(100).build();
ScanIterable scanResponses = ddb.scanPaginator(request);
List < Map < String, AttributeValue >> items;
Iterator < Map < String, AttributeValue >> map_it;
Map < String, AttributeValue > currentmap = null;
Set < String > mapKeys;
Iterator < String > keys_it;
String currentkey;
for (software.amazon.awssdk.services.dynamodb.model.ScanResponse response: scanResponses) {
    count += response.count();
    items = response.items();
    map_it = items.iterator();
    while (map_it.hasNext()) {
        currentmap = map_it.next();
        mapKeys = currentmap.keySet();
        keys_it = mapKeys.iterator();
        while (keys_it.hasNext()) {
            currentkey = keys_it.next();
            if (currentkey.equals("id")) // my results will have a "number" type id
                System.out.println(currentkey + "=" + currentmap.get(currentkey).n());
            else                         // the rest are strings
                System.out.println(currentkey + "=" + currentmap.get(currentkey).s());
        }
    }
}
System.out.println("count=" + count);

结果如下:

id=252593
object={some: "objectstring"}
artistDiscogs=Joan Jett
coverImage=https://img.discogs.com/CxmwMYC3XxF7CJdpKwCnpQUZEaE=/600x776/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(90)/discogs-images/A-283542-1430342671-6179.jpeg.jpg
name=joan jett