private int scanCountInternal(String tableName, Map<String, Condition> filter, int attempt) { LinkedList<Map<String, AttributeValue>> items = new LinkedList<Map<String, AttributeValue>>(); try { ScanRequest request = new ScanRequest(tableName).withScanFilter(filter).withCount(true); ScanResult result = ddb.scan(request); int count = 0; count = count + result.getCount(); // keep repeating until we get through all matched items Key lastKeyEvaluated = null; do { lastKeyEvaluated = result.getLastEvaluatedKey(); if (lastKeyEvaluated != null) { request = new ScanRequest(tableName) .withScanFilter(filter) .withExclusiveStartKey(lastKeyEvaluated) .withCount(true); result = ddb.scan(request); count = count + result.getCount(); } } while (lastKeyEvaluated != null); return count; } catch (AmazonServiceException e) { if (DynamoDBUtil.AWS_ERR_CODE_RESOURCE_NOT_FOUND.equals(e.getErrorCode())) { throw new IllegalArgumentException("no such table: " + tableName, e); } else if (DynamoDBUtil.AWS_STATUS_CODE_SERVICE_UNAVAILABLE == e.getStatusCode()) { // retry after a small pause DynamoDBUtil.sleepBeforeRetry(attempt); attempt++; return scanCountInternal(tableName, filter, attempt); } else { throw new DataStoreOperationException( "problem with table: " + tableName + ", filter: " + filter, e); } } }
@Override public int scan( String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) { logger.debug("scan " + recordcount + " records from key: " + startkey + " on table: " + table); /* * on DynamoDB's scan, startkey is *exclusive* so we need to * getItem(startKey) and then use scan for the res */ GetItemRequest greq = new GetItemRequest(table, createPrimaryKey(startkey)); greq.setAttributesToGet(fields); GetItemResult gres = null; try { gres = dynamoDB.getItem(greq); } catch (AmazonServiceException ex) { logger.error(ex.getMessage()); return SERVER_ERROR; } catch (AmazonClientException ex) { logger.error(ex.getMessage()); return CLIENT_ERROR; } if (null != gres.getItem()) { result.add(extractResult(gres.getItem())); } int count = 1; // startKey is done, rest to go. Key startKey = createPrimaryKey(startkey); ScanRequest req = new ScanRequest(table); req.setAttributesToGet(fields); while (count < recordcount) { req.setExclusiveStartKey(startKey); req.setLimit(recordcount - count); ScanResult res = null; try { res = dynamoDB.scan(req); } catch (AmazonServiceException ex) { logger.error(ex.getMessage()); ex.printStackTrace(); return SERVER_ERROR; } catch (AmazonClientException ex) { logger.error(ex.getMessage()); ex.printStackTrace(); return CLIENT_ERROR; } count += res.getCount(); for (Map<String, AttributeValue> items : res.getItems()) { result.add(extractResult(items)); } startKey = res.getLastEvaluatedKey(); } return OK; }