Enterprise Java

Scan DynamoDB Items with Java

On previous posts we covered how to query a DynamoDB database
Query DynamoDB Part 1
Query DynamoDB Part2.

Apart from issuing queries DynamoDB also offers Scan functionality. What scan does is fetching all the Items you might have on your DynamoDB Table. Therefore scan does not require any rules based on our partition key or your global/local secondary indexes. What scan offers is filtering based on the items already fetched and return specific attributes from the items fetched.

The snippet below issues a scan on the Logins table by adding filtering and selecting only the email field.

public List<String> scanLogins(Date date) {

        List<String> emails = new ArrayList<>();


        Map<String, String> attributeNames = new HashMap<String, String >();
        attributeNames.put("#timestamp", "timestamp");


        Map<String, AttributeValue> attributeValues = new HashMap<String, AttributeValue>();
        attributeValues.put(":from", new AttributeValue().withN(Long.toString(date.getTime())));


        ScanRequest scanRequest = new ScanRequest()
                .withTableName(TABLE_NAME)
                .withFilterExpression("#timestamp < :from")
                .withExpressionAttributeNames(attributeNames)
                .withExpressionAttributeValues(attributeValues)
                .withProjectionExpression("email");

        Map<String,AttributeValue> lastKey = null;

        do {

            ScanResult scanResult = amazonDynamoDB.scan(scanRequest);

            List<Map<String,AttributeValue>> results = scanResult.getItems();
            results.forEach(r->emails.add(r.get("email").getS()));
            lastKey = scanResult.getLastEvaluatedKey();
            scanRequest.setExclusiveStartKey(lastKey);
        } while (lastKey!=null);

        return emails;
    }

Before using scan to an application we have to take into consideration that scan fetches all table items. Therefore It has a high cost both on charges and performance. Also it might consume your provision capacity.
It is better to stick to queries and avoid scans.

You can find the sourcecode on github.

Reference: Scan DynamoDB Items with Java from our JCG partner Emmanouil Gkatziouras at the gkatzioura blog.

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button