Search in sources :

Example 46 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project aws-doc-sdk-examples by awsdocs.

the class CreateTablesLoadData method deleteTable.

private static void deleteTable(String tableName) {
    Table table = dynamoDB.getTable(tableName);
    try {
        System.out.println("Issuing DeleteTable request for " + tableName);
        table.delete();
        System.out.println("Waiting for " + tableName + " to be deleted...this may take a while...");
        table.waitForDelete();
    } catch (Exception e) {
        System.err.println("DeleteTable request failed for " + tableName);
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table)

Example 47 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project aws-doc-sdk-examples by awsdocs.

the class CreateTablesLoadData method loadSampleThreads.

private static void loadSampleThreads(String tableName) {
    try {
        // 7
        long time1 = (new Date()).getTime() - (7 * 24 * 60 * 60 * 1000);
        // days
        // ago
        // 14
        long time2 = (new Date()).getTime() - (14 * 24 * 60 * 60 * 1000);
        // days
        // ago
        // 21
        long time3 = (new Date()).getTime() - (21 * 24 * 60 * 60 * 1000);
        // days
        // ago
        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);
        Item item = new Item().withPrimaryKey("ForumName", "Amazon DynamoDB").withString("Subject", "DynamoDB Thread 1").withString("Message", "DynamoDB thread 1 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date2)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("index", "primarykey", "table")));
        table.putItem(item);
        item = new Item().withPrimaryKey("ForumName", "Amazon DynamoDB").withString("Subject", "DynamoDB Thread 2").withString("Message", "DynamoDB thread 2 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date3)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("index", "partitionkey", "sortkey")));
        table.putItem(item);
        item = new Item().withPrimaryKey("ForumName", "Amazon S3").withString("Subject", "S3 Thread 1").withString("Message", "S3 Thread 3 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date1)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("largeobjects", "multipart upload")));
        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) HashSet(java.util.HashSet)

Example 48 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project aws-doc-sdk-examples by awsdocs.

the class CreateTablesLoadData method loadSampleProducts.

private static void loadSampleProducts(String tableName) {
    Table table = dynamoDB.getTable(tableName);
    try {
        System.out.println("Adding data to " + tableName);
        Item item = new Item().withPrimaryKey("Id", 101).withString("Title", "Book 101 Title").withString("ISBN", "111-1111111111").withStringSet("Authors", new HashSet<String>(Arrays.asList("Author1"))).withNumber("Price", 2).withString("Dimensions", "8.5 x 11.0 x 0.5").withNumber("PageCount", 500).withBoolean("InPublication", true).withString("ProductCategory", "Book");
        table.putItem(item);
        item = new Item().withPrimaryKey("Id", 102).withString("Title", "Book 102 Title").withString("ISBN", "222-2222222222").withStringSet("Authors", new HashSet<String>(Arrays.asList("Author1", "Author2"))).withNumber("Price", 20).withString("Dimensions", "8.5 x 11.0 x 0.8").withNumber("PageCount", 600).withBoolean("InPublication", true).withString("ProductCategory", "Book");
        table.putItem(item);
        item = new Item().withPrimaryKey("Id", 103).withString("Title", "Book 103 Title").withString("ISBN", "333-3333333333").withStringSet("Authors", new HashSet<String>(Arrays.asList("Author1", "Author2"))).withNumber("Price", 2000).withString("Dimensions", "8.5 x 11.0 x 1.5").withNumber("PageCount", 600).withBoolean("InPublication", false).withString("ProductCategory", "Book");
        table.putItem(item);
        // Add bikes.
        item = new Item().withPrimaryKey("Id", 201).withString("Title", "18-Bike-201").withString("Description", "201 Description").withString("BicycleType", "Road").withString("Brand", "Mountain A").withNumber("Price", 100).withStringSet("Color", new HashSet<String>(Arrays.asList("Red", "Black"))).withString("ProductCategory", "Bicycle");
        table.putItem(item);
        item = new Item().withPrimaryKey("Id", 202).withString("Title", "21-Bike-202").withString("Description", "202 Description").withString("BicycleType", "Road").withString("Brand", "Brand-Company A").withNumber("Price", 200).withStringSet("Color", new HashSet<String>(Arrays.asList("Green", "Black"))).withString("ProductCategory", "Bicycle");
        table.putItem(item);
        item = new Item().withPrimaryKey("Id", 203).withString("Title", "19-Bike-203").withString("Description", "203 Description").withString("BicycleType", "Road").withString("Brand", "Brand-Company B").withNumber("Price", 300).withStringSet("Color", new HashSet<String>(Arrays.asList("Red", "Green", "Black"))).withString("ProductCategory", "Bicycle");
        table.putItem(item);
        item = new Item().withPrimaryKey("Id", 204).withString("Title", "18-Bike-204").withString("Description", "204 Description").withString("BicycleType", "Mountain").withString("Brand", "Brand-Company B").withNumber("Price", 400).withStringSet("Color", new HashSet<String>(Arrays.asList("Red"))).withString("ProductCategory", "Bicycle");
        table.putItem(item);
        item = new Item().withPrimaryKey("Id", 205).withString("Title", "20-Bike-205").withString("Description", "205 Description").withString("BicycleType", "Hybrid").withString("Brand", "Brand-Company C").withNumber("Price", 500).withStringSet("Color", new HashSet<String>(Arrays.asList("Red", "Black"))).withString("ProductCategory", "Bicycle");
        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) HashSet(java.util.HashSet)

Example 49 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project aws-doc-sdk-examples by awsdocs.

the class MoviesCreateTable 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);
    String tableName = "Movies";
    try {
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName, // Partition
        Arrays.asList(// Partition
        new KeySchemaElement("year", KeyType.HASH), // key
        new KeySchemaElement("title", // Sort key
        KeyType.RANGE)), Arrays.asList(new AttributeDefinition("year", ScalarAttributeType.N), new AttributeDefinition("title", ScalarAttributeType.S)), new ProvisionedThroughput(10L, 10L));
        table.waitForActive();
        System.out.println("Success.  Table status: " + table.getDescription().getTableStatus());
    } catch (Exception e) {
        System.err.println("Unable to create table: ");
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 50 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project aws-doc-sdk-examples by awsdocs.

the class MoviesItemOps01 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";
    final Map<String, Object> infoMap = new HashMap<String, Object>();
    infoMap.put("plot", "Nothing happens at all.");
    infoMap.put("rating", 0);
    try {
        System.out.println("Adding a new item...");
        PutItemOutcome outcome = table.putItem(new Item().withPrimaryKey("year", year, "title", title).withMap("info", infoMap));
        System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult());
    } catch (Exception e) {
        System.err.println("Unable to add item: " + year + " " + title);
        System.err.println(e.getMessage());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) PutItemOutcome(com.amazonaws.services.dynamodbv2.document.PutItemOutcome) Table(com.amazonaws.services.dynamodbv2.document.Table) HashMap(java.util.HashMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Aggregations

Table (com.amazonaws.services.dynamodbv2.document.Table)63 Item (com.amazonaws.services.dynamodbv2.document.Item)40 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)38 AmazonServiceException (com.amazonaws.AmazonServiceException)35 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)31 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)28 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)28 IOException (java.io.IOException)24 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)23 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)23 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)23 HashMap (java.util.HashMap)23 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)22 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)22 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)17 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)17 ArrayList (java.util.ArrayList)17 AmazonClientException (com.amazonaws.AmazonClientException)14 Test (org.junit.Test)14 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)10