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());
}
}
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());
}
}
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());
}
}
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");
}
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");
}
Aggregations