Search in sources :

Example 1 with UpdateItemSpec

use of com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec in project spring-integration-aws by spring-projects.

the class DynamoDbMetaDataStore method putIfAbsent.

@Override
public String putIfAbsent(String key, String value) {
    Assert.hasText(key, "'key' must not be empty.");
    Assert.hasText(value, "'value' must not be empty.");
    awaitForActive();
    try {
        this.table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY, key).withAttributeUpdate(new AttributeUpdate(VALUE).put(value)).withExpected(new Expected(KEY).notExist()));
        return null;
    } catch (ConditionalCheckFailedException e) {
        return get(key);
    }
}
Also used : AttributeUpdate(com.amazonaws.services.dynamodbv2.document.AttributeUpdate) Expected(com.amazonaws.services.dynamodbv2.document.Expected) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 2 with UpdateItemSpec

use of com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec in project spring-integration-aws by spring-projects.

the class DynamoDbMetaDataStore method replace.

@Override
public boolean replace(String key, String oldValue, String newValue) {
    Assert.hasText(key, "'key' must not be empty.");
    Assert.hasText(oldValue, "'value' must not be empty.");
    Assert.hasText(newValue, "'newValue' must not be empty.");
    awaitForActive();
    try {
        return this.table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY, key).withAttributeUpdate(new AttributeUpdate(VALUE).put(newValue)).withExpected(new Expected(VALUE).eq(oldValue)).withReturnValues(ReturnValue.UPDATED_NEW)).getItem() != null;
    } catch (ConditionalCheckFailedException e) {
        return false;
    }
}
Also used : AttributeUpdate(com.amazonaws.services.dynamodbv2.document.AttributeUpdate) Expected(com.amazonaws.services.dynamodbv2.document.Expected) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 3 with UpdateItemSpec

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

the class MoviesItemOps03 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 = :r, info.plot=:p, info.actors=:a").withValueMap(new ValueMap().withNumber(":r", 5.5).withString(":p", "Everything happens all at once.").withList(":a", Arrays.asList("Larry", "Moe", "Curly"))).withReturnValues(ReturnValue.UPDATED_NEW);
    try {
        System.out.println("Updating the item...");
        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 4 with UpdateItemSpec

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

the class DocumentAPIItemCRUDExample method updateMultipleAttributes.

private static void updateMultipleAttributes() {
    Table table = dynamoDB.getTable(tableName);
    try {
        UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("Id", 120).withUpdateExpression("add #a :val1 set #na=:val2").withNameMap(new NameMap().with("#a", "Authors").with("#na", "NewAttribute")).withValueMap(new ValueMap().withStringSet(":val1", "Author YY", "Author ZZ").withString(":val2", "someValue")).withReturnValues(ReturnValue.ALL_NEW);
        UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
        // Check the response.
        System.out.println("Printing item after multiple attribute update...");
        System.out.println(outcome.getItem().toJSONPretty());
    } catch (Exception e) {
        System.err.println("Failed to update multiple attributes 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 5 with UpdateItemSpec

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

Aggregations

UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)17 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 Test (org.testng.annotations.Test)6 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)4 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)3 AttributeUpdate (com.amazonaws.services.dynamodbv2.document.AttributeUpdate)3 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)3 NameMap (com.amazonaws.services.dynamodbv2.document.utils.NameMap)3 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)3 IOException (java.io.IOException)3 Expected (com.amazonaws.services.dynamodbv2.document.Expected)2 PrimaryKey (com.amazonaws.services.dynamodbv2.document.PrimaryKey)1 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)1 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)1 SSHCertRecord (com.yahoo.athenz.common.server.ssh.SSHCertRecord)1 WorkloadRecord (com.yahoo.athenz.common.server.workload.WorkloadRecord)1 Date (java.util.Date)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1