use of com.amazonaws.services.dynamodbv2.document.utils.NameMap 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.NameMap 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.NameMap 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.NameMap in project aws-doc-sdk-examples by awsdocs.
the class MoviesScan 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");
ScanSpec scanSpec = new ScanSpec().withProjectionExpression("#yr, title, info.rating").withFilterExpression("#yr between :start_yr and :end_yr").withNameMap(new NameMap().with("#yr", "year")).withValueMap(new ValueMap().withNumber(":start_yr", 1950).withNumber(":end_yr", 1959));
try {
ItemCollection<ScanOutcome> items = table.scan(scanSpec);
Iterator<Item> iter = items.iterator();
while (iter.hasNext()) {
Item item = iter.next();
System.out.println(item.toString());
}
} catch (Exception e) {
System.err.println("Unable to scan the table:");
System.err.println(e.getMessage());
}
}
use of com.amazonaws.services.dynamodbv2.document.utils.NameMap in project aws-doc-sdk-examples by awsdocs.
the class DocumentAPIItemCRUDExample method deleteItem.
private static void deleteItem() {
Table table = dynamoDB.getTable(tableName);
try {
DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("Id", 120).withConditionExpression("#ip = :val").withNameMap(new NameMap().with("#ip", "InPublication")).withValueMap(new ValueMap().withBoolean(":val", false)).withReturnValues(ReturnValue.ALL_OLD);
DeleteItemOutcome outcome = table.deleteItem(deleteItemSpec);
// Check the response.
System.out.println("Printing item that was deleted...");
System.out.println(outcome.getItem().toJSONPretty());
} catch (Exception e) {
System.err.println("Error deleting item in " + tableName);
System.err.println(e.getMessage());
}
}
Aggregations