use of com.amazonaws.services.dynamodbv2.document.Table in project aws-doc-sdk-examples by awsdocs.
the class LowLevelLocalSecondaryIndexExample 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) {
}
}
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 query.
public static void query(String indexName) {
System.out.println("\n***********************************************************\n");
System.out.println("Querying table " + tableName + "...");
QueryRequest queryRequest = new QueryRequest().withTableName(tableName).withConsistentRead(true).withScanIndexForward(true).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
HashMap<String, Condition> keyConditions = new HashMap<String, Condition>();
keyConditions.put("CustomerId", new Condition().withComparisonOperator(ComparisonOperator.EQ).withAttributeValueList(new AttributeValue().withS("bob@example.com")));
if (indexName == "IsOpenIndex") {
System.out.println("\nUsing index: '" + indexName + "': Bob's orders that are open.");
System.out.println("Only a user-specified list of attributes are returned\n");
queryRequest.setIndexName(indexName);
keyConditions.put("IsOpen", new Condition().withComparisonOperator(ComparisonOperator.EQ).withAttributeValueList(new AttributeValue().withN("1")));
// ProjectionExpression
queryRequest.setProjectionExpression("OrderCreationDate, ProductCategory, ProductName, OrderStatus");
} else if (indexName == "OrderCreationDateIndex") {
System.out.println("\nUsing index: '" + indexName + "': Bob's orders that were placed after 01/31/2013.");
System.out.println("Only the projected attributes are returned\n");
queryRequest.setIndexName(indexName);
keyConditions.put("OrderCreationDate", new Condition().withComparisonOperator(ComparisonOperator.GT).withAttributeValueList(new AttributeValue().withN("20130131")));
// Select
queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES);
} else {
System.out.println("\nNo index: All of Bob's orders, by OrderId:\n");
}
queryRequest.setKeyConditions(keyConditions);
QueryResult result = client.query(queryRequest);
List<Map<String, AttributeValue>> items = result.getItems();
Iterator<Map<String, AttributeValue>> itemsIter = items.iterator();
while (itemsIter.hasNext()) {
Map<String, AttributeValue> currentItem = itemsIter.next();
Iterator<String> currentItemIter = currentItem.keySet().iterator();
while (currentItemIter.hasNext()) {
String attr = (String) currentItemIter.next();
if (attr == "OrderId" || attr == "IsOpen" || attr == "OrderCreationDate") {
System.out.println(attr + "---> " + currentItem.get(attr).getN());
} else {
System.out.println(attr + "---> " + currentItem.get(attr).getS());
}
}
System.out.println();
}
System.out.println("\nConsumed capacity: " + result.getConsumedCapacity() + "\n");
}
use of com.amazonaws.services.dynamodbv2.document.Table 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.document.Table 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.document.Table 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