com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec Java Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DocumentAPIItemCRUDExample.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void deleteItem() {

        Table table = dynamoDB.getTable(tableName);

        try {

            DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("Id", 120)
                .withConditionExpression("#ip = :val").withNameMap(new NameMap().with("#ip", "InPublication"))
                .withValueMap(new ValueMap().withBoolean(":val", false)).withReturnValues(ReturnValue.ALL_OLD);

            DeleteItemOutcome outcome = table.deleteItem(deleteItemSpec);

            // Check the response.
            System.out.println("Printing item that was deleted...");
            System.out.println(outcome.getItem().toJSONPretty());

        }
        catch (Exception e) {
            System.err.println("Error deleting item in " + tableName);
            System.err.println(e.getMessage());
        }
    }
 
Example #2
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDelete_withItem() {
    // Given
    final ItemId itemId = new ItemId(randomId());
    final StubItem stubItem = generateRandomStubItem(itemId);
    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    final Table mockTable = mock(Table.class);

    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    // When
    dynamoDocumentStoreTemplate.delete(stubItem);

    // Then
    final ArgumentCaptor<DeleteItemSpec> getItemRequestCaptor = ArgumentCaptor.forClass(DeleteItemSpec.class);
    verify(mockTable).deleteItem(getItemRequestCaptor.capture());
}
 
Example #3
Source File: MoviesItemOps06.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)  {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");
                
        // Conditional delete (will fail)
        
        DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
            .withPrimaryKey(new PrimaryKey("year", 2015, "title", "The Big New Movie"))
            .withConditionExpression("info.rating <= :val")
            .withValueMap(new ValueMap()
                   .withNumber(":val", 5.0));
        
        System.out.println("Attempting a conditional delete...");
        try {
            table.deleteItem(deleteItemSpec);
            System.out.println("DeleteItem succeeded");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("DeleteItem failed");
        }

    }
 
Example #4
Source File: DocumentAPIItemCRUDExample.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
private static void deleteItem() {

        Table table = dynamoDB.getTable(tableName);

        try {

            DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
            .withPrimaryKey("Id", 120)
            .withConditionExpression("#ip = :val")
            .withNameMap(new NameMap()
                .with("#ip", "InPublication"))
            .withValueMap(new ValueMap()
            .withBoolean(":val", false))
            .withReturnValues(ReturnValue.ALL_OLD);

            DeleteItemOutcome outcome = table.deleteItem(deleteItemSpec);

            // Check the response.
            System.out.println("Printing item that was deleted...");
            System.out.println(outcome.getItem().toJSONPretty());

        } catch (Exception e) {
            System.err.println("Error deleting item in " + tableName);
            System.err.println(e.getMessage());
        }
    }
 
Example #5
Source File: MoviesItemOps06.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
            .build();

        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");

        int year = 2015;
        String title = "The Big New Movie";

        DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
            .withPrimaryKey(new PrimaryKey("year", year, "title", title)).withConditionExpression("info.rating <= :val")
            .withValueMap(new ValueMap().withNumber(":val", 5.0));

        // Conditional delete (we expect this to fail)

        try {
            System.out.println("Attempting a conditional delete...");
            table.deleteItem(deleteItemSpec);
            System.out.println("DeleteItem succeeded");
        }
        catch (Exception e) {
            System.err.println("Unable to delete item: " + year + " " + title);
            System.err.println(e.getMessage());
        }
    }
 
Example #6
Source File: DynamoDBSSHRecordStoreConnection.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Override
public boolean deleteSSHCertRecord(String instanceId, String service) {

    final String primaryKey = getPrimaryKey(instanceId, service);
    try {
        DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
                .withPrimaryKey(KEY_PRIMARY, primaryKey);
        table.deleteItem(deleteItemSpec);
        return true;
    } catch (Exception ex) {
        LOGGER.error("DynamoDB Delete Error for {}: {}/{}", primaryKey, ex.getClass(), ex.getMessage());
        return false;
    }
}
 
Example #7
Source File: DynamoDBCertRecordStoreConnection.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Override
public boolean deleteX509CertRecord(String provider, String instanceId, String service) {

    final String primaryKey = getPrimaryKey(provider, instanceId, service);
    try {
        DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
                .withPrimaryKey(KEY_PRIMARY, primaryKey);
        table.deleteItem(deleteItemSpec);
        return true;
    } catch (Exception ex) {
        LOGGER.error("DynamoDB Delete Error for {}: {}/{}", primaryKey, ex.getClass(), ex.getMessage());
        return false;
    }
}
 
Example #8
Source File: DynamoDBCertRecordStoreConnectionTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteX509Record() {
    DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
            .withPrimaryKey("primaryKey", "athenz.provider:cn:1234");
    Mockito.doReturn(deleteOutcome).when(table).deleteItem(deleteItemSpec);

    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();

    boolean requestSuccess = dbConn.deleteX509CertRecord("athenz.provider", "12345", "cn");
    assertTrue(requestSuccess);
    dbConn.close();
}
 
Example #9
Source File: DynamoDBCertRecordStoreConnectionTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteX509RecordException() {

    Mockito.doThrow(new AmazonDynamoDBException("invalid operation"))
            .when(table).deleteItem(any(DeleteItemSpec.class));

    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();

    boolean requestSuccess = dbConn.deleteX509CertRecord("athenz.provider", "12345", "cn");
    assertFalse(requestSuccess);
    dbConn.close();
}
 
Example #10
Source File: DynamoDBSSHRecordStoreConnectionTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteSSHRecord() {
    DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
            .withPrimaryKey("primaryKey", "cn:1234");
    Mockito.doReturn(deleteOutcome).when(table).deleteItem(deleteItemSpec);

    DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);

    boolean requestSuccess = dbConn.deleteSSHCertRecord("12345", "cn");
    assertTrue(requestSuccess);
    dbConn.close();
}
 
Example #11
Source File: DynamoDBSSHRecordStoreConnectionTest.java    From athenz with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteSSHRecordException() {

    Mockito.doThrow(new AmazonDynamoDBException("invalid operation"))
            .when(table).deleteItem(ArgumentMatchers.any(DeleteItemSpec.class));

    DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);

    boolean requestSuccess = dbConn.deleteSSHCertRecord("12345", "cn");
    assertFalse(requestSuccess);
    dbConn.close();
}