use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project aws-doc-sdk-examples by awsdocs.
the class CreateTablesLoadData method loadSampleReplies.
private static void loadSampleReplies(String tableName) {
try {
// 1 day ago
long time0 = (new Date()).getTime() - (1 * 24 * 60 * 60 * 1000);
// 7 days ago
long time1 = (new Date()).getTime() - (7 * 24 * 60 * 60 * 1000);
// 14 days ago
long time2 = (new Date()).getTime() - (14 * 24 * 60 * 60 * 1000);
// 21 days ago
long time3 = (new Date()).getTime() - (21 * 24 * 60 * 60 * 1000);
Date date0 = new Date();
date0.setTime(time0);
Date date1 = new Date();
date1.setTime(time1);
Date date2 = new Date();
date2.setTime(time2);
Date date3 = new Date();
date3.setTime(time3);
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Table table = dynamoDB.getTable(tableName);
System.out.println("Adding data to " + tableName);
// Add threads.
Item item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 1").withString("ReplyDateTime", (dateFormatter.format(date3))).withString("Message", "DynamoDB Thread 1 Reply 1 text").withString("PostedBy", "User A");
table.putItem(item);
item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 1").withString("ReplyDateTime", dateFormatter.format(date2)).withString("Message", "DynamoDB Thread 1 Reply 2 text").withString("PostedBy", "User B");
table.putItem(item);
item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 2").withString("ReplyDateTime", dateFormatter.format(date1)).withString("Message", "DynamoDB Thread 2 Reply 1 text").withString("PostedBy", "User A");
table.putItem(item);
item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 2").withString("ReplyDateTime", dateFormatter.format(date0)).withString("Message", "DynamoDB Thread 2 Reply 2 text").withString("PostedBy", "User A");
table.putItem(item);
} catch (Exception e) {
System.err.println("Failed to create item in " + tableName);
System.err.println(e.getMessage());
}
}
use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project aws-doc-sdk-examples by awsdocs.
the class TryDax method main.
public static void main(String[] args) throws Exception {
TryDaxHelper helper = new TryDaxHelper();
TryDaxTests tests = new TryDaxTests();
DynamoDB ddbClient = helper.getDynamoDBClient();
DynamoDB daxClient = null;
if (args.length >= 1) {
daxClient = helper.getDaxClient(args[0]);
}
String tableName = "TryDaxTable";
System.out.println("Creating table...");
helper.createTable(tableName, ddbClient);
System.out.println("Populating table...");
helper.writeData(tableName, ddbClient, 10, 10);
DynamoDB testClient = null;
if (daxClient != null) {
testClient = daxClient;
} else {
testClient = ddbClient;
}
System.out.println("Running GetItem, Scan, and Query tests...");
System.out.println("First iteration of each test will result in cache misses");
System.out.println("Next iterations are cache hits\n");
// GetItem
tests.getItemTest(tableName, testClient, 1, 10, 5);
// Query
tests.queryTest(tableName, testClient, 5, 2, 9, 5);
// Scan
tests.scanTest(tableName, testClient, 5);
helper.deleteTable(tableName, ddbClient);
}
use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project aws-doc-sdk-examples by awsdocs.
the class TryDaxTests method getItemTest.
void getItemTest(String tableName, DynamoDB client, int pk, int sk, int iterations) {
long startTime, endTime;
System.out.println("GetItem test - partition key " + pk + " and sort keys 1-" + sk);
Table table = client.getTable(tableName);
for (int i = 0; i < iterations; i++) {
startTime = System.nanoTime();
try {
for (Integer ipk = 1; ipk <= pk; ipk++) {
for (Integer isk = 1; isk <= sk; isk++) {
table.getItem("pk", ipk, "sk", isk);
}
}
} catch (Exception e) {
System.err.println("Unable to get item:");
e.printStackTrace();
}
endTime = System.nanoTime();
printTime(startTime, endTime, pk * sk);
}
}
use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project aws-doc-sdk-examples by awsdocs.
the class DocumentAPIBatchGet method retrieveMultipleItemsBatchGet.
private static void retrieveMultipleItemsBatchGet() {
try {
TableKeysAndAttributes forumTableKeysAndAttributes = new TableKeysAndAttributes(forumTableName);
// Add a partition key
forumTableKeysAndAttributes.addHashOnlyPrimaryKeys("Name", "Amazon S3", "Amazon DynamoDB");
TableKeysAndAttributes threadTableKeysAndAttributes = new TableKeysAndAttributes(threadTableName);
// Add a partition key and a sort key
threadTableKeysAndAttributes.addHashAndRangePrimaryKeys("ForumName", "Subject", "Amazon DynamoDB", "DynamoDB Thread 1", "Amazon DynamoDB", "DynamoDB Thread 2", "Amazon S3", "S3 Thread 1");
System.out.println("Making the request.");
BatchGetItemOutcome outcome = dynamoDB.batchGetItem(forumTableKeysAndAttributes, threadTableKeysAndAttributes);
Map<String, KeysAndAttributes> unprocessed = null;
do {
for (String tableName : outcome.getTableItems().keySet()) {
System.out.println("Items in table " + tableName);
List<Item> items = outcome.getTableItems().get(tableName);
for (Item item : items) {
System.out.println(item.toJSONPretty());
}
}
// Check for unprocessed keys which could happen if you exceed
// provisioned
// throughput or reach the limit on response size.
unprocessed = outcome.getUnprocessedKeys();
if (unprocessed.isEmpty()) {
System.out.println("No unprocessed keys found");
} else {
System.out.println("Retrieving the unprocessed keys");
outcome = dynamoDB.batchGetItemUnprocessed(unprocessed);
}
} while (!unprocessed.isEmpty());
} catch (Exception e) {
System.err.println("Failed to retrieve items.");
System.err.println(e.getMessage());
}
}
use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project aws-doc-sdk-examples by awsdocs.
the class SampleDataLoad method loadSampleForums.
private static void loadSampleForums(String tableName) {
Table table = dynamoDB.getTable(tableName);
try {
System.out.println("Adding data to " + tableName);
Item item = new Item().withPrimaryKey("Name", "Amazon DynamoDB").withString("Category", "Amazon Web Services").withNumber("Threads", 2).withNumber("Messages", 4).withNumber("Views", 1000);
table.putItem(item);
item = new Item().withPrimaryKey("Name", "Amazon S3").withString("Category", "Amazon Web Services").withNumber("Threads", 0);
table.putItem(item);
} catch (Exception e) {
System.err.println("Failed to create item in " + tableName);
System.err.println(e.getMessage());
}
}
Aggregations