下面列出了怎么用com.amazonaws.services.dynamodbv2.model.DeleteItemResult的API类实例代码及写法,或者点击链接到github查看源代码。
DeleteItemResult deleteItem(final DeleteItemRequest request) throws BackendException {
setUserAgent(request);
DeleteItemResult result;
final int wcu = estimateCapacityUnits(DELETE_ITEM, request.getTableName());
timedWriteThrottle(DELETE_ITEM, request.getTableName(), wcu);
final Timer.Context apiTimerContext = getTimerContext(DELETE_ITEM, request.getTableName());
try {
result = client.deleteItem(request);
} catch (Exception e) {
throw processDynamoDbApiException(e, DELETE_ITEM, request.getTableName());
} finally {
apiTimerContext.stop();
}
meterConsumedCapacity(DELETE_ITEM, result.getConsumedCapacity());
return result;
}
@Test
public void test_deleteItem_WithAllParameters() throws Exception {
createTable();
putItem(TEST_ATTRIBUTE, TEST_ATTRIBUTE_VALUE);
Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put(TEST_ATTRIBUTE, new AttributeValue()
.withS(TEST_ATTRIBUTE_VALUE));
String returnValues = "";
DeleteItemResult deleteResult = dynamoDb.deleteItem(TEST_TABLE_NAME, key, returnValues);
AttributeValue attributeValue = deleteResult.getAttributes().get(TEST_ATTRIBUTE);
GetItemResult getResult = getItem(TEST_ATTRIBUTE, TEST_ATTRIBUTE_VALUE);
assertThat(attributeValue.getS(), equalTo(TEST_ATTRIBUTE_VALUE));
assertThat(getResult, nullValue());
}
public DeletePointResult deletePoint(DeletePointRequest deletePointRequest) {
long geohash = S2Manager.generateGeohash(deletePointRequest.getGeoPoint());
long hashKey = S2Manager.generateHashKey(geohash, config.getHashKeyLength());
DeleteItemRequest deleteItemRequest = deletePointRequest.getDeleteItemRequest();
deleteItemRequest.setTableName(config.getTableName());
AttributeValue hashKeyValue = new AttributeValue().withN(String.valueOf(hashKey));
deleteItemRequest.getKey().put(config.getHashKeyAttributeName(), hashKeyValue);
deleteItemRequest.getKey().put(config.getRangeKeyAttributeName(), deletePointRequest.getRangeKeyValue());
DeleteItemResult deleteItemResult = config.getDynamoDBClient().deleteItem(deleteItemRequest);
DeletePointResult deletePointResult = new DeletePointResult(deleteItemResult);
return deletePointResult;
}
private static void deleteItem() {
try {
HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("Id", new AttributeValue().withN("120"));
Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
expressionAttributeValues.put(":val", new AttributeValue().withBOOL(false));
ReturnValue returnValues = ReturnValue.ALL_OLD;
DeleteItemRequest deleteItemRequest = new DeleteItemRequest().withTableName(tableName).withKey(key)
.withConditionExpression("InPublication = :val")
.withExpressionAttributeValues(expressionAttributeValues).withReturnValues(returnValues);
DeleteItemResult result = client.deleteItem(deleteItemRequest);
// Check the response.
System.out.println("Printing item that was deleted...");
printItem(result.getAttributes());
}
catch (AmazonServiceException ase) {
System.err.println("Failed to get item after deletion " + tableName);
}
}
private static void deleteItem() {
try {
HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("Id", new AttributeValue().withN("120"));
Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
expressionAttributeValues.put(":val", new AttributeValue().withBOOL(false));
ReturnValue returnValues = ReturnValue.ALL_OLD;
DeleteItemRequest deleteItemRequest = new DeleteItemRequest()
.withTableName(tableName)
.withKey(key)
.withConditionExpression("InPublication = :val")
.withExpressionAttributeValues(expressionAttributeValues)
.withReturnValues(returnValues);
DeleteItemResult result = client.deleteItem(deleteItemRequest);
// Check the response.
System.out.println("Printing item that was deleted...");
printItem(result.getAttributes());
} catch (AmazonServiceException ase) {
System.err.println("Failed to get item after deletion " + tableName);
}
}
/**
* Adds a DeleteItem request to the transaction
*
* @param request
* @throws DuplicateRequestException if the item in the request is already involved in this transaction
* @throws ItemNotLockedException when another transaction is confirmed to have the lock on the item in the request
* @throws TransactionCompletedException when the transaction has already completed
* @throws TransactionNotFoundException if the transaction does not exist
* @throws TransactionException on unexpected errors or unresolvable OCC contention
*/
public DeleteItemResult deleteItem(DeleteItemRequest request)
throws DuplicateRequestException, ItemNotLockedException,
TransactionCompletedException, TransactionNotFoundException, TransactionException {
DeleteItem wrappedRequest = new DeleteItem();
wrappedRequest.setRequest(request);
Map<String, AttributeValue> item = driveRequest(wrappedRequest);
stripSpecialAttributes(item);
return new DeleteItemResult().withAttributes(item);
}
@Override
public DeleteItemResult deleteItem(DeleteItemRequest request)
throws AmazonServiceException, AmazonClientException {
Map<String, ExpectedAttributeValue> expectedValues = request.getExpected();
checkExpectedValues(request.getTableName(), request.getKey(), expectedValues);
// conditional checks are handled by the above call
request.setExpected(null);
return txn.deleteItem(request);
}
@Override
public DeleteItemResult deleteItem(String tableName,
Map<String, AttributeValue> key) throws AmazonServiceException,
AmazonClientException {
return deleteItem(new DeleteItemRequest()
.withTableName(tableName)
.withKey(key));
}
@Override
public DeleteItemResult deleteItem(String tableName,
Map<String, AttributeValue> key, String returnValues)
throws AmazonServiceException, AmazonClientException {
return deleteItem(new DeleteItemRequest()
.withTableName(tableName)
.withKey(key)
.withReturnValues(returnValues));
}
@Override
public DynamoDBResponse deleteItem(DeleteItemRequest dir) {
logger.debug("delete item into JSON table");
String tableName = dir.getTableName();
TableDef tableDef = cassandraManager.getTableDef(tableName);
PreparedStatement deleteStatement = tableDef.getDeleteStatement();
AttributeDefinition partitionKeyAttr = tableDef.getPartitionKey();
Optional<AttributeDefinition> maybeCusteringKeyAttr = tableDef.getClusteringKey();
Map<String, AttributeValue> keys = dir.getKey();
Object partitionKeyValue = getAttributeObject(
ScalarAttributeType.fromValue(partitionKeyAttr.getAttributeType()),
keys.get(partitionKeyAttr.getAttributeName())
);
BoundStatement boundStatement;
if (maybeCusteringKeyAttr.isPresent())
{
Object clusteringKeyValue = getAttributeObject(
ScalarAttributeType.fromValue(maybeCusteringKeyAttr.get().getAttributeType()),
keys.get(maybeCusteringKeyAttr.get().getAttributeName())
);
boundStatement = deleteStatement.bind(partitionKeyValue, clusteringKeyValue);
}
else
{
boundStatement = deleteStatement.bind(partitionKeyValue);
}
ResultSet result = session().execute(boundStatement);
if (result.wasApplied()){
DeleteItemResult dres = new DeleteItemResult();
return new DynamoDBResponse(dres, 200);
}
else return null;
}
@Override
public DeleteItemResult deleteItem(DeleteItemRequest deleteItemRequest) {
return null;
}
@Override
public DeleteItemResult deleteItem(String tableName, Map<String, AttributeValue> key) {
throw new UnsupportedOperationException();
}
@Override
public DeleteItemResult deleteItem(String tableName, Map<String, AttributeValue> key, String returnValues) {
throw new UnsupportedOperationException();
}
@Override
public DeleteItemResult deleteItem(DeleteItemRequest request) {
bh.consume(request);
return DELETE_ITEM_RESULT;
}
@Override
protected DeleteItemResult call() throws BackendException {
return delegate.deleteItem(request);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public List<Record> emit(final UnmodifiableBuffer<Record> buffer) {
if (isShutdown) {
if (buffer.getRecords().isEmpty()) {
// This is OK, but not expected
log.warn("Record processor called emit after calling shutdown. Continuing becuase buffer is empty.");
return Collections.emptyList();
} else {
throw new IllegalStateException("Cannot emit records after emitter has been shutdown.");
}
}
// Asynchronously process all writes, but block on the results.
List<Record> records = buffer.getRecords();
// Stores records that failed with a non-retryable exception
final List<Record> failedRecords = Collections.synchronizedList(new ArrayList<Record>());
// Queue of records to submit
final BlockingQueue<Record> toSubmit = new LinkedBlockingQueue<Record>(records);
// Used to detect when all requests have either succeeded or resulted in a non-retryable exception
final CountDownLatch doneSignal = new CountDownLatch(records.size());
final AtomicInteger retryCount = new AtomicInteger();
boolean interrupted = false;
try {
while (doneSignal.getCount() > 0) {
Record recordToSubmit = null;
try {
recordToSubmit = toSubmit.poll(WAIT_TIME_MS, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
interrupted = true;
}
final Record record = recordToSubmit;
if (null == record) {
continue; // Check if all records have completed and if not try to poll again
}
// Generate the request based on the record
AmazonWebServiceRequest request = createRequest(record);
if (request == null) { // Should only happen if DynamoDB Streams API updates to support different operations
// than {INSERT, MODIFY, REMOVE}.
continue;
}
// Submit the write request based on its type
if (request instanceof PutItemRequest) { // PUT
getDynamodb().putItemAsync((PutItemRequest) request,
(AsyncHandler<PutItemRequest, PutItemResult>) getHandler(toSubmit, failedRecords, retryCount, doneSignal, record));
} else if (request instanceof DeleteItemRequest) { // DELETE
getDynamodb().deleteItemAsync((DeleteItemRequest) request,
(AsyncHandler<DeleteItemRequest, DeleteItemResult>) getHandler(toSubmit, failedRecords, retryCount, doneSignal, record));
} else if (request instanceof UpdateItemRequest) { // UPDATE
getDynamodb().updateItemAsync((UpdateItemRequest) request,
(AsyncHandler<UpdateItemRequest, UpdateItemResult>) getHandler(toSubmit, failedRecords, retryCount, doneSignal, record));
} else { // Should only happen if DynamoDB allows a new operation other than {PutItem, DeleteItem,
// UpdateItem} for single item writes.
log.warn("Unsupported DynamoDB request: " + request);
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
emitCloudWatchMetrics(records, failedRecords, retryCount);
if (!records.isEmpty()) {
log.debug("Successfully emitted " + (records.size() - failedRecords.size()) + " records ending with sequence number "
+ buffer.getLastSequenceNumber());
} else {
log.debug("No records to emit");
}
return failedRecords;
}
public DeletePointResult(DeleteItemResult deleteItemResult) {
this.deleteItemResult = deleteItemResult;
}
public DeleteItemResult getDeleteItemResult() {
return deleteItemResult;
}
@Override
public DeleteItemResult deleteItem(DeleteItemRequest request)
throws AmazonServiceException, AmazonClientException {
throw new UnsupportedOperationException("Use the underlying client instance instead");
}
@Override
public DeleteItemResult deleteItem(String tableName,
Map<String, AttributeValue> key) throws AmazonServiceException,
AmazonClientException {
throw new UnsupportedOperationException("Use the underlying client instance instead");
}
@Override
public DeleteItemResult deleteItem(String tableName,
Map<String, AttributeValue> key, String returnValues)
throws AmazonServiceException, AmazonClientException {
throw new UnsupportedOperationException("Use the underlying client instance instead");
}
@Override
public DeleteItemResult deleteItem(DeleteItemRequest request) throws AmazonServiceException, AmazonClientException {
return getBackend().deleteItem(request);
}
@Override
public DeleteItemResult deleteItem(String tableName, Map<String, AttributeValue> key) throws AmazonServiceException, AmazonClientException {
return getBackend().deleteItem(tableName, key);
}
@Override
public DeleteItemResult deleteItem(String tableName, Map<String, AttributeValue> key, String returnValues) throws AmazonServiceException, AmazonClientException {
return getBackend().deleteItem(tableName, key, returnValues);
}