Scanning Data From DynamoDB

Problem: 

You can query or scan data from dynamodb. Try different ways to scan from DynamoDB.

Solution Summary: 

You can do DynamoDB scan in two ways:

  1. Calling scan on a Table object.
  2. Calling scan on the client object of type AmazonDynamoDB.
Prerequisites: 
Solution: 

Calling scan on a Table object can be done as follows:

    @Autowired

    AmazonDynamoDB client;

          System.out.println("Scaning!!!");

        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable(demoTableName);

 

        ScanSpec scanSpec = new ScanSpec();

        List<Map<String, String>> finalList = new ArrayList<>();

        Map<String, String> rowMap = new HashMap<>();

 

        ItemCollection<ScanOutcome> scanItems = table.scan(scanSpec);

 

        Iterator<Item> scanItemsIterator = scanItems.iterator();

 

        while (scanItemsIterator.hasNext()) {

            Item item = scanItemsIterator.next();

            String name = item.getString("Name");

            String desig = item.getString("Designation");

            System.out.println("adding "+name + " as "+ desig);

            rowMap.put(name, desig);

            finalList.add(rowMap);

        }

 

        System.out.println("SCAN LIST RESULT:");

        System.out.println(finalList);

 

Calling scan on the client object of type AmazonDynamoDB can be done as:

    @Autowired

    AmazonDynamoDB client;

        System.out.println("Scanning with ScanRequest");

        ScanRequest scanRequest = new ScanRequest()

                .withProjectionExpression("#Name, Designation")

                .withTableName(demoTableName);

 

        scanRequest.withProjectionExpression("Designation");

        System.out.println(scanRequest.getProjectionExpression());

 

        ScanResult result = client.scan(scanRequest);

        final List<Map<String, AttributeValue>> resultItems = result.getItems();

 

        final List<Map<String, String>> rowMaps = new ArrayList<>();

        resultItems.forEach(map -> {

            final Map<String, String> m = map.entrySet()

                    .stream()

                    .collect(Collectors.toMap(Map.Entry::getKey, v -> v.getValue().getS()));

            rowMaps.add(m);

        });

       

        System.out.println("SCAN LIST RESULT:");

        System.out.println(rowMaps);

 

Both approaches have their own merits; for instance, first approach gives more flexibility, but will require an extra call to dynamodb to first get the table object and then do the query.

Go through the documentation and decide on the best approach suitable for your case.

Contact

We learn together, do innovations and then document them at cloudericks.com.

Offline Contact
We connect physically only in Bengaluru currently, but most of our activities happen online. Please follow us here or in social media for more details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About us

Team Cloudericks is a community started by Heartin Kanikathottu to learn about and master cloud computing. Our current focus is on Amazon Web Servivces.

Cloudericks.com aims to document what we learn. We believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream

,
,