Tuesday, September 30, 2014

How to fetch all results from DynamoDB

Dynamo DB simple scan will not give you all the results.


  
  scanTable(List attributesToGet) {
  List<> allRows = new LinkedList<>(); 
  ScanResult result = null;
  do{
   ScanRequest scanRequest = new ScanRequest().withTableName(this.tableName).withAttributesToGet(attributesToGet);  
   if(result != null){
    scanRequest.setExclusiveStartKey(result.getLastEvaluatedKey());
      }
   long time = System.currentTimeMillis();
   result = this.client.scan(scanRequest);
   allRows.addAll(result.getItems());
   
   log.info("scan in: " + (System.currentTimeMillis() - time) + " ms");
  } while(result.getLastEvaluatedKey() != null);
  
  return allRows;
 }