use of com.amazonaws.services.dynamodbv2.document.utils.ValueMap in project aws-doc-sdk-examples by awsdocs.
the class MoviesItemOps06 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";
DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey(new PrimaryKey("year", year, "title", title)).withConditionExpression("info.rating <= :val").withValueMap(new ValueMap().withNumber(":val", 5.0));
try {
System.out.println("Attempting a conditional delete...");
table.deleteItem(deleteItemSpec);
System.out.println("DeleteItem succeeded");
} catch (Exception e) {
System.err.println("Unable to delete item: " + year + " " + title);
System.err.println(e.getMessage());
}
}
use of com.amazonaws.services.dynamodbv2.document.utils.ValueMap in project aws-doc-sdk-examples by awsdocs.
the class MoviesQuery 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");
HashMap<String, String> nameMap = new HashMap<String, String>();
nameMap.put("#yr", "year");
HashMap<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put(":yyyy", 1985);
QuerySpec querySpec = new QuerySpec().withKeyConditionExpression("#yr = :yyyy").withNameMap(nameMap).withValueMap(valueMap);
ItemCollection<QueryOutcome> items = null;
Iterator<Item> iterator = null;
Item item = null;
try {
System.out.println("Movies from 1985");
items = table.query(querySpec);
iterator = items.iterator();
while (iterator.hasNext()) {
item = iterator.next();
System.out.println(item.getNumber("year") + ": " + item.getString("title"));
}
} catch (Exception e) {
System.err.println("Unable to query movies from 1985");
System.err.println(e.getMessage());
}
valueMap.put(":yyyy", 1992);
valueMap.put(":letter1", "A");
valueMap.put(":letter2", "L");
querySpec.withProjectionExpression("#yr, title, info.genres, info.actors[0]").withKeyConditionExpression("#yr = :yyyy and title between :letter1 and :letter2").withNameMap(nameMap).withValueMap(valueMap);
try {
System.out.println("Movies from 1992 - titles A-L, with genres and lead actor");
items = table.query(querySpec);
iterator = items.iterator();
while (iterator.hasNext()) {
item = iterator.next();
System.out.println(item.getNumber("year") + ": " + item.getString("title") + " " + item.getMap("info"));
}
} catch (Exception e) {
System.err.println("Unable to query movies from 1992:");
System.err.println(e.getMessage());
}
}
use of com.amazonaws.services.dynamodbv2.document.utils.ValueMap 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.utils.ValueMap 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.utils.ValueMap in project aws-doc-sdk-examples by awsdocs.
the class TryDaxTests method queryTest.
void queryTest(String tableName, DynamoDB client, int pk, int sk1, int sk2, int iterations) {
long startTime, endTime;
System.out.println("Query test - partition key " + pk + " and sort keys between " + sk1 + " and " + sk2);
Table table = client.getTable(tableName);
HashMap<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put(":pkval", pk);
valueMap.put(":skval1", sk1);
valueMap.put(":skval2", sk2);
QuerySpec spec = new QuerySpec().withKeyConditionExpression("pk = :pkval and sk between :skval1 and :skval2").withValueMap(valueMap);
for (int i = 0; i < iterations; i++) {
startTime = System.nanoTime();
ItemCollection<QueryOutcome> items = table.query(spec);
try {
Iterator<Item> iter = items.iterator();
while (iter.hasNext()) {
iter.next();
}
} catch (Exception e) {
System.err.println("Unable to query table:");
e.printStackTrace();
}
endTime = System.nanoTime();
printTime(startTime, endTime, iterations);
}
}
Aggregations