[Recipes] 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 Steps: 

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.

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for 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

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream