Search in sources :

Example 6 with UpdateItemSpec

use of com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec 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 7 with UpdateItemSpec

use of com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec 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 8 with UpdateItemSpec

use of com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec 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)

Aggregations

UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)8 Table (com.amazonaws.services.dynamodbv2.document.Table)6 UpdateItemOutcome (com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome)6 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)6 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)3 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)3 NameMap (com.amazonaws.services.dynamodbv2.document.utils.NameMap)3 IOException (java.io.IOException)3 AttributeUpdate (com.amazonaws.services.dynamodbv2.document.AttributeUpdate)2 Expected (com.amazonaws.services.dynamodbv2.document.Expected)2 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)2 PrimaryKey (com.amazonaws.services.dynamodbv2.document.PrimaryKey)1