Search in sources :

Example 1 with DeleteItemResult

use of com.amazonaws.services.dynamodbv2.model.DeleteItemResult in project camel by apache.

the class DeleteItemCommand method execute.

@Override
public void execute() {
    DeleteItemResult result = ddbClient.deleteItem(new DeleteItemRequest().withTableName(determineTableName()).withKey(determineKey()).withReturnValues(determineReturnValues()).withExpected(determineUpdateCondition()));
    addAttributesToResult(result.getAttributes());
}
Also used : DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult)

Example 2 with DeleteItemResult

use of com.amazonaws.services.dynamodbv2.model.DeleteItemResult in project jcabi-dynamo by jcabi.

the class AwsTable method delete.

@Override
public void delete(final Map<String, AttributeValue> attributes) throws IOException {
    final AmazonDynamoDB aws = this.credentials.aws();
    try {
        final DeleteItemRequest request = new DeleteItemRequest();
        request.setTableName(this.self);
        request.setKey(attributes);
        request.setReturnValues(ReturnValue.NONE);
        request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
        final DeleteItemResult result = aws.deleteItem(request);
        final long start = System.currentTimeMillis();
        Logger.info(this, "#delete('%[text]s'): deleted item in '%s', %s, in %[ms]s", attributes, this.self, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
    } catch (final AmazonClientException ex) {
        throw new IOException(String.format("Failed to delete at \"%s\" by keys %s", this.self, attributes), ex);
    } finally {
        aws.shutdown();
    }
}
Also used : DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) AmazonClientException(com.amazonaws.AmazonClientException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult) IOException(java.io.IOException)

Example 3 with DeleteItemResult

use of com.amazonaws.services.dynamodbv2.model.DeleteItemResult in project aws-doc-sdk-examples by awsdocs.

the class LowLevelItemCRUDExample method deleteItem.

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);
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) ReturnValue(com.amazonaws.services.dynamodbv2.model.ReturnValue) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult)

Example 4 with DeleteItemResult

use of com.amazonaws.services.dynamodbv2.model.DeleteItemResult in project jcabi-dynamo by jcabi.

the class AwsIterator method remove.

@Override
@SuppressWarnings("PMD.UseConcurrentHashMap")
public void remove() {
    synchronized (this.dosage) {
        final AmazonDynamoDB aws = this.credentials.aws();
        try {
            final Dosage prev = this.dosage.get();
            final List<Map<String, AttributeValue>> items = new ArrayList<Map<String, AttributeValue>>(prev.items());
            final Map<String, AttributeValue> item = items.remove(this.position);
            final long start = System.currentTimeMillis();
            final DeleteItemResult res = aws.deleteItem(new DeleteItemRequest().withTableName(this.name).withKey(new Attributes(item).only(this.keys)).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withExpected(new Attributes(item).only(this.keys).asKeys()));
            this.dosage.set(new AwsIterator.Fixed(prev, items));
            --this.position;
            Logger.info(this, "#remove(): item #%d removed from DynamoDB, %s, in %[ms]s", this.position, new PrintableConsumedCapacity(res.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        } finally {
            aws.shutdown();
        }
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) ArrayList(java.util.ArrayList) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ToString(lombok.ToString) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult) Map(java.util.Map)

Example 5 with DeleteItemResult

use of com.amazonaws.services.dynamodbv2.model.DeleteItemResult in project jcabi-dynamo by jcabi.

the class AwsTableTest method deletesItemFromDynamo.

/**
 * AwsTable can delete an item.
 * @throws Exception If some problem inside
 */
@Test
public void deletesItemFromDynamo() throws Exception {
    final Credentials credentials = Mockito.mock(Credentials.class);
    final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class);
    Mockito.doReturn(aws).when(credentials).aws();
    Mockito.doReturn(new DeleteItemResult().withConsumedCapacity(new ConsumedCapacity().withCapacityUnits(1.0d))).when(aws).deleteItem(Mockito.any(DeleteItemRequest.class));
    Mockito.doReturn(new DescribeTableResult().withTable(new TableDescription().withKeySchema(new KeySchemaElement().withAttributeName(AwsTableTest.KEY)))).when(aws).describeTable(Mockito.any(DescribeTableRequest.class));
    final String attr = "attribute-2";
    final AttributeValue value = new AttributeValue("value-2");
    final String name = "table-name-2";
    final Table table = new AwsTable(credentials, Mockito.mock(Region.class), name);
    table.delete(new Attributes().with(attr, value));
    Mockito.verify(aws).deleteItem(DeleteItemRequest.class.cast(MockitoHamcrest.argThat(Matchers.allOf(Matchers.hasProperty(AwsTableTest.TABLE_NAME, Matchers.equalTo(name)), Matchers.hasProperty(AwsTableTest.KEY, Matchers.hasEntry(Matchers.equalTo(attr), Matchers.equalTo(value)))))));
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult) DescribeTableResult(com.amazonaws.services.dynamodbv2.model.DescribeTableResult) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) ConsumedCapacity(com.amazonaws.services.dynamodbv2.model.ConsumedCapacity) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) Test(org.junit.Test)

Aggregations

DeleteItemRequest (com.amazonaws.services.dynamodbv2.model.DeleteItemRequest)5 DeleteItemResult (com.amazonaws.services.dynamodbv2.model.DeleteItemResult)5 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)3 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)3 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonServiceException (com.amazonaws.AmazonServiceException)1 ConsumedCapacity (com.amazonaws.services.dynamodbv2.model.ConsumedCapacity)1 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)1 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)1 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)1 ReturnValue (com.amazonaws.services.dynamodbv2.model.ReturnValue)1 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ToString (lombok.ToString)1 Test (org.junit.Test)1