public boolean deleteAllItems(String tableName) throws DataAccessException { ScanRequest request = new ScanRequest().withTableName(tableName); boolean deleted = false; ScanResult result = ddb.scan(request); for (Map<String, AttributeValue> item : result.getItems()) { Key key = DynamoDBUtil.getIdKey(item); deleteItem(tableName, key); deleted = true; } // keep repeating until we get through all matched items Key lastKeyEvaluated = null; do { lastKeyEvaluated = result.getLastEvaluatedKey(); if (lastKeyEvaluated != null) { request = new ScanRequest(tableName).withExclusiveStartKey(lastKeyEvaluated); result = ddb.scan(request); for (Map<String, AttributeValue> item : result.getItems()) { Key key = DynamoDBUtil.getIdKey(item); deleteItem(tableName, key); deleted = true; } } } while (lastKeyEvaluated != null); return deleted; }
private List<Map<String, AttributeValue>> scanInternal( String tableName, Map<String, Condition> filter, int max, int attempt) { LinkedList<Map<String, AttributeValue>> items = new LinkedList<Map<String, AttributeValue>>(); try { ScanRequest request = new ScanRequest(tableName).withScanFilter(filter); ScanResult result = ddb.scan(request); items.addAll(result.getItems()); // 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); result = ddb.scan(request); items.addAll(result.getItems()); } } while (lastKeyEvaluated != null && items.size() < max); // truncate if needed while (items.size() > max) { items.removeLast(); } return items; } 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 scanInternal(tableName, filter, max, attempt); } else { throw new DataStoreOperationException( "problem with table: " + tableName + ", filter: " + filter, e); } } }