Search in sources :

Example 6 with UpdateItemOutcome

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

the class DocumentAPIItemCRUDExample method updateExistingAttributeConditionally.

private static void updateExistingAttributeConditionally() {
    Table table = dynamoDB.getTable(tableName);
    try {
        // Specify the desired price (25.00) and also the condition (price =
        // 20.00)
        UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("Id", 120).withReturnValues(ReturnValue.ALL_NEW).withUpdateExpression("set #p = :val1").withConditionExpression("#p = :val2").withNameMap(new NameMap().with("#p", "Price")).withValueMap(new ValueMap().withNumber(":val1", 25).withNumber(":val2", 20));
        UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
        // Check the response.
        System.out.println("Printing item after conditional update to new attribute...");
        System.out.println(outcome.getItem().toJSONPretty());
    } catch (Exception e) {
        System.err.println("Error updating item in " + tableName);
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) UpdateItemOutcome(com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome) NameMap(com.amazonaws.services.dynamodbv2.document.utils.NameMap) IOException(java.io.IOException)

Example 7 with UpdateItemOutcome

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

the class MoviesItemOps05 method main.

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";
    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(new PrimaryKey("year", year, "title", title)).withUpdateExpression("remove info.actors[0]").withConditionExpression("size(info.actors) > :num").withValueMap(new ValueMap().withNumber(":num", 3)).withReturnValues(ReturnValue.UPDATED_NEW);
    // Conditional update (we expect this to fail)
    try {
        System.out.println("Attempting a conditional update...");
        UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
        System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty());
    } catch (Exception e) {
        System.err.println("Unable to update item: " + year + " " + title);
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) UpdateItemOutcome(com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) PrimaryKey(com.amazonaws.services.dynamodbv2.document.PrimaryKey) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 8 with UpdateItemOutcome

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

the class MoviesItemOps04 method main.

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";
    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("year", year, "title", title).withUpdateExpression("set info.rating = info.rating + :val").withValueMap(new ValueMap().withNumber(":val", 1)).withReturnValues(ReturnValue.UPDATED_NEW);
    try {
        System.out.println("Incrementing an atomic counter...");
        UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
        System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty());
    } catch (Exception e) {
        System.err.println("Unable to update item: " + year + " " + title);
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) UpdateItemOutcome(com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 9 with UpdateItemOutcome

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

the class DocumentAPIItemCRUDExample method updateAddNewAttribute.

private static void updateAddNewAttribute() {
    Table table = dynamoDB.getTable(tableName);
    try {
        UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("Id", 121).withUpdateExpression("set #na = :val1").withNameMap(new NameMap().with("#na", "NewAttribute")).withValueMap(new ValueMap().withString(":val1", "Some value")).withReturnValues(ReturnValue.ALL_NEW);
        UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
        // Check the response.
        System.out.println("Printing item after adding new attribute...");
        System.out.println(outcome.getItem().toJSONPretty());
    } catch (Exception e) {
        System.err.println("Failed to add new attribute in " + tableName);
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) UpdateItemOutcome(com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome) NameMap(com.amazonaws.services.dynamodbv2.document.utils.NameMap) IOException(java.io.IOException)

Example 10 with UpdateItemOutcome

use of com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome in project athenz by yahoo.

the class DynamoDBNotificationsHelperTest method testUpdateLastNotifiedItem.

@Test
public void testUpdateLastNotifiedItem() throws TimeoutException, InterruptedException {
    Date now = new Date(1591706189000L);
    long lastNotifiedTime = now.getTime();
    long yesterday = lastNotifiedTime - TimeUnit.DAYS.toMillis(1);
    long fiveDaysAgo = lastNotifiedTime - 5 * 24 * 60 * 60 * 1000;
    DynamoDBNotificationsHelper dynamoDBNotificationsHelper = new DynamoDBNotificationsHelper();
    Map<String, AttributeValue> reNotified = ZTSTestUtils.generateAttributeValues("home.test.service3", "reNotified", Long.toString(fiveDaysAgo), Long.toString(fiveDaysAgo), "testServer", null, "testHost2");
    Item item = ItemUtils.toItem(reNotified);
    UpdateItemOutcome updateItemOutcome1 = Mockito.mock(UpdateItemOutcome.class);
    when(updateItemOutcome1.getItem()).thenReturn(item);
    Table table = Mockito.mock(Table.class);
    Mockito.when(table.updateItem(any(UpdateItemSpec.class))).thenReturn(updateItemOutcome1);
    Item updatedItem = dynamoDBNotificationsHelper.updateLastNotifiedItem("lastNotifiedServer", lastNotifiedTime, yesterday, item, "primaryKey", table);
    assertEquals(updatedItem, item);
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Table(com.amazonaws.services.dynamodbv2.document.Table) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) UpdateItemOutcome(com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome) Test(org.testng.annotations.Test)

Aggregations

Table (com.amazonaws.services.dynamodbv2.document.Table)9 UpdateItemOutcome (com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome)9 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)8 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)6 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)4 Test (org.testng.annotations.Test)4 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)3 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)3 Item (com.amazonaws.services.dynamodbv2.document.Item)3 NameMap (com.amazonaws.services.dynamodbv2.document.utils.NameMap)3 IOException (java.io.IOException)3 ProvisionedThroughputExceededException (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputExceededException)2 PrimaryKey (com.amazonaws.services.dynamodbv2.document.PrimaryKey)1 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)1 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)1 TimeoutException (java.util.concurrent.TimeoutException)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1