@Override public <P extends ParaObject> void deleteAll(String appid, List<P> objects) { if (objects == null || objects.isEmpty() || StringUtils.isBlank(appid)) { return; } List<WriteRequest> reqs = new ArrayList<>(objects.size()); for (ParaObject object : objects) { if (object != null) { reqs.add(new WriteRequest().withDeleteRequest(new DeleteRequest(). withKey(Collections.singletonMap(Config._KEY, new AttributeValue(getKeyForAppid(object.getId(), appid)))))); } } batchWrite(Collections.singletonMap(getTableNameForAppid(appid), reqs)); logger.debug("DAO.deleteAll() {}", objects.size()); }
protected int addDeleteRequest(AttributeValue tableHashKey, AttributeValue tableRangeKey) { Map<String, AttributeValue> primaryKey = genTablePrimaryKey(tableHashKey, tableRangeKey); batchDeleteRequests.add(new WriteRequest().withDeleteRequest(new DeleteRequest().withKey(primaryKey))); int deletedItems = 0; if (batchDeleteRequests.size() == MAX_BATCH_WRITE_REQUEST_NUM) { deletedItems = sendDeleteRequests(); } return deletedItems; }
/** * @{inheritDoc */ @Override public void deleteForJob(final String tableName, final String jobId, final boolean asynch) { Runnable task = new Runnable() { public void run() { MethodTimer mt = new MethodTimer(logger, this.getClass(), "deleteForJob (" + jobId + ")"); List<Item> items = getItems(tableName, null, null, null, jobId); if (!items.isEmpty()) { List<WriteRequest> requests = new ArrayList<WriteRequest>(); try { for (Item item : items) { String id = null; for (Attribute attr : item.getAttributes()) { if (DatabaseKeys.REQUEST_NAME_KEY.getShortKey().equals(attr.getName())) { id = attr.getValue(); break; } } if (id != null) { Map<String, AttributeValue> keyMap = new HashMap<String, AttributeValue>(); keyMap.put(DatabaseKeys.REQUEST_NAME_KEY.getShortKey(), new AttributeValue().withS(id)); keyMap.put(DatabaseKeys.JOB_ID_KEY.getShortKey(), new AttributeValue().withS(jobId)); DeleteRequest deleteRequest = new DeleteRequest().withKey(keyMap); WriteRequest writeRequest = new WriteRequest().withDeleteRequest(deleteRequest); requests.add(writeRequest); } } sendBatch(tableName, requests); } catch (Exception t) { logger.error("Error adding results: " + t.getMessage(), t); throw new RuntimeException(t); } } mt.endAndLog(); } }; if (asynch) { EXECUTOR.execute(task); } else { task.run(); } }
/** * Deletes all objects in a shared table, which belong to a given appid, by scanning the GSI. * @param appid app id */ public static void deleteAllFromSharedTable(String appid) { if (StringUtils.isBlank(appid) || !isSharedAppid(appid)) { return; } Pager pager = new Pager(50); List<WriteRequest> allDeletes = new LinkedList<>(); Page<Item, QueryOutcome> items; // read all phase do { items = queryGSI(appid, pager); if (items == null) { break; } for (Item item : items) { String key = item.getString(Config._KEY); // only delete rows which belong to the given appid if (StringUtils.startsWith(key, appid.trim())) { logger.debug("Preparing to delete '{}' from shared table, appid: '{}'.", key, appid); pager.setLastKey(item.getString(Config._ID)); allDeletes.add(new WriteRequest().withDeleteRequest(new DeleteRequest(). withKey(Collections.singletonMap(Config._KEY, new AttributeValue(key))))); } } } while (items.iterator().hasNext()); // delete all phase final int maxItems = 20; int batchSteps = (allDeletes.size() > maxItems) ? (allDeletes.size() / maxItems) + 1 : 1; List<WriteRequest> reqs = new LinkedList<>(); Iterator<WriteRequest> it = allDeletes.iterator(); String tableName = getTableNameForAppid(appid); for (int i = 0; i < batchSteps; i++) { while (it.hasNext() && reqs.size() < maxItems) { reqs.add(it.next()); } if (reqs.size() > 0) { logger.info("Deleting {} items belonging to app '{}', from shared table (page {}/{})...", reqs.size(), appid, i + 1, batchSteps); batchWrite(Collections.singletonMap(tableName, reqs)); } reqs.clear(); } }
private static void writeMultipleItemsBatchWrite() { try { // Begin syntax extract // Create a map for the requests in the batch Map<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>(); // Create a PutRequest for a new Forum item Map<String, AttributeValue> forumItem = new HashMap<String, AttributeValue>(); forumItem.put("Name", new AttributeValue().withS("Amazon RDS")); forumItem.put("Threads", new AttributeValue().withN("0")); List<WriteRequest> forumList = new ArrayList<WriteRequest>(); forumList.add(new WriteRequest().withPutRequest(new PutRequest().withItem(forumItem))); requestItems.put("Forum", forumList); // Create a PutRequest for a new Thread item Map<String, AttributeValue> threadItem = new HashMap<String, AttributeValue>(); threadItem.put("ForumName", new AttributeValue().withS("Amazon RDS")); threadItem.put("Subject", new AttributeValue().withS("Amazon RDS Thread 1")); List<WriteRequest> threadList = new ArrayList<WriteRequest>(); threadList.add(new WriteRequest().withPutRequest(new PutRequest().withItem(threadItem))); // Create a DeleteRequest for a Thread item Map<String, AttributeValue> threadDeleteKey = new HashMap<String, AttributeValue>(); threadDeleteKey.put("ForumName", new AttributeValue().withS("Some hash attribute value")); threadDeleteKey.put("Subject", new AttributeValue().withS("Some range attribute value")); threadList.add(new WriteRequest().withDeleteRequest(new DeleteRequest().withKey(threadDeleteKey))); requestItems.put("Thread", threadList); BatchWriteItemRequest batchWriteItemRequest = new BatchWriteItemRequest(); System.out.println("Making the request."); batchWriteItemRequest.withRequestItems(requestItems); client.batchWriteItem(batchWriteItemRequest); // End syntax extract } catch (AmazonServiceException ase) { System.err.println("Failed to retrieve items: "); ase.printStackTrace(System.err); } }
private static void writeMultipleItemsBatchWrite() { try { // Create a map for the requests in the batch Map<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>(); // Create a PutRequest for a new Forum item Map<String, AttributeValue> forumItem = new HashMap<String, AttributeValue>(); forumItem.put("Name", new AttributeValue().withS("Amazon RDS")); forumItem.put("Threads", new AttributeValue().withN("0")); List<WriteRequest> forumList = new ArrayList<WriteRequest>(); forumList.add(new WriteRequest().withPutRequest(new PutRequest().withItem(forumItem))); requestItems.put(table1Name, forumList); // Create a PutRequest for a new Thread item Map<String, AttributeValue> threadItem = new HashMap<String, AttributeValue>(); threadItem.put("ForumName", new AttributeValue().withS("Amazon RDS")); threadItem.put("Subject", new AttributeValue().withS("Amazon RDS Thread 1")); threadItem.put("Message", new AttributeValue().withS("ElasticCache Thread 1 message")); threadItem.put("KeywordTags", new AttributeValue().withSS(Arrays.asList("cache", "in-memory"))); List<WriteRequest> threadList = new ArrayList<WriteRequest>(); threadList.add(new WriteRequest().withPutRequest(new PutRequest().withItem(threadItem))); // Create a DeleteRequest for a Thread item Map<String, AttributeValue> threadDeleteKey = new HashMap<String, AttributeValue>(); threadDeleteKey.put("ForumName", new AttributeValue().withS("Amazon S3")); threadDeleteKey.put("Subject", new AttributeValue().withS("S3 Thread 100")); threadList.add(new WriteRequest().withDeleteRequest(new DeleteRequest().withKey(threadDeleteKey))); requestItems.put(table2Name, threadList); BatchWriteItemResult result; BatchWriteItemRequest batchWriteItemRequest = new BatchWriteItemRequest() .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); do { System.out.println("Making the request."); batchWriteItemRequest.withRequestItems(requestItems); result = client.batchWriteItem(batchWriteItemRequest); // Print consumed capacity units for(ConsumedCapacity consumedCapacity : result.getConsumedCapacity()) { String tableName = consumedCapacity.getTableName(); Double consumedCapacityUnits = consumedCapacity.getCapacityUnits(); System.out.println("Consumed capacity units for table " + tableName + ": " + consumedCapacityUnits); } // Check for unprocessed keys which could happen if you exceed provisioned throughput System.out.println("Unprocessed Put and Delete requests: \n" + result.getUnprocessedItems()); requestItems = result.getUnprocessedItems(); } while (result.getUnprocessedItems().size() > 0); } catch (AmazonServiceException ase) { System.err.println("Failed to retrieve items: "); ase.printStackTrace(System.err); } }