Search in sources :

Example 81 with DynamoDB

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());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) Date(java.util.Date)

Example 82 with DynamoDB

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);
}
Also used : DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB)

Example 83 with DynamoDB

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);
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table)

Example 84 with DynamoDB

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());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) TableKeysAndAttributes(com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes) TableKeysAndAttributes(com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes) KeysAndAttributes(com.amazonaws.services.dynamodbv2.model.KeysAndAttributes) BatchGetItemOutcome(com.amazonaws.services.dynamodbv2.document.BatchGetItemOutcome) IOException(java.io.IOException)

Example 85 with DynamoDB

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());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) AmazonServiceException(com.amazonaws.AmazonServiceException)

Aggregations

DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)38 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)32 Table (com.amazonaws.services.dynamodbv2.document.Table)25 Item (com.amazonaws.services.dynamodbv2.document.Item)19 Test (org.junit.Test)18 AmazonServiceException (com.amazonaws.AmazonServiceException)16 HashMap (java.util.HashMap)15 TestRunner (org.apache.nifi.util.TestRunner)15 AmazonClientException (com.amazonaws.AmazonClientException)14 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)14 MockFlowFile (org.apache.nifi.util.MockFlowFile)14 ArrayList (java.util.ArrayList)11 Map (java.util.Map)11 TableWriteItems (com.amazonaws.services.dynamodbv2.document.TableWriteItems)10 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)10 IOException (java.io.IOException)10 TableKeysAndAttributes (com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes)9 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)9 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)9 List (java.util.List)9