Search in sources :

Example 56 with Table

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

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

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

the class DocumentAPIScan method findProductsForPriceLessThanOneHundred.

private static void findProductsForPriceLessThanOneHundred() {
    Table table = dynamoDB.getTable(tableName);
    Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
    expressionAttributeValues.put(":pr", 100);
    ItemCollection<ScanOutcome> items = // FilterExpression
    table.scan(// FilterExpression
    "Price < :pr", // ProjectionExpression
    "Id, Title, ProductCategory, Price", // ExpressionAttributeNames - not used in this example
    null, expressionAttributeValues);
    System.out.println("Scan of " + tableName + " for items with a price less than 100.");
    Iterator<Item> iterator = items.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next().toJSONPretty());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) HashMap(java.util.HashMap) ScanOutcome(com.amazonaws.services.dynamodbv2.document.ScanOutcome)

Example 59 with Table

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

the class LowLevelGlobalSecondaryIndexExample method waitForTableToBecomeAvailable.

private static void waitForTableToBecomeAvailable(String tableName) {
    System.out.println("Waiting for " + tableName + " to become ACTIVE...");
    long startTime = System.currentTimeMillis();
    long endTime = startTime + (10 * 60 * 1000);
    while (System.currentTimeMillis() < endTime) {
        DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
        TableDescription tableDescription = client.describeTable(request).getTable();
        String tableStatus = tableDescription.getTableStatus();
        System.out.println("  - current state: " + tableStatus);
        if (tableStatus.equals(TableStatus.ACTIVE.toString()))
            return;
        try {
            Thread.sleep(1000 * 20);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    throw new RuntimeException("Table " + tableName + " never went active");
}
Also used : DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

Example 60 with Table

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

the class LowLevelLocalSecondaryIndexExample method waitForTableToBeDeleted.

private static void waitForTableToBeDeleted(String tableName) {
    System.out.println("Waiting for " + tableName + " while status DELETING...");
    long startTime = System.currentTimeMillis();
    long endTime = startTime + (10 * 60 * 1000);
    while (System.currentTimeMillis() < endTime) {
        try {
            DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
            TableDescription tableDescription = client.describeTable(request).getTable();
            String tableStatus = tableDescription.getTableStatus();
            System.out.println("  - current state: " + tableStatus);
            if (tableStatus.equals(TableStatus.ACTIVE.toString()))
                return;
        } catch (ResourceNotFoundException e) {
            System.out.println("Table " + tableName + " is not found. It was deleted.");
            return;
        }
        try {
            Thread.sleep(1000 * 20);
        } catch (Exception e) {
        }
    }
    throw new RuntimeException("Table " + tableName + " was never deleted");
}
Also used : DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

Aggregations

Table (com.amazonaws.services.dynamodbv2.document.Table)63 Item (com.amazonaws.services.dynamodbv2.document.Item)40 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)38 AmazonServiceException (com.amazonaws.AmazonServiceException)35 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)31 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)28 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)28 IOException (java.io.IOException)24 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)23 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)23 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)23 HashMap (java.util.HashMap)23 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)22 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)22 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)17 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)17 ArrayList (java.util.ArrayList)17 AmazonClientException (com.amazonaws.AmazonClientException)14 Test (org.junit.Test)14 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)10