use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB 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.AmazonDynamoDB 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.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.
the class PutItem method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " PutItem <table> <name> [field=value ...]\n\n" + "Where:\n" + " table - the table to put the item in.\n" + " name - a name to add to the table. If the name already\n" + " exists, its entry will be updated.\n" + "Additional fields can be added by appending them to the end of the\n" + "input.\n\n" + "Example:\n" + " PutItem Cellists Pau Language=ca Born=1876\n";
if (args.length < 2) {
System.out.println(USAGE);
System.exit(1);
}
String table_name = args[0];
String name = args[1];
ArrayList<String[]> extra_fields = new ArrayList<String[]>();
// any additional args (fields to add to database)?
for (int x = 2; x < args.length; x++) {
String[] fields = args[x].split("=", 2);
if (fields.length == 2) {
extra_fields.add(fields);
} else {
System.out.format("Invalid argument: %s\n", args[x]);
System.out.println(USAGE);
System.exit(1);
}
}
System.out.format("Adding \"%s\" to \"%s\"", name, table_name);
if (extra_fields.size() > 0) {
System.out.println("Additional fields:");
for (String[] field : extra_fields) {
System.out.format(" %s: %s\n", field[0], field[1]);
}
}
HashMap<String, AttributeValue> item_values = new HashMap<String, AttributeValue>();
item_values.put("Name", new AttributeValue(name));
for (String[] field : extra_fields) {
item_values.put(field[0], new AttributeValue(field[1]));
}
final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
try {
ddb.putItem(table_name, item_values);
} catch (ResourceNotFoundException e) {
System.err.format("Error: The table \"%s\" can't be found.\n", table_name);
System.err.println("Be sure that it exists and that you've typed its name correctly!");
System.exit(1);
} catch (AmazonServiceException e) {
System.err.println(e.getMessage());
System.exit(1);
}
System.out.println("Done!");
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.
the class UseDynamoMapping method main.
public static void main(String[] args) {
final String USAGE = "\n" + "To run this example, supply the following values: \n" + "artist name \n" + "song title \n" + "album title \n" + "number of awards \n";
if (args.length < 4) {
System.out.println(USAGE);
System.exit(1);
}
String artist = args[0];
String songTitle = args[1];
String albumTitle = args[2];
String awards = args[3];
// snippet-start:[dynamodb.java.dynamoDB_mapping.main]
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
MusicItems items = new MusicItems();
try {
// Add new content to the Music table
items.setArtist(artist);
items.setSongTitle(songTitle);
items.setAlbumTitle(albumTitle);
// convert to an int
items.setAwards(Integer.parseInt(awards));
// Save the item
DynamoDBMapper mapper = new DynamoDBMapper(client);
mapper.save(items);
// Load an item based on the Partition Key and Sort Key
// Both values need to be passed to the mapper.load method
String artistName = artist;
String songQueryTitle = songTitle;
// Retrieve the item
MusicItems itemRetrieved = mapper.load(MusicItems.class, artistName, songQueryTitle);
System.out.println("Item retrieved:");
System.out.println(itemRetrieved);
// Modify the Award value
itemRetrieved.setAwards(2);
mapper.save(itemRetrieved);
System.out.println("Item updated:");
System.out.println(itemRetrieved);
System.out.print("Done");
} catch (AmazonDynamoDBException e) {
e.getStackTrace();
}
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.
the class CreateTable method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " CreateTable <table>\n\n" + "Where:\n" + " table - the table to create.\n\n" + "Example:\n" + " CreateTable HelloTable\n";
if (args.length < 1) {
System.out.println(USAGE);
System.exit(1);
}
/* Read the name from command args */
String table_name = args[0];
System.out.format("Creating table \"%s\" with a simple primary key: \"Name\".\n", table_name);
CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(new AttributeDefinition("Name", ScalarAttributeType.S)).withKeySchema(new KeySchemaElement("Name", KeyType.HASH)).withProvisionedThroughput(new ProvisionedThroughput(new Long(10), new Long(10))).withTableName(table_name);
final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
try {
CreateTableResult result = ddb.createTable(request);
System.out.println(result.getTableDescription().getTableName());
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Done!");
}
Aggregations